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!
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!
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.
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();
}
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()
{
...
}
}
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.
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.
You can run your tests in parallel, but that is a just a good idea when you're ready to run all of your tests. Below is an idea that you can use while you're developing and running newly compiled code.
Serialize your WebDriver {browser} instance object into a file (or in memory in a Windows service if you prefer), and then retrieve (de-serialize) that file into an object each time you launch WebDriver. It takes a little massaging though, of the WebDriver source code.
When I get some time, I will update my answer here. I hope Stack Overflow will allow me to paste as much code changes as it took. Plus, I should still give credit to Erik in his answer and comments. Although it was my idea to begin with (for making WebDriver faster).
.NET: Serializing object to a file from a 3rd party assembly (to make Selenium WebDriver faster)
==========
Simple logic:
if serialized object file doesn't exist ==> open browser instance as normal, and create the file
if serialized object file exists ==> deserialize file into object ==> set browser instance equal to deserialized object (casted correctly of course)
==========
The other requirement is that you put in a condition into your method decorated with the [TearDown] attribute so your browser doesn't close when the script (test) finishes. The first time through, it'll take time to open it up. But once that browser window is open, you're good to go.
[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());
}
you can use below code sample to achieve this task
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");
}
if a firefox browser is opened using a Firefox web driver, you can use Remote WebDriver to use that browser instance. So First I try to initialize a Remote Web Driver, if no instance is running, then try block will fail and Catch block will open the Firefox driver. Now for example, your script fails on a specific location and the browser remained open. Now again run that initialization code, the try block will pass this time and remote web Driver will use the existing opened browser. (No new browser window will open).
Put this initialization in your Test Startup method
This block of code saves my ample amount of time.
I have also written a blog post on it.
You can read it here.
http://www.binaryclips.com/2016/03/selenium-web-driver-in-c-how-to.html
org.openqa.selenium.remote.server.handler.GetAllSessions
. Here is my id, but i don't know if i can invoke it to rehuse certain session instead the lastest session used. –
Confirmand © 2022 - 2024 — McMap. All rights reserved.