I want to write a python script that can check/test the css on a website.
selenium webdriver is equipped with value_of_css_property(property_name), which can be used to extract value of a css property for an element. This can be compared with an expected value. The selenium can then interact and if required record the changes in property and again compare with the expected value.
or capture screenshot of an element and then use PIL to compare the screenshot with the expected one (something what python needle does),however since size and placement change according to resolution/device I don't know how feasible this approach is.
any thoughts as to what can be done from above or any other suggestions?
We've been using Galen Framework for this particular "visual testing" problem:
Layout testing seemed always a complex task. Galen Framework offers a
simple solution: test location of objects relatively to each other on
page. Using a special syntax and comprehensive rules you can describe
any layout you can imagine
Galen Framework is designed with responsiveness in mind. It is easy to
set up a test for different browser sizes. Galen just opens a browser,
resizes it to a defined size and then tests the page according to
specifications
We are writing Galen tests in JavaScript, there is a Python port, but not sure how up-to-date it is.
needle is an awesome tool, but images overall are "clumsy" and hard to deal with and maintain. If you make your sizes and resolutions concrete, then it would make things easier.
Using value_of_css_property() is always an option, but it might not scale well and it would be challenging to avoid violating the DRY principle. Think of some sort of an abstraction layer over checking your CSS properties. For instance, have a "style" config in your tests with pre-defined styles with a number of required properties (e.g. every "submit" button in your application needs to have btn and btn-primary classes and needs to have a specific size, font size, background and border colors)..then you can configure a pre-defined style for every page object field in a page object..just thoughts.
Related
I'm working on a school project which I would like to showcase in a web browser or application.
I would like the user to control the work with a mouse or keyboard. I want to show a unique image based on where the curser is over a visible grid. An additional feature is the ability to switch to a different "stack" of images upon user input from the scroll wheel or in a dialog.
I have a beginner-intermediate understanding of Python.
Theoretically, I could write this using Sage, but I would like the feedback to be instant - a change shouldn't require a new calculation, just show a new image.
Additionally, I would like to create a feature which takes the user on a "tour" based on information attached to an image.
My first thought was to use an online website builder (Webflow), though an opportunity to learn a new language or expand upon my knowledge of Python is my first choice.
What language is best suited for this?
This is possible in Python, as nearly everything is (Python is a Genral Purpose Language), so you could certainly implement this in Python.
The best language for this, however,IMO, would be JavaScript.
Python will almost certainly get in your way or at least hinder you slightly in comparison.
An 'online website builder' is not likely to provide you with the required amount to control needed to implement you project - most of these are painfully simplistic drag-and-drog tools where any real control only comes from adding your own CSS/HTML/JS anyways.
JS is an incredibly useful language and also very well suited for nearly all web/browser projects, so use this opportunity to learn it !
Further, React Native can let you use JS for mobile apps too, if that's what you meant by 'applications' or you could simply keep it a web app.
PS. This may also be possible with HTML5, which is perhaps simpler and easier to learn, but I'm no a web dev so that will have to be confirmed by someone else.
I am sure, though, that this is very efficiently doable in JS.
I am testing complex and non-public webpages with python-selenium, which have interconnected iframes.
To proper click on a button or select some given element in a different iframe I have to switch to that iframe . Now, as contents of the pages might reload to the correct iframe I constantly have to check if the correct iframe is loaded yet, otherwise I have to go back to the default content, do the check again etc.
I find this completely annoying and user-unfriendly behavior of selenium.
Is there a basic workaround to find e.g. an element in ANY iframe? Because I do not care about iframes. I care about elements...
Unfortunately, no, there’s no way around this.
For context, this is likely not simply a limitation of Selenium alone, but of the WebDriver specification and, ultimately, modern browsers. Selenium merely implements the WebDriver specification, which in turn is limited by the features exposed by modern browsers. And browsers likely have good reasons for preventing you from doing this.
What you think of as a single page is actually comprised of multiple documents:
the root document, whose URL and title you see in your browser chrome, and
one or more embedded (or child) documents, for which an <iframe> element is really just a kind of “mount point.”
While the utility of being able to transparently traverse across document boundaries (as easily as one might traverse across a file system mount point) is obvious, browsers likely have their reasons for blocking it.
Not the least of these, I suspect, is to prevent cross-site scripting (XSS) attacks. That is, just because the browser user has the ability to view an embedded document, doesn’t mean a script in the parent document should be able to “see” into it. And allowing traversal from the parent into the child (via, e.g., find_element_by_xpath), would likely require that.
(Another reason, I imagine, is that most modern browsers isolate each document in a separate process, making traversal across their respective DOMs a far more complicated feature to implement.)
Easing the burden with capybara-py
Given that one must switch contexts in order to search for and interact with elements in other documents, you can make it easier on yourself by adopting capybara-py. It’s a layer on top of Selenium that provides (among many other things) simple context managers for switching between frames and windows:
from capybara.dsl import page
with page.frame("name-of-child-frame"):
page.click_link("The label of a link inside the child frame")
page.assert_text("Some text that is now expected to appear in the parent document")
Unfortunately the API is built that way and you can't do anything about it. Each IFrame is a separate document as such, so eventually search a object in every IFrame would mean Selenium has to switch to every IFrame and do that for you.
Now you can build a workaround by storing the IFrame paths and using helper methods to automatically switch to that IFrame hierarchy in your code. Selenium won't help you here, but you can ease your pain by writing helper methods designed as per your needs
I'm contemplating using python for some functional testing of flash ad-units for work. Currently, we have an ad (in flash) that has N locations (can be defined as x,y) that need to be 'clicked'. I'd like to use python, but I know Java will do this.
I also considered Jython + Sikuli, but wanted to know if there is a python only library or tool to do this. I'd prefer to not run Jython + Sikuli if there is a native python option.
TIA.
#user1929959 From the pyswftools page, "At the moment, the library can be used in Python applications (including WebBased applications) to generate Flash animations on the fly.". And from the bottle-flash page, "This plugin enables flash messages in bottle.". Neither help me, unless I'm overlooking something ...
There are a number of ways I've seen around the net, but most seem to involve exposing Flash through JS and then using the JS interface, which is a bit of a problem if you are trying to test things that you don't have dev access to, or need to be in a prod-like state for your tests. Of course, even if you do that, you aren't really simulating user interaction, since you are working through an API.
If you can reliably model your Flash components with fixed pixel positions relative to the page element the Flash component is running in, you should be able to use Selenium Webdriver to position the mouse cursor and send click commands without actually cracking Flash itself. I'm not 100% sure that would work, but it seems at least worth a shot. Validation will be a bit trickier, but I think you should be able to do it with some form of image comparison. A few of the Flash automators I saw are actually using image processing under the hood to control both input and output, so it seems like a legitimate way to interact with it.
How you can realize a minimized view of a html page in a div (like google preview)?
http://img228.imageshack.us/i/minimized.png/
edit: ok.. i see its a picture on google, probably a minimized screenshot.
This is more or less a duplicate of the question: Create thumbnails from URLs using PHP
However, just to add my 2¢, my strong preference would be to use an existing web service, e.g. websnapr, as mentioned by thirtydot in the comments on your question. Generating the snapshots yourself will be difficult to scale well, and just the kind of thing I'd think is worth using an established service for.
If you really do want to do this yourself, I've had success using CutyCapt to generate snapshots of webpages - there are various other similar options (i.e. external programs you can call to do the rendering) mentioned in that other question.
google displays an image thumbnail, so you would need to generate an image using GD or ImageMagic.
The general flow would be
Fetch page content, including stylesheets and all images via curl (potentially tricky to capture all the embedded files but shouldn't be beyond a competent PHP programmer
Construct a rendering of the page inside PHP itself (EXTREMELY tricky! Wouldn't even know where to start with that, though there might be some kind of third party extension available)
Use GD/Imagemagic/whatever to generate a thumbnail image in an appropriate format (shouldn't be too hard).
Clearly, it's the rendering the page from the HTML, CSS, images etc you downloaded that is going to be the difficult part.
Personally I'd be wondering if the effort involved is worth it.
I'm looking for a python browser widget (along the lines of pyQT4's QTextBrowser class or wxpython's HTML module) that has events for interaction with the DOM. For example, if I highlight an h1 node, the widget class should have a method that notifies me something was highlighted and what dom properties that node had (<h1>, contents of the tag, sibling and parent tags, etc). Ideally the widget module/class would give access to the DOM tree object itself so I can traverse it, modify it, and re-render the new tree.
Does something like this exist? I've tried looking but I'm unfortunately not able to find it. Thanks in advance!
It may not be ideal for your purposes, but you might want to take a look at the Python bindings to KHTML that are part of PyKDE. One place to start looking is the KHTMLPart class:
http://api.kde.org/pykde-4.2-api/khtml/KHTMLPart.html
Since the API for this class is based on the signals and slots paradigm used in Qt, you will need to connect various signals to slots in your own code to find out when parts of a document have been changed. There's also a DOM API, so it should also be possible to access DOM nodes for selected parts of the document.
More information can be found here:
http://api.kde.org/pykde-4.2-api/khtml/index.html
I would also love such a thing. I suspect one with Python bindings does not exist, but would be really happy to be wrong about this.
One option I recently looked at (but never tried) is the Webkit browser. Now this has some bindings for Python, and built against different toolkits (I use GTK). However there are available API for the entire Javascript machine for C++, but no Python bindings and I don't see any reason why these can't be bound for Python. It's a fairly huge task, I know, but it would be a universally useful project, so maybe worth the investment.
If you don't mind being limited to Windows, you can use the IE browser control. From wxPython, it's in wx.lib.iewin.IEHtmlWindow (there's a demo in the wxPython demo). This gives you full access to the DOM and ability to sink events, e.g.
ie.document.body.innerHTML = u"<p>Hello, world</p>"