Difference between findElement and findElements
Reporting and elements may contain errors that are not detected with FIND_ELEMENT_FAILURE: No element found
.
To create this, you can use a simple scenario that opens the perfecto.io site and searches for an element that is not there. In the first case, the findElements
method is used; in the other case, the findElement
is used. Continue to the end of the article to find out the results.
findElements method
findElement sample
reportiumClient.testStart("FindElements", new TestContext("NatoTest", "findElements"));
driver.get("perfecto.io");
List<WebElement> elements1 = driver.findElementsByXPath("//*[@class=\"gLFyfg\"]");
reportiumClient.testStop(TestResultFactory.createSuccess());
The following image shows the result in Perfecto Smart Reporting:
The following image shows the error itself. Although the expected result has not been found, the test is marked as successful.
findElement method
findElement sample
reportiumClient.testStart("FindElement", new TestContext("NatoTest", "findElement"));
driver.get("perfecto.io");
WebElement element1 = driver.findElementByXPath("//*[@class=\"gLFyfg\"]");
reportiumClient.testStop(TestResultFactory.createSuccess());
The following image shows the result in Perfecto Smart Reporting:
The following image shows the error itself. The expected result is not found AND the test is marked as failed.
Why is this happening?
As defined by the Selenium WebDriver API, the method findElement()
should be used when the element is expected to exist. If the element doesn’t exist, it is an error and the command is marked as failed. The method findElements()
should be used for checking whether one or more elements that fit the find criteria exist. If no element is found, which is a valid situation, an empty list is returned and the command is marked as successful.