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

旧城冷巷雨未停 提交于 2019-12-11 15:25:14

问题


I'm going to share my code written in Selenium/Cucumber framework through BitBucket. I used following to make the code executable in Firefox.

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

How can running the code in another PC or laptop be possible, because the Gecko driver path info is related to my local machine? Is there a way to make gecko.driver usable for that person who's going to run in shared code or they have to change the path information with their own?


回答1:


You have multiple options:

Set appropriate environmental variables

Don't use System.setProperty to set webdriver.gecko.driver. This should be set as an environmental variable on the machine, not in code. This allows you to configure multiple dev machines/build boxes with gecko driver in multiple locations. Each machine just needs to set the environmental variable webdriver.gecko.driver to point to the relevant path on the local machine and it "will just work".

Use the driver binary downloader maven plugin

This will allow your Maven project to automatically download driver binaries as specified in an associated RepositoryMap.xml (It obviously requires you to use 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>

Unfortunately, while this does download binaries Maven doesn't pass Environmental variables between the different JVMs it starts up for different phases. So you will need to pass some configuration into your test configuration e.g:

<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 for a full working example.

Use Webdriver Manager

This will allow you to download and configure driver binaries using Java code. You can specify specific versions using a versions.properties file:

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
    }

}

For more information See Here.




回答2:


You can use selenium server instead

Download here

Collect selenium-server-standalone-3.141.59.jar and geckodriver in the same path to be easy to use

  1. With command line go to your path and run this command :

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

If successful, you will get this log:

[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. Open another command line, then run the following command (go to your path) :

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

If successful, you will get this log:

The node is registered to the hub and ready to use

In your code, initialization driver with the following code :

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



来源:https://stackoverflow.com/questions/56758747/is-there-a-way-to-make-gecko-driver-usable-for-everyone-in-shared-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!