Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to attach an already running browser to selenium webdriver in java? [duplicate]

I am developing a application in which we need to check if the web application is running and the url giving the expected results.

Our Scheduler run this application 12 times a day. In every launch the firefox driver gets executed and a new browser window opens to perform the operations.

I want a technique where we open the firefox browser 1 time and reuse it in every call by selenium-driver.

like image 379
Satish Pandey Avatar asked Aug 15 '12 13:08

Satish Pandey


People also ask

Can Selenium interact with already opened browser?

New Selenium IDEWe can interact with an existing browser session. This is performed by using the Capabilities and ChromeOptions classes. The Capabilities class obtains the browser capabilities with the help of the getCapabilities method.

How do I run Selenium scripts on already opened browser on Mac?

ChromeOptions option = new ChromeOptions(); option. DebuggerAddress = "127.0. 0.1:9222"; ChromeDriver driver = new ChromeDriver(option); After that, you can do the work as always.


2 Answers

I am actually not entirely sure you can switch to a window not spawned by the driver. I think the people working on the selenium 2 project have worked a little bit on switching to a window spawned by a different driver. When you do:

driver.getWindowHandles()

All you get are the windows spawned by the driver object it is called on.

This is a pretty old feature request: Allow webdriver to attach to a running browser . So it's not possible right now.

like image 115
some_other_guy Avatar answered Sep 30 '22 13:09

some_other_guy


You can see an example I wrote that proves that re-using browsers works just fine in WebDriver. Basically, as long as a browser window is instantiated from WebDriver, then you can use the getWindowHandles() to always grab onto it. If you have multiple windows open, just keep track of them using a List . You can identify windows that appear a certain way or contain certain information in them by using various WebDriver methods.

In other words, if a browser window was not opened by WebDriver, then WebDriver has no ability to hook onto it.

A rough example:

public static boolean selectWindow(WebDriver driver, String windowTitle){
    //Search ALL currently available windows
    for (String handle : driver.getWindowHandles()) {
        String newWindowTitle = driver.switchTo().window(handle).getTitle();
        if(newWindowTitle.equalsIgnoreCase(windowTitle))
            //if it was found break out of the wait
            return true;
    }
    return false;

}

In one project I did, I created a method that returns certain int status codes, depending on my arrangement of windows. If the status code is what I am expecting , then I know the next test can proceed without logging in again or without opening a new window.

Of course, if your test framework, such as Surefire or TestNG, forks threads by class, then you need one webdriver instance per class. If your test framework forks by method, then you'll need to pass the webdriver instance as an argument to the test method so the thread has access to it.

like image 24
djangofan Avatar answered Sep 30 '22 11:09

djangofan