英文:
Executing a test on Headless (HtmlUnitDriver) mode, passes on Eclipse IDE but fails when executed in JMeter
问题
以下是翻译好的部分:
我在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);
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
Thread.sleep(5000);
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 Request Samplers中正确看到该脚本的类和方法。
当我在Eclipse中执行时,两个测试(无头 + Chrome)都通过,但当我从JMeter中执行时,无头测试失败。
有关可能问题的任何想法吗?
这是Results Tree监听器中的采样器响应,响应为空:
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
请注意,我只提供了代码和XML配置的翻译,其他的问题和内容我都没有回答。
<details>
<summary>英文:</summary>
I have a simple java-selenium-maven script written in Eclipse IDE which I have exported in a JAR file and I tried to execute from JMeter.
I have two versions of this script, one is with normal `ChromeDriver` and the other one is headless and it uses the `HtmlUnitDriver`.
Here is the second one as it is the one that misbehaves:
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");
}
}
This is my `pom.xml` file:
<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>
And this is the structure of my project in Eclipse IDE
[![enter image description here][1]][1]
Here is how i exported to JAR file
[![enter image description here][2]][2]
I have put this file under `apache-jmeter-5.3\lib\junit` and I can see correctly in JUnit Request Samplers the classes and methods from that script
[![enter image description here][3]][3]
When i execute in Eclipse, both test (headless + chrome) passes but when i execute from JMeter, the headless one fails
Any ideas of what might be the problem?
This is the sampler response in the Results Tree listener and the response is empty:
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
[1]: https://i.stack.imgur.com/NYqxX.png
[2]: https://i.stack.imgur.com/MXOns.png
[3]: https://i.stack.imgur.com/Hkezp.png
</details>
# 答案1
**得分**: 1
你漏掉了一个重要的步骤:你的.jar文件只包含了你的代码,没有包含`htmlunit-driver`、`selenium-java`等内容,所以我预期如果你查看[jmeter.log文件][1],你会看到JMeter找不到与Selenium相关的类。
一个简单粗暴的解决方案是执行[`mvn dependency:copy-dependencies`命令][2],一旦完成,将项目的`target/dependencies`文件夹中的所有内容复制到你的JMeter安装目录下的“lib”文件夹中(或[JMeter类路径][3]中的其他位置)。
重新启动后,你应该能看到你的测试在工作。
一个更好的选择是[使用Maven Shade插件创建“uber jar”][4],其中包含了运行你的测试所需的所有内容。
最后但同样重要的是,你可能会发现[JMeter WebDriver采样器][5]更容易使用。
[1]: https://jmeter.apache.org/usermanual/get-started.html#logging
[2]: https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html
[3]: https://jmeter.apache.org/usermanual/get-started.html#classpath
[4]: https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
[5]: https://www.blazemeter.com/blog/jmeter-webdriver-sampler/
<details>
<summary>英文:</summary>
You're missing one important step: your .jar file has only your code and doesn't contain `htmlunit-driver`, `selenium-java`, etc. so my expectation is that if you look into [jmeter.log file][1] you will see that JMeter cannot find Selenium-related classes.
Quick and dirty solution would be executing [`mvn dependency:copy-dependencies` command][2] and once it is done copy everything from `target/dependencies` folder of your project to the "lib" folder of your JMeter installation (or other place in [JMeter Classpath][3])
After restart you should see your test working.
A better option would be [using Maven Shade plugin for creating a "uber jar"][4] containing everyting which is needed for running your test
And last but not the least you may find [JMeter WebDriver Sampler][5] much easier to use
[1]: https://jmeter.apache.org/usermanual/get-started.html#logging
[2]: https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html
[3]: https://jmeter.apache.org/usermanual/get-started.html#classpath
[4]: https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
[5]: https://www.blazemeter.com/blog/jmeter-webdriver-sampler/
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论