Selenium and Javascript Events

Selenium is a great way to test web applications and it has Python bindings. I explained in a previous post how to set this up with coverage analysis. However, writing tests is non-trivial, in particular it is easy enough to write tests that suffer from race conditions. Suppose you write a test that includes a check for the existence of a particular DOM element. Here is a convenient method to make doing so a one-liner. It assumes that you are within a class that has the web driver as a member and that you’re using ‘pytest’ but you can easily adapt this for your own needs. ...

March 16, 2016

Update Flask and Coverage

Update: Flask+Coverage Analysis In a previous post I demonstrated how to get coverage analysis working for a Flask web application in a relatively simple manner. In the section “At then end of your tests” I stated that you needed your tests to clean-up by telling the server to shutdown. The end of your test code would look something like this: finally: driver.get(get_url('shutdown')) ... This could have made things a little fiddly since your test code would have to make sure to access the shutdown route exactly once, regardless of how many tests were run. ...

January 26, 2016

Flask and Coverage Analysis

Flask + Coverage Analysis This post demonstrates a simple web application written in Flask with coverage analysis for the tests. The main idea should be pretty translatable into most Python web application frameworks. Update I’ve updated this scheme and described the update here. tl;dr If you’re having difficulty getting coverage analysis to work with Flask then have a look at my example repository. The main take away is that you simply start the server in a process of its own using coverage to start it. However, in order for this to work you have to make sure you can shut the server process down from the test process. To do this we simply add a new “shutdown” route which is only available under testing. Your test code, whether written in Python or, say Javascript, can then make a request to this “shutdown” route once it completes its tests. This allows the server process to shutdown naturally and therefore allow ‘coverage’ to complete. ...

January 25, 2016

Selenium vs CasperJS

Suppose you have a web service using a Python web framework such as Flask. So you wish to do a full user test by automating the browser. Should you use the Python bindings to Selenium or CasperJS? In this post I wish to detail some of the advantages and drawbacks of both. Python + Selenium Setup This is fairly straightforward. You are simply writing some Python code that happens to call a library which binds to Selenium. In order for this to work you will need to install phantomJS but using npm this is pretty trivial. ...

January 8, 2016