How to use a already opened firefox for testing in Selenium

前端 未结 6 781
不思量自难忘°
不思量自难忘° 2020-12-05 15:49

This declaration

WebDriver driver = new FirefoxDriver();

always opens a new instance window of Firefox. It doesn\'t use the already opened

相关标签:
6条回答
  • 2020-12-05 16:16

    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

    0 讨论(0)
  • 2020-12-05 16:26

    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;}
    }
    
    0 讨论(0)
  • 2020-12-05 16:27

    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.

    0 讨论(0)
  • 2020-12-05 16:27

    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.

    0 讨论(0)
  • 2020-12-05 16:31

    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

    0 讨论(0)
  • 2020-12-05 16:35

    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());
        }
      }
      
    0 讨论(0)
提交回复
热议问题