1

I'm new to Webdriver and I'm wondering how do I make my test case fail if an element is present. Using the following code I can send an output that if the element id errorMessages is present an output will be displayed. Is there anyway I can make the test fail also?

if (driver.findElements(By.id("errorMessages")).size() != 0) {
    System.out.println("Error..Warning Subscriber already exists ");
}
2
  • Are you using JUnit?
    – Saifur
    Dec 16, 2014 at 14:39
  • I' currently using TestNG
    – TrevDroid
    Dec 16, 2014 at 15:12

2 Answers 2

1

With TestNG you can simply use Assert.fail("your message"); inside the if block to mark the test as failed. Something like:

if (driver.findElements(By.id("errorMessages")).size() != 0) {
   Assert.fail("Error..Warning Subscriber already exists ");    
}

See this for overloading

1

I think you are looking at it in the wrong way. Rather than checking if the size is what you expect and then forcing the test to fail, use the inbuilt assertion methods so it reads much more like a logical statement:

assertTrue(driver.findElements(By.id("errorMessages")).size() == 0, "Error..Warning Subscriber already exists");

Documentation from the TestNG site

The inbuilt assertion methods will then print out that message if that assertion fails.

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.