3

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.

1 Answer 1

2

I want my tests to fail-fast. A test should never throw an exception. If it does it should break. If one of the first steps fails probably other steps will fail too, generating a lot of unneeded output. You will need to fix the test and run it, so you will find other issues anyways during the fixing.

Fail-fast is a programming concept that translates to automated testing pretty well:

What part of the code need to be modified to stop the execution if click doesnt work because of element not found

You need to remove the try/catch. Only use try/catch if you want to handle to exception yourself. You could also add a Assert.fail('your message') in the catch if you want to give a better message to your errors.

4
  • Want to know how to fail the Test case among below two options //Element is not available and will get No such element execption public void clickButton() { driver.findElement(BY.id("test")).click } //reusable method-to fail for click using assert when element is not available As i have written above --Also edited the code based on your feedback Please refer to code and again and help me out Commented Aug 8, 2017 at 8:11
  • That is personal preference. I would keep the complexity as low as possible. If you can read the exceptions why would you write extra code to make it more clear? If non-coders need to read the error messages, then it would make sense to make them more readable. Your code examples do not use the wait. A wait always works with a .until() see the examples here: seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/… Commented Aug 8, 2017 at 8:40
  • I would not edit questions, but add new smaller specific questions to SQA.SE. Don't be afraid to add a lot of questions. Commented Aug 8, 2017 at 8:41
  • I have made my question specific now and want to how to fail the test case instead of when to fail the test case.Making the question specific about how! Commented Aug 8, 2017 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.