Selenium Webdriver via C# - how to connect to an already open browser?

Refer to Selenium Issue 18 . This is a very popular feature request, which has unfortunately not been implemented. The comments suggest a few workarounds for now, I haven't tried them but you might find something useful.

Up vote 6 down vote favorite share g+ share fb share tw.

I would really appreciate a guide on how to connect to an already open browser using Selenium Webdriver via C#. This issue eats around 30% of my script development time! C# selenium webdriver link|improve this question edited Mar 16 at 14:03Rob Hruska15.2k53072 asked Oct 14 '11 at 10:08Andrey707 57% accept rate.

– atwellpub Dec 30 '11 at 22:28 No. I'm not sure about the answers below, please comment back if something works for you – Andrey Feb 3 at 16:06.

Refer to Selenium Issue 18. This is a very popular feature request, which has unfortunately not been implemented. The comments suggest a few workarounds for now, I haven't tried them but you might find something useful.

I did not find any workarounds for C# there – Andrey Oct 17 '11 at 8:14 there is one in Java which could give you a pretty good idea of how the workaround is accomplished, then you can implement it in C# – prestomanifesto Oct 17 '11 at 19:38 That's exactly what I need help with :) – Andrey Oct 18 '11 at 9:25.

Since time is your primary issue and this is not natively supported. Why adopt an alternative approach. Develop/implement a basic connect & navigate to home page use case.

Then extend that class for your more complex use case. Alternatively make Test case controller, then use a factory to instantiate your detailed tests, passing the webdriver instance into the test case.

Look into Galio/MBUnit or PNUnit for Paralleizing your TestFixtures. Run the selenium RC jar file on a Remote Machine and give it the role of Hub. Then run another Selenum RC jar file with the role of -Node and have it connect to the Hub.

Install the appropriate browsers on the machines that each Selenium Jar -Node is running on. Look up Selenium Grid 2. It's essentially using the old Selenium RC server to control the sessions for your Selenium WebDrivers.

Setup your TestFixtures to create a IWebDriver RemoteWebDriver and pass in the IP address of the Hub. And at the top of the Test Fixture Class you can use Parallelisable and TestFixture The Hub will handle the Browser sessions (which can actually run more than 10 browsers at the same time) given you know how to thread your selenium tests. (Which is why I recommend looking into Unit Testing Framework Galio/MBUnit and PNUnit.

Then you can use the Galio GUI which will recognise your Parallelising attributes on your TestFixtures and run them at the same time, which will trigger new RemoteWebDrivers to connect to your Hub which will trigger off the Browser Sessions on each of the Nodes. There are no issues with Screenshots as they can still be acheived using the RemoteWebDriver, also any PageObjects (If you are using the PageObject design pattern) with Selenium WebDriver won't need to be changed because WebDriver is supported. Doing some research into this, you could actually get many tests all running at the same time, while not only improving your develoment time and testing feedback time, it also stresses the system which you are testing under realistic conditions of hammering the service many times in a short duration using multiple browsers.

It won't be easy, but to get you started here are some things you might want to search into, because It's not something I can just write here as 'a solution' to your problem. Selenium Grid 2. Selenium RC Server - Roles (-Hub, -Node) EventFiringWebDriver - This gives you the ability to wrap your actual WebDriver in another Driver Object which triggers events useful for logging information, such as onFindingElement, OnElementFound, OnException, OnNavigating.

MBUnit/Galio Parallel Testing NUnit (2.6) Test, TestCaseSource("Function") Attribute functions. SetUp TearDown TestFixture Serialised TestCases to XML. Selenium PageObject Design Pattern.

- Have a quick look at some of the questions I have pasted on StackOverflow.

Sorry, don't understand how this is releavant. I need to connect to already open browser using Selenium WebDriver via C# – Andrey Feb 3 at 16:01 You said you need to, because its taking up time having to open and close a browser for each test. Why not just run them all at the same time and cut your development time by a large amount.

– Patrick Magee Feb 3 at 19:49 How can running of scripts decrease their development time? – Andrey Feb 9 at 14:40 Because the faster the scripts finish running, the faster he can continue his development? Isn't rocket science sir.

– Patrick Magee Feb 9 at 16:12.

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("google.com/"); } TestFixtureTearDown public void TestFixtureTearDown() { driver.Quit(); } 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("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() { ... } } 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("google.com/"); as first step for each test). So probably the best solution will be parallel execution and Setup, Teardown methods for each test.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions