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. I hope this post will help you out too.