1

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.

Kulin
  • 11
  • 1
  • 2

4 Answers4

0

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
Yun
  • 1,032
  • 7
  • 20
0

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.

    }
}
kishor sharma
  • 179
  • 2
  • 17
  • Your catch will only work if your function returns void. Your function expects a boolean to be returned. – PixelPlex Oct 09 '19 at 13:38
-1

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
Yosuva Arulanthu
  • 1,444
  • 4
  • 18
  • 32
  • -1. You should return `webelement.isDisplayed();` because if the element is hidden no exception will be thrown but the element is not displayed. – AndiCover Oct 04 '19 at 09:23
  • @AndiCover Thanks for the comment. I have updated my code to return ``webelement.isDisplayed();`` . Please check and let me know – Yosuva Arulanthu Oct 06 '19 at 16:55
  • Better but still not optimal. Just return in the try block and remove the then unreachable return statement after the catchblock. In your example the isDisplayed method would be called twice in the successful case. – AndiCover Oct 06 '19 at 20:09
  • @AndiCover Method has return type as boolean so i can't remove the last return statement – Yosuva Arulanthu Oct 06 '19 at 20:23
-1

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;
       }
   }
Sammar Ahmad
  • 236
  • 5
  • 16
  • this answer shows how to check whether an element exists, but it does not provide anything helpful regarding whether an _existing_ element is displayed or not... – emfi Oct 30 '20 at 13:47