How to run Selenium WebDriver test cases in Chrome

2019-09-13 01:36发布

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.testng.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?

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

2条回答
Ridiculous、
2楼-- · 2019-09-13 02:05

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/

查看更多
姐就是有狂的资本
3楼-- · 2019-09-13 02:10

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();
查看更多
登录 后发表回答