如何使用Selenium C#断言页面上隐藏了元素/按钮?

如何使用Selenium C#断言页面上隐藏了元素/按钮?,c#,.net,selenium,selenium-webdriver,C#,.net,Selenium,Selenium Webdriver,我试图验证一个场景,即对于特定用户,某些web元素/按钮应该隐藏在页面上。我已经完成了一个方法的quick-n-dirty实现来验证这一点,我想知道是否有更好的方法来验证这一点。请指教 public void ValidateThatButtonIsHidden(string button) { IWebElement theButton = null; if (button.ToLower().Trim() == "submit an order")

我试图验证一个场景,即对于特定用户,某些web元素/按钮应该隐藏在页面上。我已经完成了一个方法的quick-n-dirty实现来验证这一点,我想知道是否有更好的方法来验证这一点。请指教

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));

    }

其思想是用户可以调用此方法并通过他/她的测试中的字符串来验证隐藏元素。

您可以使用
显示的
方法来检查页面中元素的可见性

如果元素在页面中可见,则显示的按钮将返回值为true,否则不可见元素将写入false

因此,您可以如下更改您的断言

 Assert.IsFalse(button.Displayed);

您可以使用
displated
方法检查页面中元素的可见性

如果元素在页面中可见,则显示的按钮将返回值为true,否则不可见元素将写入false

因此,您可以如下更改您的断言

 Assert.IsFalse(button.Displayed);

您可以在
ExpectedConditions
类中与
ElementExists
方法结合使用
InvisibilityOfElementLocated
方法。其思想是,如果元素存在于DOM中但仍然不可见,则必须将其隐藏:

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

您可以在
ExpectedConditions
类中与
ElementExists
方法结合使用
InvisibilityOfElementLocated
方法。其思想是,如果元素存在于DOM中但仍然不可见,则必须将其隐藏:

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

这对我很有帮助。不仅适用于检查不可见性,还适用于启用/未启用、存在/不存在等:

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;
        }
    }

这对我很有帮助。不仅适用于检查不可见性,还适用于启用/未启用、存在/不存在等:

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;
        }
    }

我评估了这里建议的所有解决方案,但面临的挑战是(我应该提到我们也有角度页面),有时应该隐藏的元素实际上在DOM中不存在。 因为我无法找到元素,而且我希望该方法能够在其他测试中重用/可伸缩,如果我必须测试另一个隐藏的按钮/元素的话。这就是我所做的,值得一提的是,我正在使用Specflow来参数化我的测试

 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);
        }                 


     }

希望这有帮助。这对我处理这种情况非常有效

我评估了这里建议的所有解决方案,但面临的挑战是(我应该提到我们也有角度页面),有时应该隐藏的元素实际上在DOM中不存在。 因为我无法找到元素,而且我希望该方法能够在其他测试中重用/可伸缩,如果我必须测试另一个隐藏的按钮/元素的话。这就是我所做的,值得一提的是,我正在使用Specflow来参数化我的测试

 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);
        }                 


     }

希望这有帮助。这对我处理这种情况非常有效

InvisibilityOfElementLocated将检查元素在DOM上是否不可见或不存在。所以,这并不意味着元素只是隐藏的。InvisibilityOfElementLocated将检查元素在DOM上是否不可见或不存在。所以,这并不意味着元素只是隐藏的。所以,如果按钮被显示,那么这个断言将失败?是的。如果按钮被显示,那么按钮。显示的值将为真。这里我们使用Assert.IsFalse进行验证。那么,它将失败。那么,如果显示按钮,那么此断言将失败?是的。如果显示按钮,那么按钮。显示的值将为真。这里我们使用Assert.IsFalse进行验证。因此,它将失败。这一点看起来更稳健,更适合处理各种结果。不过,我需要一点帮助来彻底理解这一点,如下所述--“private ElementStatus isElementVisible(WebDriver-driver,By,ElementStatus-getStatus)”我是否应该通过Id/CssSelector/Xpath等向她声明我是否正在查找元素?这一点看起来更健壮,更适合处理各种结果。不过,我需要一些帮助来彻底理解这一点,如下所述--“private ElementStatus isElementVisible(WebDriver-driver,By,ElementStatus-getStatus)”我是否应该通过Id/CssSelector/Xpath等来声明我是否正在查找元素?