Webdriver C#如何设置多个浏览器的驱动程序超时 [英] Webdriver C# how to set driver timeouts with multiple browsers

查看:401
本文介绍了Webdriver C#如何设置多个浏览器的驱动程序超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望在这里找到我的答案,我花了一个星期的更好的一半试图自己解决这个问题,我无法解决我的问题。一个小背景下,我是C#和NUnit测试与webdriver的新手,我试图为我工作的公司创建一个回归服。我们有一些产品与ebay高度集成,因此对于部分测试,我需要点击一个按钮,它将我带到ebay登录页面,然后我需要登录。似乎很简单(或者我认为)。我遇到的问题是每次点击这个易趣登录页面FF超时。我目前有我的测试设置运行在多个浏览器,铬和IE浏览器(他们也挂一点),但FF从来没有通过。



这对我来说太奇怪了,因为在本次测试中,我前往ebay并成功登录。但是当我不得不把我的公司帐户连接到易趣帐户这个登录页面永远。我知道我们通过一些独特的令牌易趣链接的帐户,这是我认为导致长时间加载。

所以FF的失败总是一样的,60秒后超时。我已经阅读了其他似乎是类似问题的问题( Selenium WebDriver零星抛出超时异常)有一些我感兴趣的解决方案是将驱动程序超时设置为大于60秒。我不知道如何使用多个浏览器设置来做到这一点。

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




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

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

任何帮助都将不胜感激,如果您想了解更多信息,明白你在问什么;)

谢谢大家的阅读,这个社区是惊人的。

Still与这一个挣扎。这是错误信息,如果有帮助。

  ------开始运行测试------ 
执行测试的NUnit VS Adapter 2.0.0.0启动
从C:\ Users \jburns\documents\visual studio 2013 \Projects\PTTest\PTTest\bin\Debug加载测试\PTTest.dll
运行开始:C:\ Users \jburns\documents\visual studio 2013\Projects\PTTest\PTTest\\\\\\\\\\\\\\\\\\\\ $ b TearDown测试夹具失败PTTest.UnitTest1< ChromeDriver>
对于URL http:// localhost:64706 / session / 0186901c828d3a1ad7523ecd41dedf9a /元素的远程WebDriver服务器的HTTP请求在60秒后超时。
在OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest请求)
在OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(命令commandToExecute)
在OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute (命令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上下文)
在OpenQA.Selenium .By.FindElement(ISearchContext上下文)
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.P TGeneral.EmailCleanUP()在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测试夹具失败PTTest.UnitTest1< FirefoxDriver>

所以我将[TestFixture]中的多个浏览器文件剥离出来,测试只对FF,并增加驱动程序超时到3m与
$ b $ pre $ General.Driver =新的FirefoxDriver(新的FirefoxBinary(),新的FirefoxProfile (),TimeSpan.FromSeconds(180));

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

解决方案

你的问题是,你必须等待,直到页面加载。

然后解决方案将使用等待方法每次当新页面正在打开。



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

  public void WaitForPageLoading(int secondsToWait = 600)
{
Stopwatch sw = new Stopwatch();
sw.Start();
尝试
{
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(页面加载超时已过+ secondsToWait +seconds);
}
finally
{
sw.Stop();
}
}


Hoping to find my answer here I have spent the better half of a week trying to figure this out myself and I cannot fix my issue. A little background, I am new to C# and NUnit tests with webdriver, I am attempting to create a regression suit for the company I work for. We have a few products that are heavily integrated with ebay and so for part of the test I need to click on a button which takes me to the ebay login page and then I need to login. Seems simple enough (or so I thought). The issue I am running into is when hitting this ebay login page FF times out each time. I currently have my test setup to run in multiple browsers, chrome and IE are passing (they also hang a little) but FF has never passed.

This is odd to me because earlier in this test I go to ebay and sucessfully login. But when I have to link my company account to the ebay account this login page takes forever. I know we pass some unique token to ebay to link the accounts which is what I think causes the long load times.

So the failure with FF is always the same, timed out after 60 seconds. I have read other questions that seemed to be a similar issue (Selenium WebDriver throws Timeout exceptions sporadically) there are a few solutions the one I am interested in is setting the driver timeout to something greater than 60 seconds. I do not know how to do this with multiple browser setup I have going.

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

    }

I would really like to keep this setup as I like how I can test IE, FF, and Chrome, but I do not know how to implement a solution like this then.

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

Any help would be greatly appreciated, if you would like more information ill be happy to provide, assuming I understand what you are asking ;)

Thank you all for even reading, this community is amazing.

Still struggling with this one. Here is the error message if that helps.

------ 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>

So I stripped out the multiple browser piece I had in the [TestFixture] and setup so that I could test only against FF and increased the driver timeout to 3m with

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

This worked and made my tests pass. But I still need a solution that works when I run against multiple browsers, as I don't want to maintain 2 different projects when there should be a way to

解决方案

Your problem is that you have to wait till a page is loading.

Then the solution will be to use waiting methods each time when a new page is being opening.

Put this method after navigating to each new page:

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

这篇关于Webdriver C#如何设置多个浏览器的驱动程序超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆