Home > Back-end >  Custom IsDisplayed extension returns true when it should be false
Custom IsDisplayed extension returns true when it should be false

Time:10-18

I'm doing some self dev in Automation, I've written an extension method to assert that an element is displayed which also includes a wait.

There are no errors displayed in the code, it builds and runs 'fine', and it's applied successfully from my Extensions class to my page object and then to the step definition.

I wanted to check that it was passing / failing correctly, so I've passed in the wrong password on a login test in order to confirm it fails, but it's passing as though it's logged in successfully. I've double checked that the element I'm using is only there once logged in, so that leaves me with the extension method being a possible issue.

Does this look right?

My Extension (in Extensions class):

public static bool IsDisplayed(this IWebDriver driver, By by, int timeoutInSeconds = 10)
        {
            try
            {
                return driver.FindElement(by, timeoutInSeconds).Displayed;
            }
            catch (Exception)
            {
                return false;
            }
        }

Application to element in Page Object:

public void HomePageIsDisplayed()
        {
            _driver.IsDisplayed(TrelloHomeLogo);
        }

Application in Step Definitions:

[Then(@"it logs in sucessfully")]
        public void ThenItLogsInSucessfully()
        {
            _homePage.HomePageIsDisplayed();
        }

TIA

CodePudding user response:

Found the answer to this so thought I'd answer it myself for anyone else who might have the same issue...

As I was trying to assert that something was displayed, I needed my method to return something but it couldn't as it was a void and not a bool (facepalmed myself for not seeing this).

The extension is fine, but changes to the page object and step definition are as follows...

Page object:

public bool HomePageIsDisplayed()
        {
            return _driver.IsDisplayed(TrelloHomeLogo);
        }

Step definition:

[Then(@"it logs in sucessfully")]
        public void ThenItLogsInSucessfully()
        {
            Assert.True(_homePage.HomePageIsDisplayed(), "Home page should be displayed");
        }

That string in the step definition is optional but I've added it for additional info if the test fails, so I know exactly what's failed in the collection of tests.

  • Related