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?
You can keep driver inside your project folder and get that project path using
System.getProperty
You can try below code :-
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.
Also you need to add this line to set it up
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 theSystem.setProperty()
as follows :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 thechromedriver.exe
and read it before all scenarios and use throughout the executions like this.