Buster
Buster

Reputation: 715

Webdriver tests failing due to System.Net.WebException timeout

C#

.Net 4.5

VS 2013

NUnit 3.2.1

Webdriver and Webdriver.Support 2.53

So my issue is I am trying navigating to ebay's sandbox login page and login. This seems simple enough but I am struggling to get the page to fully load before giving me a System.Net.WebException timeout error.

Here is the link I am trying to go to https://signin.sandbox.ebay.com/

And Here is what my code looks like that is doing this.

var EbaySandboxPage = new EbaySandboxLoginPageModel(Driver);
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(200));
Driver.Navigate().GoToUrl(EbaySandboxLoginPageModel.sandboxUrl);

And here is the exception that is getting thrown every time I try this in Firefox.

Test Name:  VerifyItemsSold
Test FullName: POMAuctivaTest.TestSuite.PostSaleTestSuite<FirefoxDriver>.VerifyItemsSold
Test Source:    c:\git\POMAuctivaTest\POMAuctivaTest.TestSuite\PostSaleTestSuite.cs : line 204
Test Outcome:   Failed
Test Duration:  0:00:00.0000001

Result Message: 
OneTimeSetUp: OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:7055/hub/session/80efbcbe-841d-4a53-a422-5e7498a0438b/element timed out after 60 seconds.
----> System.Net.WebException : The operation has timed out

So my question is how to I change the System.Net.WebRequest.Timeout property? I am not using an instance of webrequest. I guess webdriver is but I would imagine there is a way for me to change this value. As you can see I have already upped the SetPageLoadTimeout() value to exceed 2 min. Which in my manual testing has been more than enough.

Here was my attempt at @Buaban's solution although mine was still throwing the exception.

        Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(120));
        try
        {
            Driver.Navigate().GoToUrl(EbaySandboxLoginPageModel.sandboxUrl);
        }
        catch (WebDriverException)
        {

        }
        EbaySandboxPage.WaitForElementVisible(Driver, EbaySandboxLoginPageModel.usernameFieldSelector); 

Here is what the WaitForElementVisible() method looks like.

    public void WaitForElementVisible(IWebDriver driver, By element)
    {
        try
        {
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(90));
            wait.Until(ExpectedConditions.ElementIsVisible(element));
        }
        catch (WebDriverTimeoutException)
        {
            TakeScreenshot(Driver);
            Console.Write("Test failed trying to wait for this element " + element.ToString() + " to be visible ");
        }
    }

Here is the definition of the usernameFieldSelector

public static By usernameFieldSelector = By.CssSelector("#userid");

Upvotes: 0

Views: 1044

Answers (2)

Buster
Buster

Reputation: 715

So thank your @Florent and @Buaban, with your help I was able to figure out a solution to this. Ill post it here but award the answer to your Buaban as I am not sure I would have been able to get to this as quickly without your help.

        Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(30));
        try
        {
            Driver.Navigate().GoToUrl(@"https://signin.sandbox.ebay.com/");
        }
        catch (Exception)
        {
            System.Diagnostics.Debug.WriteLine("Some resources are dead!");
        }
        var attempts = 0;
        while (attempts < 2)
        {
            try
            {
                IWait<IWebDriver> wait = new DefaultWait<IWebDriver>(Driver);
                wait.Timeout = TimeSpan.FromSeconds(20);
                wait.PollingInterval = TimeSpan.FromMilliseconds(300);
                wait.Until(d => d.FindElements(By.XPath("//span[text()='SIGN IN']")).Count > 0);
                break;
            }
            catch (WebDriverException)
            {
                attempts++;
            }
        } 

Upvotes: 1

Buaban
Buaban

Reputation: 5137

As Florent B. mentioned in comment, some page resources are dead. You have to ignore the exception then wait for an element on the page. See example below:

Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(30));
try
{
    Driver.Navigate().GoToUrl(@"https://signin.sandbox.ebay.com/");
}
catch (Exception)
{
    System.Diagnostics.Debug.WriteLine("Some resources are dead!");
}
IWait<IWebDriver> wait = new DefaultWait<IWebDriver>(Driver);
wait.Timeout = TimeSpan.FromSeconds(10);
wait.PollingInterval = TimeSpan.FromMilliseconds(300);
wait.Until(d => d.FindElements(By.XPath("//span[text()='SIGN IN']")).Count > 0);

System.Diagnostics.Debug.WriteLine("SIGN IN textbox is loaded");

Upvotes: 0

Related Questions