Please post your Web Driver questions in official Web Driver forum

Sunday, July 31, 2016

What when your employer kills software testing job?

Screenshot from 2016-07-27 14:58:21.png

Few months ago I faced a situation wherein my employer decided to stop having software testing job. Development teams were tasked to carry out QA /Testing activities along with development tasks. Only time would tell how successful or disastrous it turns out to be but my QA colleagues and I were faced with difficult times especially when many of us were in a foreign country. Test automation engineers were given the option to switch to development but I decided to not switch. I decided to continue with in the field of software testing which eventually resulted in me finding a new job. Here are my thoughts on what you can do if you also face this situation when your software testing job is killed -

  • Knowledge transfer to development team. Devs are usually lazy when it comes to integration/system testing (If not then probably we would have never had software testing field) or they don’t have enough information about all the pieces of system put together. You would do great favour to them to share system and testing knowledge with them so that they can continue with testing in your absence.

  • Look for short term testing opportunities within your current employer. Sounds contradictory when your employer does not want to have software tester at all? If you work in an MNCs then chances are that subject matter experts would be required by few teams. Soon after I was told that I should switch to development I was contacted by one team to help them on performance testing. They had been trying to hire performance testing expert but did not any have success. This was one opportunity I could not say No to. I had worked on performance testing in past but was far away from being a subject matter expert. In jumped in to new task, got to work on performance testing (exclusively) for six months. Lots of learning and interesting results. If I had said no to this opportunity then I would not have been able to learn about performance testing what I know today. If you want to know more about my performance testing learning then you can follow Performance Testing videos

  • Don’t switch to another role for sake of it. Don’t take on development or any other role your heart is not ready to work with. Never let one company decide about the field you should work on. Follow your heart and work on what you want to work on. If you take up an unwanted role under pressure then you would end up doing shoddy job anyways. Would it be worth it?

  • Last but not the least, gear up to find a new job. In fact be ready to find a new job any time in your career. There are no permanent jobs in IT industry but there are many companies on lookout for software tester. This is what I did and found a new job in the fields of software testing. I am not a developer and can not let someone else decide what I want to work on (full stop)

Have you ever faced such situation? What did you do? Or what would you do when you face such situation in future?

Monday, July 25, 2016

Automatic JS error detection with STF and WebDriver

If you followed my post on Catching java script errors with WebDriver then you already know how browser console logs can be used to catch javascript errors. But a problem which surfaces is that javascript error check has to be added on each test step where java script error check is to be done. This would result in lots of boilerplate code. A better approach would be to automate this check.

This is what STF 4.0.14 does. It introduces CustomEventListener class which catches SEVERE errors on browser console. But before delving into CustomEventListener, let’s understand two classes available from webdriver API -

EventFiringWebDriver implements WebDriver interface besides other interfaces. It supports registering WebDriverEventListener which can be used for logging purpose. For example get() method to open application URL looks as following in EventFiringWebDriver -

public void get(String url) {
dispatcher.beforeNavigateTo(url, driver);
dispatcher.afterNavigateTo(url, driver);

Hence it is a call to get method from WebDriver interface along with dispatcher statement before and after it. Herein beforeNavigateTo and afterNavigateTo are the methods in WebDriverEventListener interface. Other methods in EventFiringWebDriver follow the same structure, for ex click method looks as -

public void click() {
dispatcher.beforeClickOn(element, driver);;
dispatcher.afterClickOn(element, driver);

Herein WebDriverEventListener is the interface (described later) which does the job of logging or carrying out an operation you want before get(), click() or any other WebDriver operation.
There are two more important methods in EventFiringWebDriver which are used to register or unregister a WebDriverEventListener  -

public EventFiringWebDriver register(WebDriverEventListener eventListener) {
return this;

public EventFiringWebDriver unregister(WebDriverEventListener eventListener) {
return this;

This is a listener interface which should be implemented to catch WebDriver events. If you don’t want to implements all of the methods in this interface than you can extend AbstractWebDriverEventListener class and override those methods (or events) for which you want to collect logs

And this is what STF does. CustomEventListener extends AbstractWebDriverEventListener class and overrides navigation and click methods -

public void beforeNavigateTo(String url, WebDriver webDriver) {
  logErrors(url, getBrowserLogs(webDriver));

public void afterNavigateTo(String url, WebDriver webDriver) {
  logErrors(url, getBrowserLogs(webDriver));

public void beforeClickOn(WebElement element, WebDriver driver) {
  logErrors("before", element, getBrowserLogs(driver));

public void afterClickOn(WebElement element, WebDriver driver) {
  logErrors("after", element, getBrowserLogs(driver));
getBrowserLogs gets the log entries for browser -

private LogEntries getBrowserLogs(WebDriver webDriver) {
  return webDriver.manage().logs().get(LogType.BROWSER);

and logErrors() logs the Severe browser console errors -

private void logErrors(String url, LogEntries logEntries) {
  if (logEntries.getAll().size() == 0) {
      TestLogging.log("********* No Severe Error on Browser Console *********", true);
  } else {
      for (LogEntry logEntry : logEntries) {
          if (logEntry.getLevel().equals(Level.SEVERE)) {
              TestLogging.log("URL: "+url);
              TestLogging.logWebStep("Time stamp: " + logEntry.getTimestamp() + ", " +
                      "Log level: " + logEntry
                      .getLevel() + ", Log message: " + logEntry.getMessage(), true);
              isJSErrorFound = true;
      assert !isJSErrorFound;

And STF takes care of instantiating EventFiringWebDriver. But if you are not using STF then you can instantiate EventFiringWebDriver as following and use it in rest of your tests -

WebDriver driver =
CustomEventListener eventListener = new CustomEventListener();
EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver(driver);

How do I use STF java script error check?

You should be using STF 4.0.14. Add property eventFiringWebDriver to your testng.xml file and set it to true -
<parameter name="eventFiringWebDriver" value="true"/>

That’s all and you are ready to catch all javascript errors from your tests. Simple is not it? :-)
I added a test in testng.xml file which demonstrate catching javascript errors with WebDriver. When you run test and open test report then you would find javascript error reported as following -

Screenshot from 2016-07-13 16:50:13.png

The test above uses the page with javascript error created by Alister Scott


  • Browser Logging mechanism is beta hence you may encounter unexpected behaviour.

  • I did not have great success in catching JS error on firefox browser. If you run the test from testng.xml file on firefox browser then no error is reported.

So what are you waiting for? Don’t let the JS error slip into production any more. Try it out and share your experience :)

Monday, July 18, 2016

Catching java script errors with WebDriver

The topic of catching java script errors with webdriver has been discussed time and again. This was also a hot topic during the days of Selenium RC. There have been quite a few approaches to catch java script errors with WebDriver, like  -

  • Adding extra script to web page and using JavascriptExecutor to retrieve them. I won’t recommend this approach as it requires application code to be changed to be able to test it

  • Using Firefox extension to catch the java script errors

  • And of late, there has been a beta feature available in WebDriver APIs which lets you capture browser console log. Using this you can capture browser level logs. I like this approach as it does not require application code to be modified or adding extra extension to a browser. I experimented this approach with FF 47.0.1, Chrome 50 and Selenium 2.53.1 and worked like a charm.

Let’s consider blog - which has a 404 error and it is visible on console. (By the very useful blog, you may like to follow it if not already :)) -

Screenshot from 2016-07-11 16:54:06.png

Browser console is also the place where javascript error would logged. Let’s catch and print this error on Chrome. Here is a sample code to do this -

package com.seleniumtests.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.testng.annotations.Test;

public class JavaScriptErrorTest {
  WebDriver webDriver;
  LogEntries logEntries;

  public static void logConsoleEntries (LogEntries logEntries) {
      for (LogEntry logEntry : logEntries) {
          System.out.println(String.valueOf(" Time Stamp: " + logEntry.getTimestamp()));
          System.out.println(String.valueOf(" Log Level: " + logEntry.getLevel()));
          System.out.println(String.valueOf(" Log Message: " + logEntry.getMessage()));

  public void catchJavaScriptError() throws Exception {
      String url = "";
      webDriver = new ChromeDriver();
      logEntries = webDriver.manage().logs().get(LogType.BROWSER);

And following message is printed -

Time Stamp: 1468249126412 Log Level: SEVERE Log Message: 0:0 Failed to load resource: the server responded with a status of 404 ()

You can build your assert statement around log.

Let’s see an example of javascript error now and this time with Firefox browser. We will consider website - for testing and it seems to have quite a few java scripts errors. Lets update our test to use Firefox driver -

public void catchJavaScriptError() throws Exception {
  String url = "";
  webDriver = new FirefoxDriver();
  logEntries = webDriver.manage().logs().get(LogType.BROWSER);

This would print lots of logs and one log would be a js error -

Time Stamp: 1468310250086 Log Level: SEVERE Log Message: TypeError: $(...).flexslider is not a function

If you want to restrict the amount of logs then you can restrict the logs to a given level when instantiating your driver -

DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
LoggingPreferences loggingPreferences = new LoggingPreferences();
loggingPreferences.enable(LogType.BROWSER, Level.SEVERE);
desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, loggingPreferences);
webDriver = new FirefoxDriver(desiredCapabilities);

And now you would only have sever errors printed. I am reluctant to use this method of restricting amount of logs as I see different results in chrome and firefox. For example logging SEVERE errors reported following on chrome -

Time Stamp: 1468311387330 Log Level: SEVERE Log Message: 0:0 Failed to load resource: net::ERR_NAME_NOT_RESOLVED Time Stamp: 1468311387331 Log Level: SEVERE Log Message: 0:0 Failed to load resource: net::ERR_NAME_NOT_RESOLVED Time Stamp: 1468311387331 Log Level: SEVERE Log Message: 0:0 Failed to load resource: net::ERR_NAME_NOT_RESOLVED Time Stamp: 1468311387331 Log Level: SEVERE Log Message: 0:0 Failed to load resource: net::ERR_NAME_NOT_RESOLVED Time Stamp: 1468311387331 Log Level: SEVERE Log Message: 0:0 Failed to load resource: net::ERR_NAME_NOT_RESOLVED Time Stamp: 1468311387584 Log Level: SEVERE Log Message: javascript 1:187 Uncaught TypeError: Cannot read property 'title' of undefined Time Stamp: 1468311387859 Log Level: SEVERE Log Message: javascript 1:4571 Uncaught TypeError: Cannot read property '0' of undefined

And following on firefox -

Time Stamp: 1468311872314 Log Level: SEVERE Log Message: TypeError: entry is undefined Time Stamp: 1468311872464 Log Level: SEVERE Log Message: TypeError: json.feed.entry is undefined Time Stamp: 1468311873221 Log Level: SEVERE Log Message: TypeError: json.feed.entry is undefined Time Stamp: 1468311873969 Log Level: SEVERE Log Message: SyntaxError: missing ) after argument list Time Stamp: 1468311876268 Log Level: SEVERE Log Message: TypeError: $(...).flexslider is not a function

Hence I prefer to not restrict the amount the log, print all the log and parse log for possible errors.

Form the above example, it is clear that we can capture logs for both Firefox and Chrome browser. But beware that this function is yet beta (at the time of writing this post) and you may encounter unexpected errors.

But collecting browser logs for javascript errors on each event would result in lots of extra code and corresponding checks. What if we could automate javascript error check which takes place on each test method? I can think of listener doing this for us. What do you think?
Fork me on GitHub