简体   繁体   中英

How to run Selenium WebDriver test cases in Chrome with Maven?

I need to create simple autotest using ChromeDriver with Maven.

excerpt from pom.xml:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.53.1</version>
</dependency>

test case:

@BeforeTest
public void StartBrowser_NavURL() {
    driver = new ChromeDriver();
    driver.manage().window().maximize();
}

@AfterTest
public void CloseBrowser() {
    driver.quit();
}

@Test
public void testToCompareDoubles() {
    driver.get("http://www.google.com");
}

And after running test executing command

mvn -test

I receive the following exception:

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver . The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html at com.google.common.base.Preconditions.checkState(Preconditions.java:199) at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109) at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32) at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296) at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:116) at com.testTask.GoogleTest.StartBrowser_NavURL(GoogleTest.java:26) at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:77) at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:110) at org.apache.maven.surefire.te stng.TestNGProvider.invoke(TestNGProvider.java:106) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75) ... Removed 23 stack frames

I've read discussion accesible via the link below: How to run Selenium WebDriver test cases in Chrome?

But I can't download executables on my server. So, it's not an option for me. But Maven downloads "selenium-chrome-driver-2.53.1.jar" on the server (which is OK for me).

Is there a way to use dowloaded .jar file instead of executable?

PS For this project I use IntelliJ Idea Community Edition and I'm not an expert with it

You have an answer in the thrown exception. Just set the path to the executable chrome driver before initializing your driver.

System.setProperty("webdriver.chrome.driver", "path to your chrome driver executable")

You can download chrome driver executable from the below link and put it to desired location:

https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver

Edited:

If you don't want to download the chrome driver manually then add dependency like this.

<dependency>
   <groupId>io.github.bonigarcia</groupId>
   <artifactId>webdrivermanager</artifactId>
   <version>1.4.9</version>
</dependency>

This will download the latest version of driver and set the proper java system variable using the command:

ChromeDriverManager.getInstance().setup();

First download chromedriver.exe file and make sure it is compatible with Selenium Webdriver version.

then you have to setup the path using System.Setproperty as shown in below code

@BeforeTest
public void StartBrowser_NavURL() {
//setup the chromedriver path
System.setProperty("webdriver.chrome.driver", "Path to your chrome driver");
driver = new ChromeDriver();
driver.manage().window().maximize();
}

After this you need configure maven Surefire plugin in order to run maven project through command line.

Link : https://maven.apache.org/surefire/maven-surefire-plugin/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM