Automate web testing with Selenium

For some months, I have been using Selenium WebDriver a lot for automating the web testing procedures. If you haven’t heard of this piece of software yet, I highly recommend it to anyone in the software testing industry and for anyone who often performs repetitive tasks in a web browser.

The basic idea of this software is that it is able to execute commands to manipulate the web browser pretty much like a user would plus it can execute JavaScript commands to simulate certain events. You can start a web browser, navigate to a predefined URL, fill in some forms, press a button and evaluate the contents of the web page shown. You are free to write the sequence of the operations that will be taken in any supported language such as Java, Python or Ruby and some others.

This means that you will be able to automate a lot of the work you might have used to do manually. Think of a web application that you work on. You probably have certain tests you perform over and over every build or release and find yourself doing the same thing again and again. When the number of things to check gets large, it is also easy to miss a certain workflow which could have negative consequences in the QA/QC terms. Having a script for the tests will serve as a documentation for your colleagues. The script is also easy to extend to accumulate the regression test cases. The script can be executed as scheduled without you doing anything at all which might be a huge time saver.

Sounds interesting? Start at the Selenium home page. Navigate to the Download and download Selenium Client & WebDriver Language Binding of your choice (I am using Python binding). After downloading and installing the module, fire up your IDE (I use Wing IDE and like it very much) and let’s build a simple script.

Navigate to this example. Remember to choose the language you want to see examples in within the Programming Language Preference section that will follow you on the screen. Then you will see the code only in one language. The official Selenium documentation is great and the API itself is very intuitive and easy to get started. After finishing the tutorials and playing a bit, consider writing some small unit tests for the web application you work on. If you use Python, unittest module is probably the best to start with and you could wrap your Selenium script into individual test cases and run them from the command line or IDE of your choice.

If you have access to Pluralsight, there is a great course by John Sonmez called Automated Web Testing with Selenium. It helped me to get on track in no time at all and I highly recommend this one.

If you want to perform some tests that involve interacting with web map components and GIS functionality, you might find it hard to find any useful information on any best practices or helpful tips where to start.

I was interested in testing web apps build with ArcGIS API for JavaScript by using Selenium, too. From what I’ve found I could perform certain light interactions with pure Selenium (testing changing the basemap and zooming in/out by invoking keystrokes and mouse events). But it gets harder to get into the deep interaction with the Esri JS API. I’ve search for the information here at GIS.SE. There you can find more information on what kind of JS libraries can be used for web testing.

Here are some useful features of Selenium you should be aware of:

  • You can take screenshots of the web browser while the program is executed. This implies that you will be able to open the saved images later on and review the web application visually if there are some parts that are hard to test automatically (such as the layout design). Having an image of how the web application looked like earlier, you could use any image comparison algorithm (like PIL in Python) to find what have changed in the web application.
  • You can execute JavaScript code when running a browser. This means you can interact with the web map component and get its properties, such as the map extent:
console.log(map.extent.toJson());
  • It is possible to resize the window with the browser opened so it will not obstruct other open windows.
driver.set_window_size(500, 500)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s