【问题标题】:Webdriver C# how to set driver timeouts with multiple browsersWebdriver C#如何使用多个浏览器设置驱动程序超时
【发布时间】:2016-03-06 02:18:29
【问题描述】:

希望在这里找到我的答案,我花了大半周的时间试图自己解决这个问题,但我无法解决我的问题。一点背景知识,我是使用 webdriver 进行 C# 和 NUnit 测试的新手,我正在尝试为我工作的公司创建回归套装。我们有一些与 ebay 高度集成的产品,因此对于部分测试,我需要单击一个按钮,将我带到 ebay 登录页面,然后我需要登录。看起来很简单(或者我是这么认为的)。我遇到的问题是每次点击这个 ebay 登录页面 FF 超时。我目前有我的测试设置可以在多个浏览器中运行,chrome 和 IE 正在通过(它们也挂了一点)但 FF 从未通过。

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

所以 FF 的失败总是一样的,在 60 秒后超时。我已经阅读了其他似乎是类似问题的问题 (Selenium WebDriver throws Timeout exceptions sporadically) 有一些我感兴趣的解决方案是将驱动程序超时设置为大于 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:\Users\jburns\documents\visual studio 2013\Projects\PTTest\PTTest\bin\Debug\PTTest.dll
Run started: C:\Users\jburns\documents\visual studio 2013\Projects\PTTest\PTTest\bin\Debug\PTTest.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:\Users\jburns\Documents\Visual Studio 2013\Projects\PTTest\PTTest\PTGeneral.cs:line 42
   at PTTest.PTGeneral.EmailCleanUP() in c:\Users\jburns\Documents\Visual Studio 2013\Projects\PTTest\PTTest\PTGeneral.cs:line 105
   at PTTest.UnitTest1`1.TearDown() in c:\Users\jburns\Documents\Visual Studio 2013\Projects\PTTest\PTTest\UnitTest1.cs:line 29
TearDown failed for test fixture PTTest.UnitTest1<FirefoxDriver>

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

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

这很有效,使我的测试通过了。但是当我在多个浏览器上运行时,我仍然需要一个可以工作的解决方案,因为我不想在应该有办法时维护 2 个不同的项目

【问题讨论】:

  • 请说明您如何导航到这个加载时间很长的页面。显示您如何点击按钮。
  • 嗯,我找到了解决这个问题的办法。我能够解决它的方法是增加 SetPageLoadTimeout()。然后我从触发页面加载的按钮中捕获了链接,并通过 GoToUrl() 调用调用了新页面,该调用尊重由 SetPageLoadTimeout() 设置的超时。

标签: c# firefox selenium webdriver nunit


【解决方案1】:

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

那么解决方法就是每次打开新页面时都使用等待方法。

在导航到每个新页面后放置此方法:

        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();
            }
        }

【讨论】:

    猜你喜欢
    • 2018-08-08
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 2013-06-08
    相关资源
    最近更新 更多