Cucumber (Java) and Selenium: where to place drive

2019-08-20 03:27发布

I'm building a test suite using Cucumber for Java with Selenium. And my project structure is pretty much like this:

  • src/test/java: this is where I have my test steps implementation.
  • src/test/resources/features: this is where I have feature files.
  • src/test/resources/seleniumdrivers: this is where I put chromedriver.exe.

Now, what I did was to add a Hooks.java class in src/test/java with a method that sets up the driver path, using the @Before hook:

@Before
public void setUpDriver(){
    System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\seleniumdrivers\\chromedriver.exe");
}

However, since this method will run before each scenario, I'd like to find a better way to set up the path, so it's only done once. Please note that I want to have the driver within my project structure and set it using a system property (I mean, I don't want to place the driver somewhere in my filesystem and add it to the PATH environment variable).

Is there a better way to do this?

4条回答
我命由我不由天
2楼-- · 2019-08-20 03:57

You can keep driver inside your project folder and get that project path using System.getProperty

You can try below code :-

 String path= System.getProperty("user.dir");

  System.setProperty("webdriver.chrome.driver", path+"\\src\\test\\resources\\test\\chromedriver.exe");
查看更多
Explosion°爆炸
3楼-- · 2019-08-20 04:02

If you use Maven add these 2 dependencies in your pom.xml and you will be fine and now you can remove the System.setProperty line. With this technique the project has less hardcode method.

 <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>3.3.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.2</version>
        <scope>test</scope>
    </dependency>

Also you need to add this line to set it up

        WebDriverManager.chromedriver().setup();
查看更多
神经病院院长
4楼-- · 2019-08-20 04:08

As you don't want to add it to the PATH environment variable you can place the chromedriver binary anywhere within your filesystem (including src\\test\\resources\\seleniumdrivers\\) and still can specify in the System.setProperty() as follows :

@Before
public void setUpDriver(){
    System.setProperty("webdriver.chrome.driver", ".\\src\\test\\resources\\seleniumdrivers\\chromedriver.exe");
}
查看更多
趁早两清
5楼-- · 2019-08-20 04:10

You can create one property file like config.properties to store all the global values which you use throughout the execution and also path of the chromedriver.exe and read it before all scenarios and use throughout the executions like this.

public class Hooks {
    private static boolean beforeSuit = true;
    private static String executablePath;
    static Properties prop;

    @Before
    public void beforeAll() {
        if(beforeSuit) {
            prop = new Properties();
            ClassLoader loader = Thread.currentThread().getContextClassLoader();           
            InputStream stream = loader.getResourceAsStream("/config.properties");
            prop.load(stream);
            //You can use this anywhere you want to launch the chrome.
            executablePath = prop.getProperty("executablePath");
            //To make it execute only once
            beforeSuit = false;

            //If you wish to launch browser only once , you can have that code here.
        }

        //Here you can keep code to be execute before each scenario

    }
}
查看更多
登录 后发表回答