C # Webdriver - Assert page name fails before page load

This problem started when I switched from testing on the www website to my version of localhost. While working in VS 2012, I will start debugging so that localhost is active, disconnect the process so that I can test it, and then run any test that I like. For a very simple example:

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

And the functions that it refers to 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 real selenium code is 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 problem. The 10 second implicit wait that I added in the browser successfully waits no more than 10 seconds after the page loads to find out if it can find any information I want to find, this is not a problem.

As I said, after I switched to testing on the local host, suddenly I had a strange problem when the page starts loading (i.e. the screen is still completely white, nothing is finished), or even sometimes the next page is just barely finished and all of a sudden the test will simply go up and fail, indicating an IsAt confirmation returning false, even if the page that it loaded was correct. I could run this test right away again and it would pass without problems. Run it a third time, and it may accidentally fail again. I'm honestly not sure what causes the problem and any help would be appreciated!

+4
source share
1 answer

Implicit expectations only work for finding items. To expect the page title to be a specific value, you will want to use an explicit wait. You can write your own version of this template, but in .NET bindings, the WebDriver.Support.dll assembly has a WebDriverWait class to help with this. Its use will 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 can even change your IsAt method to use this template by catching a WebDriverTimeoutException and returning false if the wait function is disabled.

+6
source

Source: https://habr.com/ru/post/1484269/


All Articles