How to investigate and debug a Selenium WebDriver `@Test` error

Here’s the situation. You’ve written some code to automatically execute your application. You’re using Selenium WebDriver.

Bam.

Something goes wrong.

You don’t know what to do.

Do you:

  • A) investigate?
  • B) ask someone else to fix your problem?

If you answered B to the question above, then in this post I’ll show you some strategies to help with (A).

You do have to develop the skills to investigate your own issues. Learning how to investigate will help you build those skill sets so you can do your job better. It doesn’t mean that you never ask for help, but it means you can have more self-reliance and when you ask for help you will have more information to provide the person you have asked for help.

Bing the error to Google

Don’t make the problem something that the other person can google for you

Use your favourite web search engine to search for the error message.

Hints:

  • Copy and paste the exact text
    • do not write it using your own words based on what you think it means
    • because you don’t know what it means, that’s why your searching
  • You may have to copy and paste part, or parts, of the error message
    • your error may have appeared on a specific line number, don’t include that in the search
      your error message may be related to specific environment variable, don’t include that in the search
  • put quotes " around the text that is important

Building a search for the error message is a skill. But you will get better at it over time if you try.

  • Read multiple pages and answers
    • do not assume that the first answer you encounter is the correct one
  • put in the effort to understand the answer
    • it is possible that you might not understand the answer, that means you might have to do extra web searches based on the answer. Do not stop at this point and jump to (B). Do the research to help you understand the answer, otherwise you will never learn how to do this for yourself.

Choosing the correct answer is a skill. But you will get better at it over time if you try. reference

Experiment with plausible answers

‘their’ answer may not work for you.

You will need to experiment and possibly amend the code that you saw in the answer.

This is a skill, you need to develop over time. reference

Debug and Step Through the code

The above links have additional information and videos on how to use debuggers.

  • Set a breakpoint at the line that throws the error or exception.

Run the test in debug mode.

When the code stops executing at the breakpoint. Check that the application is in the correct state i.e. all elements are ready, you can see the element you want to work with etc.

Then step over the line that throws the execption.

If it passes then chances are you need some synchronisation code immediately prior to your cheap inderal statement to wait for the element to be ready. Because that is what you did when you debugged: you waited, then you executed the code. You want to add the wait into your code.

And make it an application state wait i.e. you checked to see if the element was present, was visible, was in the state you needed. Wait for that in the code.

If that didn’t work then:

  • Set a breakpoint a few lines before the line that throws the error or exception.

Repeat your debug process, carefully watching what is happening. Making sure that the application state is being setup the way that you require it for your code to work, i.e. you click on previous controls, something appears, the data is entered etc.

You are making sure that the previous lines in the test code actually setup the pre-conditions that you need in place for the line of code that is currently throwing an exception.

You might have written something incorrectly prior to the line.

You might need to have more synchronisation, prior to the line.

Are you sure it is working as you expect?

As you debug the code and step through the lines, make sure:

  • the variables have the values you expect
  • none of the variables are null
  • it selected the correct element
  • etc.

You can do this by watching in the variables tab, using the EvaluateExpression functionality in the IDE, System.out.println variables and values to the console etc.

Are you sure that locator is correct?

Manually use the application and copy paste the locators from your code into whatever tool you are using on your computer to check locators:

  • Firebug
  • Developer tools search
  • document.evaluate
  • document.querySelector or document.querySelectorAll

Make sure you copy and paste from the code to get ‘exactly’ the same in the browser as you are using in the code. You might be amazed at how often you can fix typos when transcribing from one source to another (but only when debugging, the reverse is true (inject typos) when adding to your code).

Essentially – find another way of using the code to check if it works.

If you do have to resort to (B)

If you do have to ask someone then make sure:

  • You have tried all of the above so you can tell them what you tried.

That way:

  1. They know you tried
  2. They know what didn’t work
  • Make sure your example is the smallest code example that recreates the problem.

That way the problem is not related to some other part of the code.

By the way – doing this can often help you find the problem.

  • Make your error report detailed

e.g.

  • what line of code throws the error
  • what versions of language, webdriver, browser?
  • intermittent? always?
  • just this app, or every app

By thinking through the environment and situation you might also come up with other ideas of what to try to continue investigating.

If you resort to (B) on the internet

Here are some tips from:

This entry was posted in Java, Selenium Simplified Blog, WebDriver. Bookmark the permalink.

2 Responses to How to investigate and debug a Selenium WebDriver `@Test` error

  1. Pingback: Using Intellij to debug @Test with the ‘Watch’ function » Selenium Simplified

  2. Pingback: Testing Bits – 7/17/16 – 7/23/16 | Testing Curator Blog

Leave a Reply

Your email address will not be published. Required fields are marked *