Overview

In this post, We will learn how to read an exception message and to fix it.

For Test automation beginners, Exception is the most terrible thing. Mostly what they do when an unexpected outcome of their code happened. inexperienced developers approach experienced developers to fix it or to make them understand what is causing this failure.

Refer below diagram, whenever an exception occurred, the format of exception is in below format. Below image highlights the key information which we should read first, before starting debugging

1 – Exception name starts with “Exception in” line. This exception name gives information type of exception

2- Last 2-3 lines gives information on Class, Method, and line of exception.

Condole error-Automatorsworld
Exception screenshot highlighted with key information in rectangular box’s

Exceptions in Java are typically are of 2 types

1- Checked

2- Unchecked

A lot of literature is there on the internet to understand the definition, therefore, I am not covering it in this post, let’s check some console error messages. Let us understand with an example.

Case 1- When an Exception is in main class-

In below piece of code, I have mentioned the incorrect path of chromedriver exe, let’s check the output

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestClass {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		WebDriver driver ;
		
		// TODO Auto-generated method stub E:\chromedriver_win32
		String exePath = "D:\\chromedriver_win32\\chromedriver.exe";
		System.setProperty("webdriver.chrome.driver", exePath);
		driver=new ChromeDriver();
		//Maximizing  window
		driver.manage().window().maximize();
		driver.get("http://www.google.com");		 
          driver.findElement(By.name("q")).sendKeys("Automatorsworld");
	}
}

The output of the console is- Its throwing illegalStateException whereas if you check the last line of exception, the exact line of error is mentioned with java class name. Class- TestClass.java Line number- 15, now you must be thinking why because the path you have mentioned is in line number 13 so Answer is while creating chromedriver object it found that location as incorrect.

Summary of this exception –

On reading these logs from start to end we came to know that-

1- Type of exception which is always mentioned at the top of error.

2- At the very end, we can find the line number of exception and class in which it is occurring.

Exception in thread “main” java.lang.IllegalStateException: The driver executable does not exist: D:\chromedriver_win32\chromedriver.exe
at com.google.common.base.Preconditions.checkState(Preconditions.java:530)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:136)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:131)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:329)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:124)
at TestClass.main(TestClass.java:15)

Case2- Take another example-

In this example one class is calling another class. Test Class is calling

package TestPackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestClass {
	public static void main(String[] args) {
		// TODO Auto-generated method stub		
		WebDriver driver ;
		WebDriverLanuch obj_WDL = new WebDriverLanuch(); 		
		// TODO Auto-generated method stub E:\chromedriver_win32
		String exePath = "E:\\chromedriver_win32\\chromedriver.exe";
		System.setProperty("webdriver.chrome.driver", exePath);
		driver=new ChromeDriver();
		driver.manage().window().maximize();		
		driver = obj_WDL.launchdriver(driver);
	}
}

WebDriverLaunchClass-

package TestPackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebDriverLanuch {
	
	public WebDriver launchdriver(WebDriver driver)
	{		
		driver.get("ht/www.google.com");
		driver.findElement(By.name("q")).sendKeys("Automatorsworld");
		return driver;
	}
}

Starting ChromeDriver 2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a) on port 25410
Only local connections are allowed.
Sep 23, 2019 8:47:25 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread “main” org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {“code”:-32000,”message”:”Cannot navigate to invalid URL”}
(Session info: chrome=76.0.3809.132)
(Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: ‘3.5.0’, revision: ‘8def36e068’, time: ‘2017-08-10T23:00:22.093Z’
System info: host: ‘DELL’, ip: ‘192.168.56.1’, os.name: ‘Windows 8.1’, os.arch: ‘amd64’, os.version: ‘6.3’, java.version: ‘1.8.0_191’
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false, platform=WIN8_1, acceptSslCerts=false, goog:chromeOptions={debuggerAddress=localhost:52738}, acceptInsecureCerts=false, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a), userDataDir=C:\Users\Prashant\AppData\Local\Temp\scoped_dir7592_17975}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, version=76.0.3809.132, browserConnectionEnabled=false, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}]
Session ID: 547210b6bc77f3ba65c2f0fb047f5f5b
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:368)
at TestPackage.WebDriverLanuch.launchdriver(WebDriverLanuch.java:11)
at TestPackage.TestClass.main(TestClass.java:20)

Understanding the output-

1- Its is mentioning both the class- Class which is causing that exception.

2-The line/method of calling class along with its line of error.

Summary-

Always read the first line of console error, which is basically exception type which you can later search it in google while fixing it.

So, how to search it over google, the most recommended website is stackoverflow where the big community is there to respond. While searching stackoverflow always refer to green ticked response which is approved and accepted solution.

Always refer to green ticked solution

Secondly, Read the last line there you can understand complete error at which class it occurred and the calling class method/line.

Lastly, Read the last line there you can understand complete error at which class it occurred and the calling class method/line.