0

I use Assert class to check if some text is on the page, this command stop test executing if text is not present. I want use verification. Could someone tell where I could find documentation on such methods?

I mean WebDriver, junit. For example such code

String text=("Terms");
 List<WebElement> list = driver.findElements(By.xpath("//*[contains(text(),'" + text + "')]"));
 Assert.assertEquals("Text not found!", "Terms", list);

If there isn't text "Term" on page junit test will interrupt test, but I need just take error message and continue test.

khris
  • 4,261
  • 15
  • 57
  • 88
  • What does it mean, exactly? I know several meanings of the word "verification" in the context of software testing. What is exactly what you want to do? – Petr Janeček Jul 13 '12 at 15:24
  • Updated my answer based on the major edit to your question. Hope this helps. – Will Jul 13 '12 at 15:49
  • `Assert.assertEquals("Text not found!", "Terms", list);` always fails but that's another problem :) – Petr Janeček Jul 13 '12 at 16:13

2 Answers2

0

Sounds like you just need to catch and handle AssertionError thrown by the various methods of Assert (documented here). Something like this:

try {
  Assert.assertEquals("Text not found!", "Terms", list);
} catch (AssertionError e) {
  System.err.println("Assertion failed: " + e.getMessage());
}
Will
  • 3,506
  • 2
  • 21
  • 35
  • Here "Selenium" means Selenium IDE. With Selenium RC or Selenium WebDriver, the JUnit's or TestNG's asserting is usually used. – Petr Janeček Jul 13 '12 at 15:35
  • 1
    A simple `if` statement would be enough since you [shouldn't use try-catch for flow control](http://stackoverflow.com/questions/729379/why-not-use-exceptions-as-regular-flow-of-control). But yeah, this is pretty much it. – Petr Janeček Jul 13 '12 at 16:16
0

If you want to continue execution of your test cases even if some some result you are expecting fails and get to see the results at the end of complete execution.

You can do something like this -

Declare a variable to store all the test case results which fail during execution and then in the tearDown method you can call Assert.fail(String message)

 StringBuffer errors = new StringBuffer();

 @Test
 public void testSomething(){
    if(!"data".equals(text)){
      addErrors(text +"not equal to data");
    }

    // add any number of if statements to check anything else 
 }

 @After()
 public void tearDown(){
   if(errors.length()!=0){         
     Assert.fail(errors.toString());
   } 
 }

 public String addErrors(String message){
   errors = errors.append(message+"\n");
 }

Now in the testSomething() method you can check or test any number of WebElements on the webpage and all you have to do is have a simple if statement to check if some thing is correct and if not then call the addErrors() method. Hope this helps you.

Hari Reddy
  • 3,708
  • 4
  • 28
  • 40