Sometimes, automated tests fail.
The following test fails because it cannot locate one of its elements:
@Test
public void searchReturnsResultsTest() {
driver.get(HOME_PAGE_URL);
Assert.assertEquals(driver.getCurrentUrl(), HOME_PAGE_URL);
WebElement searchBox = driver.findElement(By.id("edit-keyword1"));
searchBox.sendKeys("java");
WebElement searchButton = driver.findElement(By.id("edit-submit"));
searchButton.click();
Assert.assertTrue(driver.getCurrentUrl().contains(RESULTS_PAGE_URL));
}
The test fails since it cannot locate the search box because the id of the search box is not correct.
It displays in the console the exception and the stack trace information:
FAILED: tests.NoTryCatchInTests.searchReturnsResultsTest
org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element: {"method":"css selector","selector":"#edit\-keyword1"}
(Session info: chrome=120.0.6099.71)
For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception
Build info: version: '4.12.1', revision: '8e34639b11'
System info: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.7'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [a347f5d9cb73dcf65d5abf0ddd0389cc, findElement {using=id, value=edit-keyword1}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 120.0.6099.71, chrome: {chromedriverVersion: 120.0.6099.71 (9729082fe617..., userDataDir: C:\Users\alexs\AppData\Loca...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:35137}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: windows, proxy: Proxy(), se:cdp: ws://localhost:35137/devtoo..., se:cdpVersion: 120.0.6099.71, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Sometimes, it is useful to collect the exception and stack trace for all tests so that they can be reviewed later.
One of the ways of doing this is by wrapping the whole test in a try statement and saving the exception and stack trace to a log in the catch clause as follows:
@Test
public void searchReturnsResultsTest() {
try {
driver.get(HOME_PAGE_URL);
Assert.assertEquals(driver.getCurrentUrl(), HOME_PAGE_URL);
WebElement searchBox = driver.findElement(By.id("edit-keyword1"));
searchBox.sendKeys("java");
WebElement searchButton = driver.findElement(By.id("edit-submit"));
searchButton.click();
Assert.assertTrue(driver.getCurrentUrl().contains(RESULTS_PAGE_URL));
}
catch (Exception e) {
addToLog("searchReturnsResults", e);
Assert.fail();
}
}
Notice that the catch clause does more than just saving exception/stack trace info to the log. It also fails the test. Without the second line, the test would pass even if it cannot find the search box element.
Wrapping the whole test in a try/catch statement is wrong and you should avoid doing it.
Keep reading with a 7-day free trial
Subscribe to Selenium For Beginners to keep reading this post and get 7 days of free access to the full post archives.