简体   繁体   中英

How do I assert that an Element/Button is hidden on a page using Selenium C#?

I am trying to validate a scenario that for a specific user some web elements/buttons should be hidden on a page. I have done a quick-n-dirty implementation of a method to verify this and would like to know if there are better ways to do it. Please advice

public void ValidateThatButtonIsHidden(string button)
    {
        IWebElement theButton = null;

        if (button.ToLower().Trim() == "submit an order")
        { theButton = FindElement(By.Id(_elementBtnId1)); }

        else if (button.ToLower().Trim() == "validate order")
        { theButton = FindElement(By.Id(_elementBtnId2)); }


        //Verifying that an element is not visible
        Assert.False(IsELementVisible(theButton));

    }

The idea is that user can call this method and pass the string from his/her test to validate the hidden element.

You can use the Displayed method to check the visibility of an element in a page.

If the element is visible in a page, then theButton.Displayed will return the value as true , else false will be written for the invisible element.

So, you can change your assertion as below

 Assert.IsFalse(button.Displayed);

You can use InvisibilityOfElementLocated method in ExpectedConditions class coupled with ElementExists method. The idea is that if the element exists in the DOM but is still not visible, it must be hidden :

By your_locator = By.Id("foo");
Assert.IsTrue(ExpectedConditions.ElementExists(your_locator) && ExpectedConditions.InvisibilityOfElementLocated(your_locator));

This has been helpful for me. Works not just for checking invisibility but also for enable/not enabled, present/not present etc:

private enum ElementStatus{
        VISIBLITY,
        NOTVISIBLE,
        ENABLED,
        NOTENABLED,
        PRESENCE,
        ABSENT
    }
    private ElementStatus isElementVisible(WebDriver driver, By by,ElementStatus getStatus){
        try{
            if(getStatus.equals(ElementStatus.ENABLED)){
                if(driver.findElement(by).isEnabled())
                    return ElementStatus.ENABLED;
                return ElementStatus.NOTENABLED; 
            }
            if(getStatus.equals(ElementStatus.VISIBLITY)){
                if(driver.findElement(by).isDisplayed())
                    return ElementStatus.VISIBLE;
                return ElementStatus.NOTVISIBLE;
            }
            return ElementStatus.PRESENT;
        }catch(org.openqa.selenium.NoSuchElementException nse){
            return ElementStatus.ABSENT;
        }
    }

I evaluated all solutions suggested here, but was faced with the challenge that (I should have mentioned we have angular pages as well), that sometimes the Elements which were supposed to be hidden were actually absent from DOM . As I was not able to find elements and also I wanted the method to be reusable/scalable for other tests should I ever have to test another hidden button/element. This is what I did, and it is worth mentioning that I am using Specflow to parameterize my tests.

 public bool IsElementPresent(By by)
    {
        try
        {
            driver.FindElement(by);
            return true;
        }
        catch (NoSuchElementException)
        {
            return false;
        }
    }

public void ThatButtonIsHidden(string p0)
    {               

        if (p0.ToLower().Trim() == "submit an order")
        {
        bool isBtnPresent = IsElementPresent(By.Id("btn1Id"));
        Assert.IsFalse(isBtnPresent);                
        }

        else if (p0.ToLower().Trim() == "validate order")
        {
            bool isBtnPresent = IsElementPresent(By.Id("btn2Id"));
            Assert.IsFalse(isBtnPresent);
        }                 


     }

Hope this helps. Works perfectly for me to handle the situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM