当前位置: 首页 > 知识库问答 >
问题:

Maven测试不使用Selenium Webdriver打开Chrome浏览器

盖雪峰
2023-03-14

我已经用TestNG for selenium webdriver(Chrome浏览器)创建了一个Maven项目。我用TestNG运行了一个基本测试,Chrome浏览器成功打开。Buf如果我用“Maven test”命令运行相同的测试,我会得到一条错误消息。

你能帮忙吗?我尝试了许多解决方案,如堆栈溢出所建议的,但仍然不成功:(

我已经创建了testng.xml文件

我已经为Selenium-Java和Selenium-Server依赖项以及Chromedriver和SureFire插件使用了不同的版本,但问题仍然存在,现在我对所有这些都使用了最新的版本(更多细节请参见下面的POM文件)。

我已经卸载和重新安装了谷歌Chrome,现在我有了最新的73版

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.

共有1个答案

习高格
2023-03-14

欢迎光临。

org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start. 

这表示ChromeDriver无法启动新的chrome浏览器会话。

请将您的java更新到最新版本(根据消息,我们可以看到您正在运行java.version:'1.8.0_201',这是较旧的版本,请考虑更新到最新的java版本11.0)

    null

考虑直接执行一个@test以确保环境良好,然后使用testng.xml进行进一步的执行。

 类似资料: