Site icon TestingDocs.com

Common Exceptions in Webdriver Tests

Overview

In this post we will discuss about common exceptions in Firefox WebDriver tests. Also, most exceptions mentioned are with Selenium 3 and Firefox browser .

For more information on Selenium 3 with Firefox Browser, follow this link:

 Selenium 3.0 and Mozilla GeckoDriver 

IllegalStateException: The path to the driver executable must be set

The most common error is if you forget to set the System property for the driver in Firefox tests.

Set the driver path :

System.setProperty(“webdriver.gecko.driver”, “path_to_driver/geckodriver.exe”);

Exception:

Exception in thread “main” java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver.

java.lang.IllegalStateException: The driver executable does not exist

Another common error that we make is to set wrong path or driver missing in the specified path. Also,check the path of geckodriver and the path specified is correct or not. Furthermore, if the path is correct check if the driver exists in the path and with correct name of the exe.

Exception:

FAILED: sampleTest
java.lang.IllegalStateException: The driver executable does not exist: {WRONG PATH}

org.openqa.selenium.NoSuchSessionException

Another interesting error that we sometimes make is access the driver object when access the driver when its closed earlier in the code or by another thread. Also, in selenium 3 we get an easy to understand trace unlike earlier version as shown below:

Exception:

FAILED: sampleTest
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?

We can simulate the error with the following code to understand better.


System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
driver = new FirefoxDriver(); 
driver.navigate().to("https://www.twitter.com");
driver.quit();
driver.get("https://www.twitter.com");

 

ClassNotFoundException: org.testng.TestNG

Sometimes you see this weird exception while running tests from TestNG xml file. Also, this happens when TestNG library jar is not under classpath. Setting proper classpath would solve this kind of exceptions.

Exception in thread “main” java.lang.NoClassDefFoundError: org/testng/TestNG
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG

Cannot find class in classpath

Check your classpath . Also, check for any errors in your project or in pom.xml file. Furthermore, some invalid plugins might also cause this kind of exception.

Sample exception:

org.testng.TestNGException:
Cannot find class in classpath: com.testingdocs.selenium.demo.<<TestClassName>>>
In conclusion, the above exceptions are just a glimpse of some errors. Also in real scenarios , exceptions take some time to debug and bring down our productivity and increase failure cases count. Furthermore, proper debugging and find root cause would solve the problems in a timely manner.

 

More Information on Selenium

https://www.selenium.dev/

Exit mobile version