Java 如何使用已经打开的 Firefox 在 Selenium 中进行测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19690157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:24:54  来源:igfitidea点击:

How to use a already opened firefox for testing in Selenium

javaselenium-webdriver

提问by karthik27

This declaration

本声明

WebDriver driver = new FirefoxDriver();

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

总是打开一个新的 Firefox 实例窗口。它不使用已经打开的 Firefox。

Can anyone let me know how to use a already opened firefox for testing instead of opening a new one?

谁能告诉我如何使用已经打开的 Firefox 进行测试而不是打开一个新的?

采纳答案by Sergio Cazzolato

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.

小心这一点,因为如果驱动程序崩溃一次,那么之后必须执行的所有测试用例都将受到影响,因为它们使用相同的驱动程序,而且您将共享 cookie,并且可能之前已经打开的会话,等等。

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.

更健壮的解决方案是为每个测试用例创建一个新的 WebDriver,因为这样做会使所有测试用例对其他测试用例的依赖程度降低。

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.

如果激励您的原因是创建每个 WebDriver 所需的时间,也许您可​​以开始考虑并行运行测试用例,例如使用 TestNG。

Thanks

谢谢

回答by Maria

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:

在进行测试时,您应该只实例化一次您的 webdriver,然后将其作为构造函数中其他类的参数传递。像这样的东西:

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;}
}

回答by LINGS

In Java, when you say newa new object is instantiated. For WebDriver, every newis a new browser window.

在 Java 中,当您说new实例化一个新对象时。对于 WebDriver,每一个new都是一个新的浏览器窗口。

If you want the same browser to be used then use the same driverobject.

如果您希望使用相同的浏览器,请使用相同的driver对象。

driver.get("URL PATH");

This will go to the new Url with the already open browser.

这将使用已经打开的浏览器转到新的 Url。

回答by joinsaad

Use Remote Web Driver like this .

像这样使用远程 Web 驱动程序。

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.

它将使用已经打开的 Firefox 浏览器。您可以在此博客文章中查看此方法的详细信息。

http://www.binaryclips.com/2016/03/selenium-web-driver-in-c-how-to.html

http://www.binaryclips.com/2016/03/selenium-web-driver-in-c-how-to.html

回答by John Doe

Java example. First, you need to have Selenium server running.

Java 示例。首先,您需要运行 Selenium 服务器。

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.

注意不同的端口号。

回答by Rajiv Sharma

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

最好的方法是,扩展 RemoteWebDriver 并覆盖 startSession 方法-:

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());
      }
    }
    
  1. 使用命令 java -jar selenium-server-standalone-3.xxjar 启动 selenium 服务器。默认情况下,您的会话在端口 4444 上启动。

  2. 打开网址http://localhost:4444/wd/hub/static/resource/hub.html

  3. 单击创建会话按钮启动新的 Firefox 会话并选择 Firefox 浏览器。

  4. 会话开始后,复制会话 ID 并将其粘贴到您想要的属性文件或 xml 文件中。

  5. 从您使用以下方法保存的文件中读取会话 ID

    @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());
      }
    }