jar created by Gradle cant find webdriver

Keker :

I want to build an executable jar that uses selenium. I am trying to do this using shadowJar with the dependencies of selenium and selenium chrome driver. build.gradle:

group 'selenium.test'
version '1.0'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1'
    }
}

apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}
jar{
    manifest{
        attributes 'Main-Class': 'Test'
    }
from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}
dependencies {
compile group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '3.6.2'
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: '3.141.59'
testCompile group: 'junit', name: 'junit', version: '4.12'
}

But when I try to execute the resulting jar I get the following error

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;

Why does this happen despite the indicated chromewebdriver dependency?

main:

public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        System.out.println(driver.getTitle());
    }
AndiCover :

The error message means that you need to set the location of your chromedriver executable as system property. You can do this manually or automated with the library io.github.bonigarcia.webdrivermanager which is already in your dependencies.


WebDriverManager

Add following line to your main method before creating an instance of the ChromeDriver.

WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();

This works similar for other drivers as well.

Manual

The manual method would look like following:

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327667&siteId=1