Skip to main content
blog title image

7 minute read - Selenium WebDriver FAQs Test Automation Tips

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

Jul 20, 2016

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. Here are some tips.

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.

Initial Setup Errors

If you are just starting, and you are setting up the basic infrastructure from a blog post then it might be out of date, or something may have changed. Getting started is the hardest part of learning to automate.

When things go wrong, drop down to the command line and make sure that the basics work:

  • mvn -v
  • javac -version

In your project folder make sure that “mvn clean” works without error.

NOTE: Make sure you are running the mvn clean command in the same folder that contains the pom.xml

Then try a mvn compile and make sure you don’t get any download errors reported.

Many of the recent questions I receive relate to simple maven configuration issues.

I have created 2 new videos, and a hints and tips lecture, which show some troubleshooting steps that you can take if you encounter maven problems.

You can also read the tips.

If you are working in an IDE, then check that everything works from the command line first. And try closing and restarting your IDE (this often works like magic).

Maven Not Downloading Stuff?

Did you run “mvn clean” amd get download issues?

I visited the url in the repo through the browser and received a 404, which initially made me think that the file was missing, but in reality I had to amend the settings.xml and add a proxy.

The settings.xml is in maven install directory

<proxy>
   <id>myproxy</id
   <active>true</active>
   <protocol>http</protocol>
   <host>123.123.123.123</host>
   <port>80</port>
   <nonProxyHosts></nonProxyHosts>
</proxy>

Note I just made up the host IP

If you need to you can add a username and password in there as well. But try without it first.

    <username>proxyuser</username>
    <password>proxypass</password>

There is a useful post on StackOverflow that describes the maven proxy fix:

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 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.

Have You Tried Resetting the focus?

Sometimes, with Actions in particular, you might have to set the focus to some other element on the screen, or get the mouse out of the way.

driver.findElement(By.tagName("html")).click();

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:

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