如何在带有Maven的Chrome中运行Selenium WebDriver测试用例? [英] How to run Selenium WebDriver test cases in Chrome with Maven?

查看:244
本文介绍了如何在带有Maven的Chrome中运行Selenium WebDriver测试用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用带有Maven的ChromeDriver创建简单的自动测试.

I need to create simple autotest using ChromeDriver with Maven.

pom.xml的摘录:

excerpt from pom.xml:

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

测试案例:

@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");
}

在运行测试执行命令后

mvn -test

我收到以下异常:

java.lang.IllegalStateException:必须通过webdriver.chrome.driver系统属性设置驱动程序可执行文件的路径;有关更多信息,请参见 https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver.可以从 http://chromedriver.storage.googleapis.com/index.html下载最新版本. 在com.google.common.base.Preconditions.checkState(Preconditions.java:199) 在org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109) 位于org.openqa.selenium.chrome.ChromeDriverService.access $ 000(ChromeDriverService.java:32) 在org.openqa.selenium.chrome.ChromeDriverService $ Builder.findDefaultExecutable(ChromeDriverService.java:137) 在org.openqa.selenium.remote.service.DriverService $ Builder.build(DriverService.java:296) 在org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88) 在org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:116) 在com.testTask.GoogleTest.StartBrowser_NavURL(GoogleTest.java:26) 在org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:77) 在org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:110) 在org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:106) 在org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) 在org.apache.maven.surefire.booter.ProviderFactory $ ProviderProxy.invoke(ProviderFactory.java:165) 在org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) 在org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) 在org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75) ...删除了23个堆叠框架

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

我已通过以下链接阅读了有关讨论的内容: 如何在Chrome中运行Selenium WebDriver测试用例?

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

但是我无法在服务器上下载可执行文件.因此,这不是我的选择. 但是Maven在服务器上下载了"selenium-chrome-driver-2.53.1.jar"(对我来说还可以).

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).

有没有办法使用下载的.jar文件而不是可执行文件?

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

P.S.对于这个项目,我使用IntelliJ Idea社区版,但我不是专家

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

推荐答案

对于抛出的异常,您有一个答案.只需在初始化驱动程序之前将路径设置为可执行chrome驱动程序即可.

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")

您可以从下面的链接下载chrome驱动程序可执行文件,并将其放在所需的位置:

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

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

如果您不想手动下载chrome驱动程序,则可以添加这样的依赖项.

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>

这将下载最新版本的驱动程序,并使用以下命令设置适当的java系统变量:

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

ChromeDriverManager.getInstance().setup();

这篇关于如何在带有Maven的Chrome中运行Selenium WebDriver测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆