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.
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.
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.
Will this work on Python?
ReplyDeleteI hope it will. May be u need to change the URI "http://localhost:7055/hub" to something else.
ReplyDeleteI have tried this in JAva. But it did not work.Is there any work around for the above said problem??????????
DeleteTry to write your java code in a JUnit project rather in Main function.
DeleteThanks for the write up. Will it work on java?
ReplyDeletehow to find out the localhost number ?
ReplyDeletecan we get Java equivalent for this
ReplyDeletecan we get Java equivalent for this
ReplyDeleteNow 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.
ReplyDeleteFor 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.
tried with java and firefox combination, not working for me... !!
ReplyDeletebelow 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");
}
Is there any work around for the above said problem??????????
DeleteHow can we achieve similar case in chrome?
ReplyDeleteCan we achieve same with IE browser ?
ReplyDelete