Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to an already open browser?

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!

like image 664
Andrey Avatar asked Oct 14 '11 10:10

Andrey


People also ask

Can we run the Selenium code in already open browser?

We can connect to an already open browser using Selenium webdriver. This can be done using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.

How do I close all open browsers in Selenium?

close() closes only the current window on which Selenium is running automated tests. The WebDriver session, however, remains active. On the other hand, the driver. quit() method closes all browser windows and ends the WebDriver session.


2 Answers

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.

like image 189
prestomanifesto Avatar answered Oct 07 '22 20:10

prestomanifesto


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.

like image 21
Aleh Douhi Avatar answered Oct 07 '22 18:10

Aleh Douhi