无论我设置的间隔等待时间如何,Selenium WebDriver都会在60秒时超时

时间:2017-09-29 16:00:25

标签: c# selenium selenium-webdriver phantomjs

因此,我在Web应用程序上使用Selenium Web Driver(使用PhantomJS作为无头浏览器)运行自动UI测试。当我在测试中达到某一点时 - 一个页面,其中Web应用程序需要一分钟才能加载下一页(有时最多3分钟) - 它将失败。

我收到以下错误消息:

  

结果讯息:
  测试方法UI_Tests.UT_GI_Template.Delivery_UI抛出异常:   OpenQA.Selenium.WebDriverException:对远程的HTTP请求   用于URL的WebDriver服务器   http://localhost:45539/session/94ef38f0-a528-11e7-a7fd-69a0e29e333f/element/:wdc:1506697956275/value   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("coffutt@icom.com");
                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();
    ....

测试将始终在下面的步骤超时 - 在下一个按钮上输入会触发一个新页面加载(最多需要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)。为什么我的测试总是在60秒后超时,因为WebDriverWait wait = new WebDriverWait(wd, new TimeSpan(0, 0, 300));的间隔时间设置为300秒?非常感谢任何帮助,谢谢!!

1 个答案:

答案 0 :(得分:4)

经过大量调查(以及大约一百万次谷歌搜索),我已经找到并解决了影响我的自动化Selenium测试的问题。 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个参数/项目。

希望这个解决方案可以帮助别人,欢呼!