Issue
My main()
function contains several sub-functions testing several features / scenarios etc each.
There are several cases of errors that may be found during the test running.
In each of these cases there is no reason to continue running the test so I'm sending a report email and closing the program with driver.close();
or driver.quit();
command.
The browser is closed however the code still trying to run so as the first command in the following sub-function still trying to do things: navigate somewhere, find objects etc but since the browser is already closed Exception is thrown as following:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
or
Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: The FirefoxDriver cannot be used after quit() was called.
So how can I tell my program to stop running and close / quit the browser properly?
Solution
Use a try/finally instead of a try/catch.
You don't want to catch the exception and then close the driver, you just want to close the driver at the end of the test, wherever that may be.
And example test method might look like:
public void exampleMethod() {
WebDriver driver = new FirefoxDriver();
try {
//Steps in your test
}
finally {
driver.quit();
}
}
Answered By - aholt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.