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

C. *_*utt 3 c# selenium phantomjs selenium-webdriver

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

我收到以下错误消息:

结果消息:
测试方法 UI_Tests.UT_GI_Template.Delivery_UI 抛出异常:OpenQA.Selenium.WebDriverException:对 URL http://localhost:45539/session/94ef38f0-a528-11e7-a7fd-69a0e29e333f的远程 WebDriver 服务器的 HTTP 请求 /: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("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();
    ....
Run Code Online (Sandbox Code Playgroud)

测试将始终在下面的步骤超时——在下一步按钮上输入会触发一个新页面加载(最多需要 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;
Run Code Online (Sandbox Code Playgroud)

有什么我不知道的时间间隔或我做错了吗?我在代码的其他任何地方都没有包含 ImplicitWaits,也没有包含任何 System.Sleep(s)。当我将间隔时间设置为 300 秒时,为什么我的测试总是在 60 秒后超时WebDriverWait wait = new WebDriverWait(wd, new TimeSpan(0, 0, 300));?任何帮助将不胜感激,谢谢!!

C. *_*utt 5

经过大量调查(以及大约一百万次谷歌搜索),我已经找出并解决了影响我的自动化 Selenium 测试的问题。60 秒 HTTP 超时错误来自 Selenium RemoteWebDriver 的默认设置,源代码中的这一行 > https://github.com/SeleniumHQ/selenium/blob/9ca04253b7ed337852d50d02c72cb44a13169b71/dotnet/src/webDrivercRemRem L69

protected static readonly TimeSpan DefaultCommandTimeout = TimeSpan.FromSeconds(60);
Run Code Online (Sandbox Code Playgroud)

更改此 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);
Run Code Online (Sandbox Code Playgroud)

我的旧测试超时,因为 ASP.net __doPostBack 按钮在我选择大量参数/项目时从我的数据库中获取数据需要超过 60 秒的时间,因此会触发 HTTP 请求的默认命令超时WebDriver 源代码。我知道这是问题所在,因为如果选择了 3 个以下的参数/项目,测试永远不会在回发时超时。

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