Webdriver tests failing due to System.Net.WebException timeout

时间:2016-04-21 22:26:56

标签: c# selenium selenium-webdriver nunit

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");

2 个答案:

答案 0 :(得分:1)

非常感谢@Florent和@Buaban,在您的帮助下,我能够找到解决方案。我会把它贴在这里但是给你的Buaban答案,因为我不确定如果没有你的帮助我会很快得到这个答案。

        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++;
            }
        } 

答案 1 :(得分:0)

正如Florent B.在评论中提到的,一些页面资源已经死亡。您必须忽略该异常,然后等待页面上的元素。见下面的例子:

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");