user2150250
user2150250

Reputation: 5167

Asserting Element Not Present Using ExpectedConditions Class

I'm trying to get into the practice of utilizing the ExpectedConditions class to do my Selenium related assertions, but I'm having trouble finding the de facto best practice for asserting a given element is not present. I'm trying to use something like this ...

Assert.assertFalse(ExpectedConditions.presenceOfElementLocated(By.xpath(myXpath)).apply(driver1));

But this won't compile of course because presenceOfElementLocated returns a WebElement instead of a boolean, which I'm finding frustrating. What do you recommend as the best practice for accomplishing what I'm going for?

EDIT: The accepted answer to the question converted to Java ...

public static boolean elementXpathPresent(WebDriver driver,String elementXpath) {
    try {
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(elementXpath)));
    } catch (TimeoutException e) {
        return false;
    }   
    return true;
}

Upvotes: 3

Views: 2533

Answers (3)

stiemannkj1
stiemannkj1

Reputation: 4549

The accepted answer is a good way to do this (and it's the way I prefer to make assertions with Selenium). But you could also use ExpectedConditions.not() with Assert.true() to assert that elements aren't present:

Assert.assertTrue(
    ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(myXpath))).apply(driver1));

Upvotes: 0

Louis
Louis

Reputation: 151401

The easiest way to check that an element is not present is to use the plural form of the findElement function (i.e. findElements) and check the length of the returned list of element. If the element is not present, then the list will be of size 0.

Assert.assertTrue(driver.findElements(By.xpath(myXpath)).size() == 0);

Whether or not it makes sense to use an explicit wait for this test really depends on your application. Most of the time when I am checking for the absence of an element, I have an earlier tests that uses an explicit wait. For instance, if I want to check that a change to a search table empties the table, I first check that the table has finished refreshing and then check that it is empty. The former check needs an explicit wait, the latter check does not.

Upvotes: 2

Saifur
Saifur

Reputation: 16201

Well, using ExpectedConditions will eventually throw NoSuchElementException if there is no element matching the selector present. In that case you probably want to use try catch with ExpectedConditions or simply use findElements() and size() to see if it finds any elements at all. See my answer here

Here is another option for you. Written in C# but will be fairly easy to convert

/// <summary>
/// 
/// </summary>
/// <returns></returns>
public bool Test()
{
    try
    {
        new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(
            ExpectedConditions.ElementExists(By.XPath("MyXpath")));
    }
    catch (NoSuchElementException)
    {
        return false;
    }

    return true;

}

/// <summary>
/// 
/// </summary>
public void Assertion()
{
    Assert.IsTrue(Test());
}

Upvotes: 2

Related Questions