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

配置失败:@beforeTest beforeTest。Maven;硒试验

任飞鸣
2023-03-14

跳过的配置:@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.chromeddriverService.access$000(chromedriverService.java:32),地址为der.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.Invoke 0(原生方法)在在java.lang.reflect.method.invokeMethod(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)在unner.run(testrunner.java:594)在org.testng.suiterunner.runtest(Suiterunner.java:380)在org.testng.suiterunner.runsequential(Suiterunner.java:375)在org.testng.suiterunner.privaterun(Suiterunner.java:340)在org.testng.suiterunner.privaterun(Suiterunner.java:289)在suitesSequentury(testng.java:1301)在org.testng.testng.runsuitesLocalic(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.java:114)在org.testng.remote.remotetestng.initandrun(remotetestng.java:114)在org.testng.remote.remotetestng.initandrun(remotetestng.java:251)在

===========================================================================================================================

=============================================================================================================================

我有以下代码:

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 html" target="_blank">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个答案

华升
2023-03-14

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

}
 类似资料:
  • 我开始阅读Unraveling AngularJS1.5以学习与Angular相关的知识,在本书的开头,作者最喜欢安装node,我做了这件事,当我在cmd上运行node时,它显示我已经安装了node,然后他说要转到第01章,我用cd命令完成了这件事,并编写npm install来安装package.json在这一步中,我得到了如下警告: npm WARN package.json unraveli

  • 我有一个Selenium Python测试来检查一个网站是否有效工作。 当页面加载时,开始和结束日期将以这种格式显示,并且按钮将变灰,直到设置正确的日期格式(如屏幕截图所示) 我的硒代码:

  • 直到几天前,我的测试还可以。现在他们失败了,但有一个例外: 来自未知错误:找不到页面:chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html(会话信息:chrome=58.0.3029.14)(驱动程序信息:chromedriver=2.26.436362(5476ec6bf7ccbad

  • 运行我能想到的最基本的硒测试。 我在使用, Chrome版本52.0.2743.82M ChromeDriver 2.22 Selenium WebDriver 2.53.1 Selenium WebDriver支持类2.53.1 Win10(最近升级) 我已经尝试卸载和重新安装chrome和删除用户配置文件。 我得到的错误是, 测试名称:TestMethod1测试全名:UnitTestProje

  • 我在使用硒测试时收到此错误 我现在的代码是 是否有其他方法可以忽略/解决此错误?