Wednesday, September 2, 2015

Test Automation Trick: Avoid driving the browser

It is possible to speedup test automation if we learn small tricks. Not always using Webdriver (or any such tool that drives browser) is one such technique. For example, consider the following scenario –

Test Search-Functionality: When user searches for “Laptop”, following should happen
  1. 1st search result should be “xyz”
  2. 2nd search result should be “123”
  3. First page should contain 10 search results

Webdriver Solution: Yes, it is easy to do this using Webdriver.  Possibly, you will be automating the following steps

  1. Open browser
  2. Get the desired URL
  3. Find the search box element
  4. Send data to the search box
  5. Find submit button and click on it
  6. Then once the page reloads, you verify the search result

Problem: There are some unnecessary tasks involved in the above process. For example
  1. You are opening a browser. However, the test is not about browser. It is about the search functionality. Perhaps you will test appearance of the search page as part of another test.
  2. You are looking for elements, sending values to those, and clicking on those. Again, this is not part of the testing. Those elements may change in future, but the search function will be there.
  3. You are opening the browser and waiting for page to load. This might add overhead because when browser loads, there are lots of stuff associate with it that are completely out of scope of this testing.

Alternate solution: For me, the alternate solution is not to use Webdriver at all. Rather I would do the followings
  1. Manually initiate the HTTP request raised when user searches for an item
  2. Capture the response
  3. And verify the search item in the response

Example: Suppose I am performing the test in www.ekhanei.com. When I search for “Laptop”, I’ve noticed that it raises a HTTP request as follows



Now that I know the HTTP request, my task is to manually raise this request and capture the response. Following code in Python will do it




The code is very simple and the comment in the code explains it all. As you can notice, I have used Requests module of Python to raise the HTTP request and to receive response.

This is just an example. The key here is to identify tests that do not need driving browsers at all. Examples of similar other tests could be checking title of a page, checking content of a page, checking number of appearance of some items in a page and so on.