【问题标题】:FAILED CONFIGURATION: @BeforeTest beforeTest . MAVEN; TestNG, Selenium配置失败:@BeforeTest beforeTest。马文; TestNG,硒
【发布时间】:2018-03-16 17:02:15
【问题描述】:

您好,我正在尝试在 Eclipse 中使用 Maven 和 TestNG 运行 Selenium 脚本。 首先,我右键单击我的项目并选择 TestNG |转换为 TestNG(testing.xml 已创建)。然后我转到运行配置并创建一个新的启动 TestNGl 并单击运行。运行时出现此错误:

[RemoteTestNG] 检测到 TestNG 版本 6.11.0 配置失败:@BeforeTest beforeTest 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:754) 在 org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124) 在 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:329) 在 org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88) 在 org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:123) 在 test.java.NewTest.beforeTest(NewTest.java:27) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108) 在 org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:523) 在 org.testng.internal.Invoker.invokeConfigurations(Invoker.java:224) 在 org.testng.internal.Invoker.invokeConfigurations(Invoker.java:146) 在 org.testng.TestRunner.beforeRun(TestRunner.java:626) 在 org.testng.TestRunner.run(TestRunner.java:594) 在 org.testng.SuiteRunner.runTest(SuiteRunner.java:380) 在 org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) 在 org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) 在 org.testng.SuiteRunner.run(SuiteRunner.java:289) 在 org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 在 org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 在 org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) 在 org.testng.TestNG.runSuitesLocally(TestNG.java:1226) 在 org.testng.TestNG.runSuites(TestNG.java:1144) 在 org.testng.TestNG.run(TestNG.java:1115) 在 org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) 在 org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) 在 org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

跳过配置:@AfterTest afterTest 跳过:f 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:754) 在 org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124) 在 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:329) 在 org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88) 在 org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:123) 在 test.java.NewTest.beforeTest(NewTest.java:27) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108) 在 org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:523) 在 org.testng.internal.Invoker.invokeConfigurations(Invoker.java:224) 在 org.testng.internal.Invoker.invokeConfigurations(Invoker.java:146) 在 org.testng.TestRunner.beforeRun(TestRunner.java:626) 在 org.testng.TestRunner.run(TestRunner.java:594) 在 org.testng.SuiteRunner.runTest(SuiteRunner.java:380) 在 org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) 在 org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) 在 org.testng.SuiteRunner.run(SuiteRunner.java:289) 在 org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 在 org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 在 org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) 在 org.testng.TestNG.runSuitesLocally(TestNG.java:1226) 在 org.testng.TestNG.runSuites(TestNG.java:1144) 在 org.testng.TestNG.run(TestNG.java:1115) 在 org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114) 在 org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) 在 org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

================================================ 默认测试 测试运行:1,失败:0,跳过:1

配置失败:1,跳过:1

================================================ 默认套件 运行的测试总数:1,失败:0,跳过:1

配置失败:1,跳过:1

我有以下代码:

package test.java;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import junit.framework.Assert;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class NewTest {

private WebDriver driver;

@Test   

public void f() {
    // TODO Auto-generated method stub
    String exePath = "/Users/********/Downloads/chromedriver";
    System.setProperty("webdriver.chrome.driver", exePath);

    //WebDriver driver = new ChromeDriver();

    driver.get("https://www.apple.com/");  
    String title = driver.getTitle();                
    Assert.assertTrue(title.contains("Apple"));         
}

@BeforeTest
public void beforeTest() {  
    driver = new ChromeDriver();  
}       
@AfterTest
public void afterTest() {
    driver.quit();          
}       

}

并遵循 pom.xml:

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>TestMaven</groupId>
<artifactId>TestMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
      <source>1.8</source>
      <target>1.8</target>
    </configuration>
  </plugin>
</plugins>
</build>
<dependencies>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.6.0</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.11</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
</dependency>
</dependencies>
</project>

【问题讨论】:

    标签: java eclipse maven selenium


    【解决方案1】:

    您需要在@beforeTest 方法bec 中设置属性。它会在@Test 之前运行。

    在您的情况下,您试图在设置它的属性之前创建驱动程序实例。

    试试下面的代码:-

      @BeforeTest
        public void beforeTest() {  
           String exePath = "/Users/********/Downloads/chromedriver/chromedriver.exe";
            System.setProperty("webdriver.chrome.driver", exePath);
            driver = new ChromeDriver();  
        } 
    

    package test.java;
    
    import org.testng.annotations.AfterTest;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.Test;
    
    import junit.framework.Assert;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    
    public class NewTest {
    
    private WebDriver driver;
    
    @Test   
    
    public void f() {
    
        driver.get("https://www.apple.com/");  
        String title = driver.getTitle();                
        Assert.assertTrue(title.contains("Apple"));         
    }
    
    @BeforeTest
    public void beforeTest() {  
    String exePath = "/Users/********/Downloads/chromedriver/chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", exePath);
    
        driver = new ChromeDriver();  
    }       
    @AfterTest
    public void afterTest() {
        driver.quit();          
    }       
    
    }
    

    【讨论】:

    • 我尝试过,但得到了同样的错误:FAILED CONFIGURATION: @BeforeTest beforeTest java.lang.IllegalStateException: 驱动程序可执行文件的路径必须由 webdriver.chrome.driver 系统属性设置;
    • String exePath = "/Users/********/Downloads/chromedriver" 你的路径应该是直到 chromedriver.exe.. 参考 syntx String exePath = "/Users/*** *****/Downloads/chromedriver/chromedriver.exe";
    • chromedriver 不是 *.exe 文件。它说“Unix 可执行文件”
    • 我假设您使用的是 Mac OS。您不必为 Chromedriver 可执行文件指定扩展名。在 beforeTest 方法中将您的 exePath 修改为 exePath = "/Users/********/Downloads/chromedriver"。
    【解决方案2】:

    当我从路径中删除 .exe 扩展名时为我工作。例如:

    System.setProperty("webdriver.chrome.driver", "/Users/***/***/chromedriver");
    

    【讨论】:

      猜你喜欢
      • 2015-08-15
      • 2012-01-22
      • 1970-01-01
      • 2012-04-28
      • 2013-12-09
      • 1970-01-01
      • 2021-09-18
      • 2018-11-21
      • 1970-01-01
      相关资源
      最近更新 更多