Rishu Saxena
Rishu Saxena

Reputation: 169

Validating absence of an element

I have a test case:

Assert.assertTrue(test .verifyNoDrillDisplayForCourses()); 

and a boolean method verifyNoDrillDisplayForCourses which verifies element("xyz") is not displayed,

    try{
         if(element("xyz"). isDisplayed())
         return false;

     else return true;
     }
     catch (Exception e) 
     {

        return false;
     }
   }

But the assertion fails as java .lang .AssertionError:expected [true] but found [false]. I am unable to figure out why?

Upvotes: 1

Views: 180

Answers (3)

Rishu Saxena
Rishu Saxena

Reputation: 169

Following code helped:

 public boolean verifyNoelement()

 {

 try


{
           if(element("element").isDisplayed())
           {
           return false;
           }
           return false;
        }
        catch(Exception e)
        {
             logMessage("No element displayed");
            return true;
        }


    }

Upvotes: 0

user3864935
user3864935

Reputation:

If you're testing for the presence of an element, if it's not found an exception will be thrown. So if you find it you're returning false, if you can't find it you're also returning false.

When testing for non-presence of an element you should have the catch block return true!

    try{
       if(element("xyz").isDisplayed()) {
             return false;
       } else return true;
    }
    catch (Exception e) 
    {

    return false;
    }
    }

I believe your if statement is missing correct formatting from what you copied over.

I've amended it above, but in case try it like this:

if(element("xyz").isDisplayed()) {
         return false;
   } else return true;

Upvotes: 1

peetya
peetya

Reputation: 3628

The isDisplayed() method will throw an StaleElementReferenceException, if the given element is not in the DOM anymore. So you have to change the catch statement to return true;.

Upvotes: 2

Related Questions