Sunday, March 27, 2016

Selenium Web Driver in C#: How to continue script on the already opened browser instance.

This was the very first challenge I encounter when I begin to do scripting in Selenium. Sometimes my scripts failed in between due to any unexpected dialog. To continue my scripts from the point where my scripts failed seems to be impossible in Selenium. So every time, my scripts failed, I have to start execution from the very beginning. This was taking a lot of time in debugging my scripts. So I desperately needed a solution.
After a couple of searches and different trial and errors, I devised a strategy.
I noticed that when I initialize the Firefox WebDriver, It opens the new browser instance. If I don’t close that browser instance and I initialize the Remote WebDriver instance, it uses the already opened FireFox browser. When I noticed that, its easy to write code to handle this and use it according to my way. So here is the code.
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(firefoxProfile);
Console.WriteLine("Executed on New FireFox driver");

}

The above code is easy to understand. If my Firefox browser is closed, the try block will fail and catch block will work, the script will open the new Firefox browser using FirefoxDriver.
If Firefox browser is already opened (due to previous script execution) , the try block will work and will initialize the Remote WebDriver. This Remote WebDriver will use the already opened FireFox browser and scripts will start where you want them to start from.
This strategy helps me a lot and saves an ample amount of time in my automation debugging. I hope this post will help you out too.

13 comments:

  1. I hope it will. May be u need to change the URI "http://localhost:7055/hub" to something else.

    ReplyDelete
    Replies
    1. I have tried this in JAva. But it did not work.Is there any work around for the above said problem??????????

      Delete
    2. Try to write your java code in a JUnit project rather in Main function.

      Delete
  2. Thanks for the write up. Will it work on java?

    ReplyDelete
  3. how to find out the localhost number ?

    ReplyDelete
  4. can we get Java equivalent for this

    ReplyDelete
  5. can we get Java equivalent for this

    ReplyDelete
  6. Now I wonder whether this technique will work for the other browsers - IE, Chrome, Safari. Or if one is using a real RemoteWebDriver (on another host or in grid node). Worth to check.

    For those asking about this for other languages, the approach is the same - use your language's version of a "try" block to instantiate a RemoteWebDriver with your browser's desired capability type and that type of URI, with your language's version of a "catch" block instantiating the normal browser session (e.g. local FirefoxDriver). try/catch is basically exception handling, whatever they call it in a given language.

    ReplyDelete
  7. tried with java and firefox combination, not working for me... !!
    below is the code snippet tried


    WebDriver webDriver = null;

    try
    {
    URL uri = new URL("http://localhost:7055/hub");
    webDriver = new RemoteWebDriver(uri, DesiredCapabilities.firefox());
    System.out.println("Executed on remote driver");
    }
    catch (Exception e)
    {
    webDriver = new FirefoxDriver();
    System.out.println("Executed on New FireFox driver");
    }

    ReplyDelete
    Replies
    1. Is there any work around for the above said problem??????????

      Delete
  8. How can we achieve similar case in chrome?

    ReplyDelete
  9. Can we achieve same with IE browser ?

    ReplyDelete