I'm using Selenium with Cucumber and also using Page Factory for good framework practices.
As we need to follow the principle of Fast Fail when we get TEST ERRORS/TEST FAILURES
We use Asserts in Selenium framework to handle TEST FAILURES which stops the execution immediately as soon as the TEST FAILURE occurs helping us in knowing the bug.
Here I want to concentrate on TEST ERROR reporting or understanding the implementation of it.
In general, Selenium will throw an exception if element is not found (When element properties had changed and WebDriver not able to find the element to do the operation)
driver.findElement(By.id("test")).click
Above code is enough to get the failure reporting. But If we use some generic methods as a part of the framework then how to handle or report the exceptions caused. Following a piece of code will illustrate my view on how to report the error or fail the test case in such scenarios.
Want to know the best ways to handle the exceptions and also want to know the code to throw the catch exception
//Need Feedback on this if block and catch block
public void clickElement(final By selector)
{
if (selector==null)
{
Assert.fail ("unable to perform click action as the element is null")
}
WebDriverWait wait = new WebDriverWait(driver, 10);
try
{
WebElement we= wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("someid")));
we.click();
}
catch (Exception e)
{
assert.fail("Element not found to click on element" +e.printstacktrace+)
}
}
What part of the code needs to be modified to stop the execution if click doesn't work because of element not found?
public void enterTextInElement(String value, String id)
{
if(id==null)
{
assert.fail("Element is null")
}
try
{
WebElement we= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
we.sendKeys(value);
}
catch (Exception e)
{
assert.fail(e.printstackTrace)
}
}
There are no return statements in both the methods I used, Should there be any?
If so Why we need return statements as Boolean instead of failing the scenarios using assert.fail
Feel free to write your opinions about the best practices based on your experience.
Could find any straight article defining these queries. So It would be lovely and awesome if I can get some good ideas around this.