Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a already opened firefox for testing in Selenium

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?

like image 692
karthik27 Avatar asked Oct 30 '13 17:10

karthik27


2 Answers

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

like image 174
Sergio Cazzolato Avatar answered Nov 15 '22 16:11

Sergio Cazzolato


Best way to do that is, extend RemoteWebDriver and override startSession method-:

Steps:

  1. Start selenium server using command- java -jar selenium-server-standalone-3.x.x.jar. By default your session start on port 4444.

  2. open url http://localhost:4444/wd/hub/static/resource/hub.html

  3. start new firefox session clicking on create session button and select firefox browser.

  4. Once the session start, copy the session id and paste it in property file or xml file where you want.

  5. 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());
      }
    }
    
like image 30
Rajiv Sharma Avatar answered Nov 15 '22 15:11

Rajiv Sharma