简体   繁体   中英

How can I catch whether the element is not visible

I'm trying to create a script where I can check if the element is or is not displayed on the UWP app. I have a methods that checks if the element is Displayed or Not Displayed..

if the element is actually displayed and I called the "IsElementDisplayed", the test seems to works fine and continue the test execution.. But when the element is really NOT displayed and I called the "IsElementDisplayed", It doesn't return false boolean value.. but it just stops the test execution and says, "no such given parameters found"...

Please check my sample code....

I have a class which contains my locators and returns the instance of WindowsElement:

    protected WindowsElement Id(string id)
    {
        return Driver.FindElementById(id);
    }

    protected WindowsElement XPath(string xpath)
    {
        return Driver.FindElementByXPath(xpath);
    }

    protected WindowsElement Name(string name)
    {
        return Driver.FindElementByName(name);
    }

    protected WindowsElement AccessibilityId(string id)
    {
        return Driver.FindElementByAccessibilityId(id);
    }

and then I have Page class which contains properties of my elements.. sample code below:

    public WindowsElement SaveObligation_Btn => this.AccessibilityId("OBLGTN_saveObligation_btn");

    public WindowsElement CancelObligation_Btn => this.AccessibilityId("OBLGTN_CancelObligation_btn");

    public WindowsElement ObligationAdd_Btn => this.AccessibilityId("SMMRY_AddObligation_btn");

And lastly I have a testhelper class that contains methods such as:

    public bool IsNotDisplayed(WindowsElement element)
    {
        try
        {
            Assert.IsFalse(element.Displayed, $"Element: {element} is not displayed on the page!");
            return false;
        }
        catch (Exception)
        {
            return true;
        }
    }

but when I trying to call the "IsNotDisplayed" method to return false if it caught any exceptions.., My test execution stops and I will have an error that is pointing to my Locator class and says, the "element is NOT found with the given parameters"...

I expect the method "isNotDisplayed" should return false boolean value, so I can validate if the element is visible or not.

I'm not familiar with C#.

But in Python, I will write like this:

try:
    element = //find your element
    if element.is_displayed():
        return True
    else:
        raise AssertionError
except Exception:
    return False

first question is what you want to do when noElementIsDisplayed? Of course, it will catch some exceptions. In your scenario, if you do not want to do anything when exception thrown then you should leave catch block empty. try the following code:

public bool IsNotDisplayed(WindowsElement element)
{
    try
    {
        Assert.IsFalse(element.Displayed, $"Element: {element} is not displayed on the page!");
        return false;
    }
    catch (Exception e)
    {
        // do nothing and it will continue your script.

    }
}

This will return true if the element is displayed else it will return false.

C# Code

public bool IsNotDisplayed(WindowsElement element)
    {
        try
        {
            element.Displayed;

        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

Java Code

public static boolean IsDispayed(WebElement webelement)
{
    try {
        webelement.isDisplayed();
    } catch (Exception e) {
        return false;
    }
    return true;
}

Python Code

try:
    element = driver.find_element_by_xpath('XPATH')
    if element.is_displayed():
        return True
    else:
        raise AssertionError
except Exception:
    return False

Try the below code for c#. This is an alternative..

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

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