Skip to main content

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);
driver.get(url);
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);
element.click();
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) {
eventListeners.add(eventListener);
return this;
}


public EventFiringWebDriver unregister(WebDriverEventListener eventListener) {
eventListeners.remove(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 -


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


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


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


@Override
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);
eventFiringWebDriver.register(eventListener);
eventFiringWebDriver.get(“www.google.com”)


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


Caveats:


  • 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 :)

Popular posts from this blog

Return only first or last element from webelements collection

We often come across situation when there are multiple elements on a page and we probably like to exercise only a few of them using selenium webdriver. May be just first and last element. For example on a search result page we may like to click on only first and last link and not all. This is when Iterables API comes handy. (By the way I am assuming that you have already completed watching selenium training videos :)). Once we have collection of web element then we can use Iterables to get only first or last element as following - Consider that we fetch collection of element as - List< WebElement > webElements = getDriver().findElements(By. id ( "htmlID" ));   Now we can get the first web element from this collection as -  WebElement firstElement = Iterables. getFirst (webElements,  getDriver().findElement(By. id ( "defaultElement" )));   Herein second argument -   (getDriver().findElement(By. id ( "defaultElement" )))    in the me

Using chrome console to test xPath and css selectors

Since the advent of selenium there have been many plugin to test xPath / css selectors but you don’t need any of them if you have chrome browser. Using Chrome console you can test both xPath and css selectors. Launch website to be tested in chrome browser and hit F-12 and you would see chrome console opened in lower pane of application - Hit escape key and console would open another pane to write element locators - And now you can start writing xPath or css selectors in chrome console and test them - The syntax for writing css id - $$(“ ”) And hit the enter key. If your expression is right then html snippet of the application element corresponding to the css selector would be displayed - If you mouse over the html snippet in chrome console then it would highlight the corresponding element in application - If you want to clean console of previously written element selectors then just hit ctrl+L keys and chrome console would be empty again. Pro

Using xPath to reach parent of an element

Well, I am big fan of css locator my self there are times when css locators don't fit requirement. One such requirement is when you want to navigate to parent element of an element and may be parent of parent and even more. Unfortunately css locators don't provide any mechanism to navigate to parent of an element. See this for more. Of late I came across a scenario when I wanted to click on a link depending upon the text in a text box. Herein parent of text box and parent of link were at the same location. More over there could have been many such combinations in application. Fortunately I just need to pick first such instance and Web Driver any way considers only first instance when multiple locators are found matching an element. Element in question is in following html - Here I need to click on highlighted anchor on the basis of input element (which is also highlighted in image) Herein first I need to reach div parent (class = 'left couponmainarea