jeudi 3 octobre 2019

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.

Aucun commentaire:

Enregistrer un commentaire