如何连接已经打开的浏览器?

我非常感谢有关如何使用Selenium Webdriver通过C#连接到已经打开的浏览器的指南。

这个问题占我脚本开发时间的30%左右!

请参阅Selenium Issue 18 。 这是一个非常受欢迎的function请求,遗憾的是没有实现。 评论提示现在有一些解决方法,我没有尝试过,但你可能会发现一些有用的东西。

您可以在[TestFixtureSetUp]和[TestFixtureTearDown]中指定启动和关闭浏览器,并从[SetUp]和[TearDown]中删除它。 [TestFixture]中的所有测试都将在同一浏览器中运行。 因此,如果你有10个类,每个类包含5个测试,而不是50个浏览器的开放和关闭,那么只有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(); } 

如果要打开和关闭浏览器一次,可以从基类inheritance[TestFixtureSetUp]和[TestFixtureTearDown]方法,如果有一个测试类在其他测试类之前执行(A_test)和一个最后执行的测试类(Z_test)你可以设置和取消设置一些标志,告诉我们是否应该启动浏览器:

 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() { ... } } 

最后的解决方案看起来不是很好的解决方案。 Althouth第一个解决方法也不保证100%测试隔离(顺便说一句,不要忘记编写driver.Navigate().GoToUrl("http://www.google.com/");作为每个测试的第一步)。 因此,最好的解决方案可能是并行执行和每个测试的Setup,Teardown方法。

由于时间是您的主要问题,因此本机不支持。 为什么采用替代方法。 开发/实现基本连接并导航到主页用例。 然后为更复杂的用例扩展该类。 或者制作测试用例控制器,然后使用工厂实例化您的详细测试,将webdriver实例传递给测试用例。

您可以并行运行测试,但是当您准备好运行所有测试时,这只是一个好主意。 下面是您在开发和运行新编译的代码时可以使用的想法。

将WebDriver {browser}实例对象序列化为文件(如果愿意,可以在Windows服务的内存中),然后在每次启动WebDriver时将该文件检索(反序列化)到对象中。 但是,WebDriver源代码需要进行一些按摩。

当我有空的时候,我会在这里更新我的答案。 我希望Stack Overflow允许我粘贴尽可能多的代码更改。 另外,我仍然应该赞扬Erik的回答和评论。 虽然这是我的想法开始(使WebDriver更快)。

.NET:将对象序列化为来自第三方程序集的文件(以使Selenium WebDriver更快)

==========

简单逻辑:

  • 如果序列化对象文件不存在==>正常打开浏览器实例,并创建该文件

  • 如果序列化对象文件存在==>将文件反序列化为对象==>将浏览器实例设置为等于反序列化对象(当然是正确转换)

==========

另一个要求是您在使用[TearDown]属性修饰的方法中添加条件,以便在脚本(测试)完成时您的浏览器不会关闭。 第一次,打开它需要时间。 但是一旦浏览器窗口打开,你就可以开始了。

 [TearDown] public void TeardownTest() { try { if (Config.CLOSE_BROWSER_AFTER_TEST_CASE) { driver.Quit(); } } catch (Exception) { // Ignore errors if unable to close the browser } Assert.AreEqual("", verificationErrors.ToString()); } 

您可以使用下面的代码示例来完成此任务

 IWebDriver WebDriver = null; try { System.Uri uri = new System.Uri("http://localhost:7055/hub"); WebDriver = new RemoteWebDriver(uri, DesiredCapabilities.Firefox()); Console.WriteLine("Executed on remote driver"); } catch (Exception) { WebDriver = new FirefoxDriver(); Console.WriteLine("Executed on New FireFox driver"); } 

如果使用Firefox Web驱动程序打开firefox浏览器,则可以使用Remote WebDriver来使用该浏览器实例。 所以首先我尝试初始化远程Web驱动程序,如果没有实例正在运行,那么try block将失败,Catch块将打开Firefox驱动程序。 例如,您的脚本在特定位置失败,浏览器保持打开状态。 现在再次运行该初始化代码,try块将通过此时间,远程Web驱动程序将使用现有的已打开浏览器。 (不会打开新的浏览器窗口)。

将此初始化放在Test Startup方法中
这段代码节省了我足够的时间。 我也写过一篇博文。 你可以在这里阅读它。 http://www.binaryclips.com/2016/03/selenium-web-driver-in-c-how-to.html