简体   繁体   English

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

[英]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. 我正在尝试验证针对特定用户的某些Web元素/按钮应该在页面上隐藏的情况。 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. 我已经完成了一种方法的快速n脏实现来验证这一点,并想知道是否有更好的方法来做到这一点。 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. 您可以使用Displayed方法检查页面中元素的可见性。

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. 如果该元素在页面中可见,则theButton.Displayed将返回值为true,否则将为不可见元素写入false。

So, you can change your assertion as below 因此,您可以按以下方式更改断言

 Assert.IsFalse(button.Displayed);

You can use InvisibilityOfElementLocated method in ExpectedConditions class coupled with ElementExists method. 您可以将ExpectedConditions类中的InvisibilityOfElementLocated方法与ElementExists方法一起使用。 The idea is that if the element exists in the DOM but is still not visible, it must be hidden : 这个想法是,如果元素存在于DOM中但仍然不可见,则必须将其隐藏:

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 . 我评估了这里提出的所有解决方案,但是面临着这样的挑战(我应该提到我们也有角度页面),有时DOM实际上缺少本应隐藏的Elements 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. 这就是我所做的,值得一提的是,我正在使用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);
        }                 


     }

Hope this helps. 希望这可以帮助。 Works perfectly for me to handle the situation. 非常适合我处理这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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