因此,我正在Web应用程序上使用Selenium Web Driver(使用PhantomJS作为无头浏览器)运行自动化UI测试。当我到达测试的某个点时(该页面上的Web应用程序需要一分钟才能加载下一页(有时最多需要3分钟)),它将失败。
我收到以下错误消息:
结果消息:
测试方法UI_Tests.UT_GI_Template.Delivery_UI引发异常:OpenQA.Selenium.WebDriverException:URL到远程WebDriver服务器的HTTP请求,网址为http:// localhost:45539 / session / 94ef38f0-a528-11e7-a7fd-69a0e29e333f / element /:wdc:1506697956275 /值在60秒后超时。---> System.Net.WebException:请求已中止:操作已超时。
我已将等待时间间隔设置为300秒,但它总是在60秒后超时。有没有人遇到这个错误?这是我的代码:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.PhantomJS;
namespace UI_Tests
{
[TestClass]
public class UT_GI_Template {
private RemoteWebDriver wd;
[TestMethod]
public void Delivery_UI()
{
IWebDriver wd = new PhantomJSDriver();
try
{
WebDriverWait wait = new WebDriverWait(wd, new TimeSpan(0, 0, 300));
wd.Navigate().GoToUrl("example.com/QA");
wait.Until(d => (d.FindElements(By.CssSelector("#ctl00_ctl00_BodyContent_emailaddress")).Count != 0));
wd.FindElement(By.CssSelector("#ctl00_ctl00_BodyContent_emailaddress")).Click();
wd.FindElement(By.CssSelector("#ctl00_ctl00_BodyContent_emailaddress")).Clear();
wd.FindElement(By.CssSelector("#ctl00_ctl00_BodyContent_emailaddress")).SendKeys("[email protected]");
wait.Until(d => (d.FindElements(By.CssSelector("#ctl00_ctl00_BodyContent_emailpassword")).Count != 0));
wd.FindElement(By.CssSelector("#ctl00_ctl00_BodyContent_emailpassword")).Click();
wd.FindElement(By.CssSelector("#ctl00_ctl00_BodyContent_emailpassword")).Clear();
....
测试始终会在下面的步骤中超时-在下一个按钮上输入Enter会触发新页面加载(最多需要3分钟)-我已检查,套件中的其余UI测试运行良好,以下步骤已被注释掉。
wait.Until(d => (d.FindElements(By.CssSelector("#ctl00_ctl00_BodyContent_BodyContent_NextButton")).Count != 0));
wd.FindElement(By.CssSelector("#ctl00_ctl00_BodyContent_BodyContent_NextButton")).SendKeys(Keys.Enter);
wait.Until(d => (d.FindElements(By.XPath("//*[@class='grid8 alpha omega']/h1")).Count != 0)); //
string madeIt5 = wd.FindElement(By.XPath("//*[@class='grid8 alpha omega']/h1")).Text;
关于时间间隔,我是否不知道或者我做错了?我在代码中的其他任何地方都没有包含ImplicitWaits,也没有包含任何System.Sleep(s)。为什么将间隔时间设置为300秒时,我的测试总是在60秒后超时WebDriverWait wait = new WebDriverWait(wd, new TimeSpan(0, 0, 300));
?任何帮助将不胜感激,谢谢!
经过大量调查(以及大约一百万次google搜索),我已经找出并解决了影响自动硒测试的问题。60秒的HTTP超时错误来自Selenium RemoteWebDriver的默认设置,源代码中的此行> https://github.com/SeleniumHQ/selenium/blob/9ca04253b7ed337852d50d02c72cb44a13169b71/dotnet/src/webdriver/Remote/RemoteWebDriver.cs# L69
protected static readonly TimeSpan DefaultCommandTimeout = TimeSpan.FromSeconds(60);
更改此值为60秒的唯一方法是在实例化webdriver时执行此操作,在我的情况下,我正在使用PhantomJS,并且代码如下所示>
var timeOutTime = TimeSpan.FromMinutes(4);
var options = new PhantomJSOptions();
options.AddAdditionalCapability("phantomjs.page.settings.resourceTimeout", timeOutTime);
RemoteWebDriver wd = new PhantomJSDriver("C:\\Users\\MyName\\Source\\Workspaces\\ICompany\\Application\\packages\\PhantomJS.2.0.0\\tools\\phantomjs\\", options, timeOutTime);
我的旧测试超时是因为ASP.net __doPostBack按钮,当我选择许多参数/项目时,从我的数据库中获取数据将花费超过60秒的时间,因此将触发HTTP请求中默认的命令超时。 WebDriver源代码。我知道这是问题所在,因为选择了3个以下参数/项后,测试永远不会在回发时超时。
希望此解决方案可以帮助其他人,加油!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句