我有一个用Eclipse IDE编写的简单的java-selenium-maven脚本,我已经将其导出到一个JAR文件中,并尝试从JMeter执行。
我有这个脚本的两个版本,一个是普通的ChromeDriver,另一个是无头的,它使用HtmlUnitDriver。
下面是第二个,因为它是行为不正常的那个:
package testing1;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class NewTestHeadless {
@Test
public void testGoogleSearch() throws InterruptedException {
WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.google.com/");
Thread.sleep(5000); // Let the user actually see something!
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
Thread.sleep(5000); // Let the user actually see something!
driver.quit();
}
@Before
public void beforeT() {
System.out.println("BEFOREEEE");
}
@After
public void afterT() {
System.out.println("AFTEERRRR");
}
}这是我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>selenium.example</groupId>
<artifactId>testing-example-selenium</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testing</name>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-2</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>4.0.0-alpha-2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<version>2.43.1</version>
</dependency>
</dependencies>
</project>这是我在Eclipse IDE中的项目结构

下面是我如何导出到JAR文件

我已经将这个文件放在apache-jmeter-5.3\lib\junit下,并且我可以在JUnit请求采样器中正确地看到该脚本中的类和方法

当我在Eclipse中执行时,两个测试(无头+ chrome)都通过了,但是当我在JMeter中执行时,无头测试失败了
你知道问题出在哪里吗?
这是结果树监听程序中的采样器响应,响应为空:
Thread Name:Scenario 27 - Selenium JUnit 5-1
Sample Start:2020-08-25 12:26:16 EEST
Load time:980
Connect Time:0
Latency:0
Size in bytes:0
Sent bytes:0
Headers size in bytes:0
Body size in bytes:0
Sample Count:1
Error Count:1
Data type ("text"|"bin"|""):text
Response code:1000
Response message:
SampleResult fields:
ContentType:
DataEncoding: windows-1252发布于 2020-08-25 23:39:48
您遗漏了一个重要的步骤:您的.jar文件只包含您的代码,而不包含htmlunit-driver、selenium-java等,所以我的预期是,如果您查看jmeter.log file,您将看到JMeter无法找到与Selenium相关的类。
快速和肮脏的解决方案是执行mvn dependency:copy-dependencies command,完成后,将项目的target/dependencies文件夹中的所有内容复制到JMeter安装的"lib“文件夹中(或JMeter Classpath中的其他位置)。
重新启动后,您应该会看到您的测试正在工作。
一个更好的选择是using Maven Shade plugin for creating a "uber jar",它包含运行测试所需的所有内容
最后但并非最不重要的一点是,您可能会发现JMeter WebDriver Sampler更易于使用
https://stackoverflow.com/questions/63575940
复制相似问题