Reputation: 484
This declaration
WebDriver driver = new FirefoxDriver();
always opens a new instance window of Firefox. It doesn't use the already opened firefox.
Can anyone let me know how to use a already opened firefox for testing instead of opening a new one?
Upvotes: 12
Views: 31059
Reputation: 33
Best way to do that is, extend RemoteWebDriver and override startSession method-:
Steps:
Start selenium server using command- java -jar selenium-server-standalone-3.x.x.jar. By default your session start on port 4444.
open url http://localhost:4444/wd/hub/static/resource/hub.html
start new firefox session clicking on create session button and select firefox browser.
Once the session start, copy the session id and paste it in property file or xml file where you want.
read session id form the file where you saved in following method
@Override
protected void startSession(Capabilities desiredCapabilities) {
String sid = getSessionIDFromPropertyFile();
if (sid != null) {
setSessionId(sid);
try {
getCurrentUrl();
} catch (WebDriverException e) {
// session is not valid
sid = null;
}
}
if (sid == null) {
super.startSession(desiredCapabilities);
saveSessionIdToSomeStorage(getSessionId().toString());
}
}
Upvotes: 1
Reputation: 986
Java example. First, you need to have Selenium server running.
java -jar C:\selenium-server-standalone-2.53.0.jar
To start a new session (first script):
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox());
Then, to reuse (attach) that session (second script):
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:7055/hub"),
DesiredCapabilities.firefox());
Notice the different port number.
Upvotes: 0
Reputation: 863
Use Remote Web Driver like this .
System.Uri uri = new System.Uri("http://localhost:7055/hub");
WebDriver = new RemoteWebDriver(uri, DesiredCapabilities.Firefox());
it will use the already opened Firefox browser. you can see the details of this approach in this blog post.
http://www.binaryclips.com/2016/03/selenium-web-driver-in-c-how-to.html
Upvotes: 12
Reputation: 3630
In Java, when you say new
a new object is instantiated. For WebDriver, every new
is a new browser window.
If you want the same browser to be used then use the same driver
object.
driver.get("URL PATH");
This will go to the new Url with the already open browser.
Upvotes: 0
Reputation: 462
Be careful with that, because in case the driver crashes once, then all the test cases that have to be executed after that will be affected because they are using the same driver, also you will be sharing cookies, and perhaps sessions already opened previously, etc.
The more robust solution is to create a new WebDriver for each test cases because doing that you are making all your tests cases less dependent on the others.
If the reason that is motivating you is the time each WebDriver takes to be created, perhaps you could start thinking on run test cases in parallel for example with TestNG.
Thanks
Upvotes: 1
Reputation: 107
You should instantiate your webdriver only once, when making a test and then pass it as argument for the other classes in constructors. Something like this:
public class Test {
WebDriver driver = new FirefoxDriver();
@Test
public void testHomePage() {
HomePage hp = new HomePage(driver);
//code here }
}
public class HomePage{
private static WebDriver driver;
public HomePage(WebDriver driver) {
this.driver = driver;}
}
Upvotes: 0