Webdriver 测试因 System.Net.WebException 超时而失败
Webdriver tests failing due to System.Net.WebException timeout
C#
.Net 4.5
对比 2013
NUnit 3.2.1
Webdriver 和 Webdriver.Support2.53
所以我的问题是我正在尝试导航到 ebay 的沙箱登录页面并登录。这看起来很简单,但在给我一个 System.Net.WebException
超时错误之前,我正在努力让页面完全加载。
这是 link 我想去的
https://signin.sandbox.ebay.com/
这就是我的代码执行此操作的样子。
var EbaySandboxPage = new EbaySandboxLoginPageModel(Driver);
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(200));
Driver.Navigate().GoToUrl(EbaySandboxLoginPageModel.sandboxUrl);
这是每次我在 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
所以我的问题是如何更改 System.Net.WebRequest.Timeout
属性?我没有使用 webrequest 的实例。我猜 webdriver 是,但我想我有办法改变这个值。如您所见,我已经将 SetPageLoadTimeout()
值提高到超过 2 分钟。在我的手动测试中,这已经足够了。
这是我对@Buaban 的解决方案的尝试,尽管我的仍然抛出异常。
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(120));
try
{
Driver.Navigate().GoToUrl(EbaySandboxLoginPageModel.sandboxUrl);
}
catch (WebDriverException)
{
}
EbaySandboxPage.WaitForElementVisible(Driver, EbaySandboxLoginPageModel.usernameFieldSelector);
这是 WaitForElementVisible()
方法的样子。
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 ");
}
}
这里是usernameFieldSelector
的定义
public static By usernameFieldSelector = By.CssSelector("#userid");
正如 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");
非常感谢您的@Florent 和@Buaban,在您的帮助下我找到了解决方案。我会 post 它在这里,但将答案授予你的 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++;
}
}
C#
.Net 4.5
对比 2013
NUnit 3.2.1
Webdriver 和 Webdriver.Support2.53
所以我的问题是我正在尝试导航到 ebay 的沙箱登录页面并登录。这看起来很简单,但在给我一个 System.Net.WebException
超时错误之前,我正在努力让页面完全加载。
这是 link 我想去的 https://signin.sandbox.ebay.com/
这就是我的代码执行此操作的样子。
var EbaySandboxPage = new EbaySandboxLoginPageModel(Driver);
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(200));
Driver.Navigate().GoToUrl(EbaySandboxLoginPageModel.sandboxUrl);
这是每次我在 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
所以我的问题是如何更改 System.Net.WebRequest.Timeout
属性?我没有使用 webrequest 的实例。我猜 webdriver 是,但我想我有办法改变这个值。如您所见,我已经将 SetPageLoadTimeout()
值提高到超过 2 分钟。在我的手动测试中,这已经足够了。
这是我对@Buaban 的解决方案的尝试,尽管我的仍然抛出异常。
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(120));
try
{
Driver.Navigate().GoToUrl(EbaySandboxLoginPageModel.sandboxUrl);
}
catch (WebDriverException)
{
}
EbaySandboxPage.WaitForElementVisible(Driver, EbaySandboxLoginPageModel.usernameFieldSelector);
这是 WaitForElementVisible()
方法的样子。
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 ");
}
}
这里是usernameFieldSelector
public static By usernameFieldSelector = By.CssSelector("#userid");
正如 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");
非常感谢您的@Florent 和@Buaban,在您的帮助下我找到了解决方案。我会 post 它在这里,但将答案授予你的 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++;
}
}