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

时间:2015-12-02 00:04:28

标签: c# firefox selenium webdriver nunit

希望在这里找到我的答案,我花了半个多星期的时间试图弄明白自己,我无法解决我的问题。一点点背景,我是C#的新手和使用webdriver的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进行测试,并将驱动程序超时时间增加到3米

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

这工作并使我的测试通过。但我仍然需要一个解决方案,当我针对多个浏览器运行时,因为我不希望在应该有方法时维护2个不同的项目

1 个答案:

答案 0 :(得分:0)

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

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

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

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