Define WebDriver path in build.gradle for Selenium test programs rather than setting System Property programmatically

Viewed 2787

For my Selenium test programs I have been defining absolute path to webdriver executables by setting System properties.

For example -

System.setProperty("webdriver.edge.driver",
                    new File("webdrivers/MicrosoftWebDriver.exe").getAbsolutePath());

System.setProperty("webdriver.chrome.driver",
                    new File("webdrivers/chromedriver.exe").getAbsolutePath());

This approach works flawlessly. However, for each development environment I have to download the webdrivers, put them in a folder, and then reference them by setting the system property as shown above.

I don't like this approach so I researched online for smarter way. Then I found some libraries in maven repository and I added them to by build.gradle file.

dependencies {
    compile group: 'org.seleniumhq.selenium', name: 'selenium-firefox-driver', version: '3.4.0'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: '3.4.0'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-ie-driver', version: '3.4.0'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-safari-driver', version: '3.4.0'
    compile group: 'org.seleniumhq.selenium', name: 'selenium-edge-driver', version: '3.4.0'
}

I shouldn't be expecting the program to magically work when I add these dependencies, right? Because that didn't work.

There is a repository here where the developer has tried to implement something that I want to achieve for my project. I tried his approach, but failed. My program wasn't able to locate appropriate drivers.

A project called WebDriverManager looked compelling but worked only for Chrome and Internet Explorer. I had issues with Firefox and Opera using this library which I didn't have while going through manual driver download and setting system property approach.

Okay, I will break down my problem into two questions now -

1) If I use the dependencies like I showed earlier, how would I modify my build.gradle file so that I done't have to set the system property and point to the webdrivers .exe files programmatically?

2) If I don't use those dependencies, can I make gradle to do the work of downloading webdrivers for all the browsers, put them in a folder, and set all the required system properties?

Thanks in advance.

1 Answers

1) I let Gradle set the system property to the location of the current webdriver files. I need them in my tests, so I set the properties before my test task.

test.doFirst{
    def classpathRoots = project.sourceSets.test.runtimeClasspath.files
    systemProperty 'webdriver.gecko.driver', classpathRoots.find { it.name.startsWith('geckodriver') }
    systemProperty 'webdriver.chrome.driver', classpathRoots.find { it.name.startsWith('chromedriver') }
    systemProperty 'webdriver.edge.driver', classpathRoots.find { it.name.startsWith('microsoftwebdriver') }
}
Related