FAILED CONFIGURATION:@BeforeTest beforeTest。 MAVEN; TestNG,Selenium

时间:2017-10-05 06:49:06

标签: java eclipse maven selenium

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

[RemoteTestNG]检测到TestNG版本6.11.0 FAILED CONFIGURATION:@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)     at test.java.NewTest.beforeTest(NewTest.java:27)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:498)     at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)     at 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.run顺序(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)     at 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)     at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)     在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)     在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

SKIPPED CONFIGURATION:@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)     at test.java.NewTest.beforeTest(NewTest.java:27)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:498)     at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)     at 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.run顺序(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)     at 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)     at 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>

1 个答案:

答案 0 :(得分:0)

您需要在@beforeTest方法中设置属性。它将在@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();          
}       

}