Maven Test无法使用Selenium Webdriver打开Chrome浏览器

时间:2019-03-31 00:17:04

标签: google-chrome selenium-webdriver testng maven-3

我已经用TestNG为硒Webdriver(Chrome浏览器)创建了一个Maven项目。我使用TestNG运行了基本测试,并且Chrome浏览器已成功打开。 Buf,如果我使用“ Maven Test”命令运行相同的测试,但收到错误消息。

能请你帮忙吗?我已经尝试了很多解决方案,如Stack Overflow所建议的,但仍然不成功:(

  • 我为基本测试“ Hello World”运行“ Maven测试”,并且构建成功>>>“ Maven Test”创建成功的证据

  • 我使用打开Chrome浏览器的命令运行TestNG测试,并且可以正常运行>>>证明TestNG成功运行

  • 我使用打开Chrome浏览器的命令运行“ Maven测试”,但它不起作用>>>对此感到沮丧,尝试了所有出现在StackOverflow中但未成功的解决方案

  • 我已经创建了testng.xml文件

  • 我对Selenium-Java和Selenium-Server依赖项以及Chromedriver和SureFire插件使用了不同的版本,但问题仍然存在,现在我对所有版本使用最新版本(请参阅下面的POM文件)有关更多详细信息)。

  • 我已经卸载并重新安装了Google 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 个答案:

答案 0 :(得分:0)

欢迎来到。

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进行进一步的执行。