Webdriver C#如何在多个浏览器中设置驱动程序超时



为了在这里找到答案,我花了半个星期的时间试图自己解决这个问题,但我无法解决我的问题。有一点背景,我是C#和NUnit网络驱动程序测试的新手,我正试图为我工作的公司创建一套回归软件。我们有一些产品与ebay高度集成,因此作为测试的一部分,我需要点击一个按钮,进入ebay登录页面,然后我需要登录。看起来很简单(或者我是这么认为的)。我遇到的问题是,当点击这个ebay登录页面时,每次FF都会超时。我目前有在多个浏览器中运行的测试设置,chrome和IE正在通过(它们也有点挂起),但FF从未通过。

这对我来说很奇怪,因为在这个测试的早些时候,我去了ebay并成功登录。但当我必须将我的公司账户链接到ebay账户时,这个登录页面需要很长时间。我知道我们会向ebay传递一些独特的令牌来链接帐户,我认为这是导致加载时间过长的原因。

因此,FF的故障总是一样的,在60秒后超时。我读过其他类似的问题(Selenium WebDriver偶尔会抛出Timeout异常)。有一些解决方案,我感兴趣的是将驱动程序超时设置为大于60秒。我不知道如何用我正在进行的多浏览器设置来做到这一点。

[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(ChromeDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
public class UnitTest1<TWebDriver> where TWebDriver: IWebDriver, new()
{
     PTGeneral General;
    [TestFixtureSetUp]
    public void SetUp()
    {
        General = new PTGeneral();
        General.Driver = new TWebDriver();
    }

我真的很想保留这个设置,因为我喜欢如何测试IE、FF和Chrome,但我不知道如何实现这样的解决方案。

 new FirefoxDriver("FfBinaryPath", FfProfileInstance, TimeSpan.FromSeconds(180));

如果您想要更多信息,我们将非常感谢您的任何帮助。如果我理解您的要求,我们将很乐意提供;)

感谢大家的阅读,这个社区太棒了。

还在为这个而挣扎。如果有帮助的话,下面是错误消息。

------ Run test started ------
NUnit VS Adapter 2.0.0.0 executing tests is started
Loading tests from C:Usersjburnsdocumentsvisual studio 2013ProjectsPTTestPTTestbinDebugPTTest.dll
Run started: C:Usersjburnsdocumentsvisual studio 2013ProjectsPTTestPTTestbinDebugPTTest.dll
TearDown failed for test fixture PTTest.UnitTest1<ChromeDriver>
The HTTP request to the remote WebDriver server for URL http://localhost:64706/session/0186901c828d3a1ad7523ecd41dedf9a/element timed out after 60 seconds.
   at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
   at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath)
   at OpenQA.Selenium.By.<>c__DisplayClasse.<XPath>b__c(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
   at PTTest.PTGeneral.IsElementPresent(By by) in c:UsersjburnsDocumentsVisual Studio 2013ProjectsPTTestPTTestPTGeneral.cs:line 42
   at PTTest.PTGeneral.EmailCleanUP() in c:UsersjburnsDocumentsVisual Studio 2013ProjectsPTTestPTTestPTGeneral.cs:line 105
   at PTTest.UnitTest1`1.TearDown() in c:UsersjburnsDocumentsVisual Studio 2013ProjectsPTTestPTTestUnitTest1.cs:line 29
TearDown failed for test fixture PTTest.UnitTest1<FirefoxDriver>

所以我去掉了[TestFixture]和设置中的多浏览器部分,这样我就只能针对FF进行测试,并使用将驱动程序超时时间增加到3米

General.Driver = new FirefoxDriver(new FirefoxBinary(), new FirefoxProfile(), TimeSpan.FromSeconds(180));

这很有效,让我的测试通过了。但我仍然需要一个在多个浏览器上运行时有效的解决方案,因为我不想在应该有办法实现

的情况下维护两个不同的项目

您的问题是必须等到加载页面。

然后,解决方案是每次打开新页面时都使用等待方法。

在导航到每个新页面后使用此方法:

        public void WaitForPageLoading(int secondsToWait = 600)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            try
            {
                while (sw.Elapsed.TotalSeconds < secondsToWait)
                {
                    var pageIsReady = (bool)((IJavaScriptExecutor)Driver).ExecuteScript("return document.readyState == 'complete'");
                    if (pageIsReady)
                        break;
                    Thread.Sleep(100);
                }
            }
            catch (Exception)
            {
                Driver.Dispose();
                throw new TimeoutException("Page loading time out time has passed " + secondsToWait + " seconds");
            }
            finally
            {
                sw.Stop();
            }
        }

最新更新