3
votes

Afternoon,

I've searched but cannot find an answer to an issue I'm having with selenium. I need to find a way to ensure elements are not displayed on a page. Basically, our users have varying levels of permissions, which results in different tabs being displayed.

We've had a few issues recently and incorrect tabs have been displayed for users that shouldn't see them. Now asserting an element is present is no issue at all, but this wont ensure rouge tabs are not present. Any help would be appreciated on this one, as it's proving difficult and I'm sure this isn't that complicated.

We're using Selenium WebDriver and writing the tests in C#.

Thanks,

Stu.

3
Try to click the tab and fail the test if successful or continue if the code catches a selenium exception.Florent B.

3 Answers

2
votes

Use webdriver's findElements method, if the List of WebElements returns size as 0, you can assert for the count of elements.

1
votes

To ensure rouge tabs are not present you can use either of the ExpectedConditions clause from the following :

ExpectedConditions.InvisibilityOfElementLocated can be used for checking that an element is either invisible or not present on the DOM.

Return Value : true if the element is not displayed, otherwise false.

ExpectedConditions.InvisibilityOfElementWithText can be used for checking that an element with text is either invisible or not present on the DOM.

Return Value : true if the element is not displayed, otherwise false.

0
votes

Selenium doesn't have anything built in for this which is slightly annoying, the way I've always handled it is to try to assert that the element is present, and wrap it in a try/catch looking specifically for an element not present exception, so that it fails if it throws any other exception that may be a genuine issue. Within the try you'd need to add something to force a failure if the element is found (any sort of assertion guaranteed to fail).

Example which is confirming that a page header is not shown (Sorry it's in Java but I haven't worked in C# before. It should be simple enough to recreate there)

    try {
        final boolean displayed = this.pageHeader.isDisplayed();
        assertFalse("Page header should not be displayed", displayed);
    } catch (final NoSuchElementException nsee) {
        this.log.debug("Page header is not displayed :PASSED");
    }