有没有办法让共享代码中的每个人都可以使用 gecko.driver?

Is there a way to make gecko.driver usable for everyone in shared code?

我将通过 BitBucket 分享我在 Selenium/Cucumber 框架中编写的代码。我使用以下方法使代码在 Firefox 中可执行。

System.setProperty("webdriver.gecko.driver","/Users/firatkaymaz/eclipse-workspace/SeleniumTest/drivers/geckodriver/geckodriver");
driver = new FirefoxDriver();

运行怎么可能在另一台 PC 或笔记本电脑上使用代码,因为 Gecko 驱动程序路径信息与我的本地计算机相关?有没有办法让 gecko.driver 可用于共享代码中 运行 的人,或者他们必须自己更改路径信息?

您有多种选择:

设置合适的环境变量

不要使用System.setProperty设置webdriver.gecko.driver。这应该在机器上设置为环境变量,而不是在代码中。这允许您在多个位置使用 gecko 驱动程序配置多个开发 machines/build 框。每台机器只需要设置环境变量webdriver.gecko.driver指向本地机器上的相关路径即可"will just work"。

使用驱动二进制下载器maven插件

这将允许您的 Maven 项目自动下载关联 RepositoryMap.xml 中指定的驱动程序二进制文件(显然需要您使用 Maven for Build/Dependency Management). If you haven't defined one it will download a default set of binaries (but they may well be out of date). For more details See Here.

<plugins>
    <plugin>
        <groupId>com.lazerycode.selenium</groupId>
        <artifactId>driver-binary-downloader-maven-plugin</artifactId>
        <version>1.0.17</version>
        <configuration>
            <!-- root directory that downloaded driver binaries will be stored in -->
            <rootStandaloneServerDirectory>/my/location/binaries</rootStandaloneServerDirectory>
            <!-- Where you want to store downloaded zip files -->
            <downloadedZipFileDirectory>/my/location/zips</downloadedZipFileDirectory>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>selenium</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

不幸的是,虽然这会下载二进制文件,但 Maven 不会在不同的 JVM 之间传递环境变量,它会在不同的阶段启动。因此,您需要将一些配置传递到您的测试配置中,例如:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.22.1</version>
    <configuration>
        <systemPropertyVariables>
            <!--Set properties passed in by the driver binary downloader-->
            <webdriver.chrome.driver>${webdriver.chrome.driver}</webdriver.chrome.driver>
            <webdriver.ie.driver>${webdriver.ie.driver}</webdriver.ie.driver>
            <webdriver.opera.driver>${webdriver.opera.driver}</webdriver.opera.driver>
            <webdriver.gecko.driver>${webdriver.gecko.driver}</webdriver.gecko.driver>
            <webdriver.edge.driver>${webdriver.edge.driver}</webdriver.edge.driver>
        </systemPropertyVariables>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <!--This goal makes the build fail if you have test failures-->
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>

See Here 一个完整的工作示例。

使用 Webdriver 管理器

这将允许您使用 Java 代码下载和配置驱动程序二进制文件。您可以使用 versions.properties 文件指定特定版本:

public class ChromeTest {

    private WebDriver driver;

    @BeforeClass
    public static void setupClass() {
        WebDriverManager.chromedriver().setup();
    }

    @Before
    public void setupTest() {
        driver = new ChromeDriver();
    }

    @After
    public void teardown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test
    public void test() {
        // Your test code here
    }

}

更多信息See Here

您可以改用 selenium 服务器

Download here

selenium-server-standalone-3.141.59.jargeckodriver收集在同一个路径下,方便使用

  1. 使用命令行转到您的路径并运行此命令:

java -jar selenium-server-standalone-3.141.59.jar -role hub

如果成功,您将获得此日志:

[Hub.start] - Selenium Grid hub is up and running
[Hub.start] - Nodes should register to http://somethingIP:4444/grid/register/
[Hub.start] - Clients should connect to http://somethingIP:4444/wd/hub
  1. 打开另一个命令行,然后运行以下命令(转到您的路径):

java -Dwebdriver.gecko.driver=geckodriver -jar selenium-server-standalone-3.141.59.jar -role node -hub http://localhost:4444/grid/register

如果成功,您将获得此日志:

The node is registered to the hub and ready to use

在您的代码中,使用以下代码初始化 driver

DesiredCapabilities dc = new DesiredCapabilities();
WebDriver driver;

//replace localhost with the real IP if you try to access it from another PC
URL url = new URL("http://localhost:4444/wd/hub");
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);

driver = new RemoteWebDriver(url, dc);

You can read the documentation here