Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Webdriver - Page Title assert fails before page loads

This issue began when I switched from testing on the www website to my localhost version of it. Working in VS 2012, I will begin debugging so the localhost is active, detach the process so I can test on it, then run any test I like. For a very basic example:

    [Test]
    public void CanGoToHomePage()
    {
        Pages.HomePage.Goto();
        Assert.IsTrue(Pages.HomePage.IsAt());
    }

And the functions it references are here:

    public class HomePage
    {
        const string Url = "http://localhost:3738";
        const string HomepageTitle = "FunnelFire - Home Page";

        public void Goto()
        {
            Browser.Goto(Url);
        }

        public bool IsAt()
        {
            return Browser.Title == HomepageTitle;
        }
    }

And the actual selenium code here:

    public static class Browser
    {
        static IWebDriver webDriver = new FirefoxDriver();

        public static void Goto(string url)
        {
            webDriver.Manage().Window.Maximize();
            webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
            webDriver.Url = url;
        }
    }

Now the issue. The 10 second implicit wait that I added in Browser does successfully wait at most 10 seconds after loading a page to see if it can locate whatever information I want it to find, that is not the problem.

As I said earlier, after I switched to testing on localhost, suddenly I ran into a strange issue where a page would begin to load (i.e. screen still totally white, nothing finished) or even sometimes the next page would JUST barely finish loading and suddenly the test would just up and fail, pointing to the Assert of IsAt returning false even though the page it was loading was the correct one. I could run that test immediately once more and it would pass without a problem. Run it a third time and it could randomly fail again. I'm honestly not sure what is causing the issue and any help would be appreciated!

like image 267
Frank McCormick Avatar asked Mar 24 '23 17:03

Frank McCormick


1 Answers

Implicit waits work only for finding elements. For waiting on the title of the page to be a certain value, you'll want to use an explicit wait. You can write your own version of this pattern, but in the .NET bindings, the WebDriver.Support.dll assembly has a WebDriverWait class to help with this. Its use would look something like this:

// WARNING! Untested code written from memory below. It has not
// been tested or even compiled in an IDE, so may be syntactically
// incorrect. The concept, however, should still be valid. 
public void WaitForTitle(IWebDriver driver, string title, TimeSpan timeout)
{
    WebDriverWait wait = new WebDriverWait(driver, timeout);
    wait.Until((d) => { return d.Title == title; });
}

You could even modify your IsAt method to use this pattern, catching the WebDriverTimeoutException and returning false if the wait function times out.

like image 97
JimEvans Avatar answered Apr 06 '23 09:04

JimEvans