如何连接到一个已经打开的浏览器? [英] How to connect to an already open browser?
问题描述
我真的很感激如何连接到通过C#中使用硒的webdriver一个已经打开的浏览器的指南。
I would really appreciate a guide on how to connect to an already open browser using Selenium Webdriver via C#.
这问题吃大约30%的我的脚本开发时间!
This issue eats around 30% of my script development time!
推荐答案
您可以指定开始和[TestFixtureSetUp]关闭浏览器和[TestFixtureTearDown]和[设置]中删除它和[拆除]。在[可是TestFixture]所有的测试将在同一浏览器中运行。所以,如果你有例如10个班,每个人有5个测试,而不是50浏览器的开启和关闭,将只有10。
You can specify starting and closing browser in [TestFixtureSetUp] and [TestFixtureTearDown] and remove it from [SetUp] and [TearDown]. All the tests in [TestFixture] will be run in the same browser. So if you have for example 10 classes and each of them contains 5 tests, instead of 50 browser's openings and closings there will be only 10.
public IWebDriver driver { get; private set; };
[TestFixtureSetUp]
public void TestFixtureSetup()
{
driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com/");
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
driver.Quit();
}
如果您要打开,一旦关闭浏览器,你可以继承[TestFixtureSetUp ]和[TestFixtureTearDown]从基类的方法,如果您有其他(A_test)前执行一个测试类将最后一个(Z_test)执行一个你可以设置和取消一些标志,这将告诉我们是否应该启动浏览器或不
If you want to open and close your browser once, you can inherit [TestFixtureSetUp] and [TestFixtureTearDown] methods from base class and if you have one test class that is executed before others (A_test) and one that is executed last (Z_test) you can set and unset some flags that will tell if we should start browser or not:
namespace Tests
{
[TestFixture]
public abstract class Test
{
private static bool _flagSetUp;
private static bool _flagTearDown;
public IWebDriver driver { get; private set; };
protected Test()
{
}
public static void SetFlag(bool flagSetUp, bool flagTearDown)
{
_flagSetUp = flagSetUp;
_flagTearDown = flagTearDown;
}
[TestFixtureSetUp]
public void TestFixtureSetup()
{
if(_flagSetUp)
{
driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com/");
_flagSetUp = false;
}
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
if(_flagTearDown)
{
driver.Quit();
}
}
}
namespace Tests
{
[TestFixture(new object[] { true, false })]
public class A_Test : Test
{
public A_Test(bool flagSetUp, bool flagTearDown)
{
SetFlag(flagSetUp, flagTearDown);
}
[Test]
public void Test1()
{
...
}
}
namespace Tests
{
[TestFixture(new object[] { false, true })]
public class Z_Test : Test
{
public A_Test(bool flagSetUp, bool flagTearDown)
{
SetFlag(flagSetUp, flagTearDown);
}
[Test]
public void Test2()
{
...
}
}
最后的解决方法看起来不是很好的解决方案。我们虽然第一个解决方法也并不能保证100%的测试隔离(顺便说一句,不要忘记写 driver.Navigate()GoToUrl(http://www.google.com/);
作为每个试验的第一工序)。所以,可能是最好的解决方法是将并行执行和设置,为每个测试拆解方法。
The last workaround looks like not good solution. Althouth first workaround also doesn't guarantee 100% tests isolation (btw don't forget to write driver.Navigate().GoToUrl("http://www.google.com/");
as first step for each test). So probably the best solution will be parallel execution and Setup, Teardown methods for each test.
这篇关于如何连接到一个已经打开的浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!