我已经使用TestNG为selenium webdriver(Chrome浏览器)创建了一个Maven项目。我使用TestNG运行基本测试,Chrome浏览器已成功打开。如果我使用“Maven Test”命令运行相同的测试,我得到一条错误消息。
能否请你帮忙?我尝试了很多解决方案,如Stack Overflow建议,但仍然不成功:(
POM文件出现在下面
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MavenProject</groupId>
<artifactId>MavenProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
testng.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name="MavenPackage.NewTest"/>
</classes>
</test> <!-- Test -->
我的代码如下:
public class NewTest {
WebDriver driver;
@Test
public void MyFunction() {
try {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.get("http://www.ebay.com");
Thread.sleep(3000);
driver.quit();
System.out.println("Test passed");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
错误信息
Starting ChromeDriver 73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72) on port 49053
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 23.798 s <<< FAILURE! - in TestSuite
[ERROR] MyFunction(MavenPackage.NewTest) Time elapsed: 21.874 s <<< FAILURE!
org.openqa.selenium.WebDriverException:
Timed out waiting for driver server to start.
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-DV883K3', ip: '192.168.1.109', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: driver.version: ChromeDriver
at MavenPackage.NewTest.MyFunction(NewTest.java:20)
Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:49053/status] to be available after 20008 ms
at MavenPackage.NewTest.MyFunction(NewTest.java:20)
Caused by: java.util.concurrent.TimeoutException
at MavenPackage.NewTest.MyFunction(NewTest.java:20)
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] NewTest.MyFunction:20 » WebDriver Timed out waiting for driver server to start...
[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 28.467 s
[INFO] Finished at: 2019-03-30T23:32:42Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M3:test (default-test) on project MavenProject: There are test failures.
欢迎来到SO。
org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start.
这表示ChromeDriver无法启动新的Chrome浏览器会话。
请将您的java更新到最新版本(根据消息,我们可以看到您正在运行java.version:'1.8.0_201'哪个更旧,请考虑更新到最新的java版本11.0)
可能的原因:
考虑直接执行单个@Test
以确保环境良好,然后使用testng.xml
进行进一步执行。