在无头HtmlUnitDriver模式下执行测试,通过Eclipse IDE,但在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);  // 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中项目的结构

enter image description here

这是我导出到JAR文件的方式

enter image description here

我已将此文件放在apache-jmeter-5.3\lib\junit下,并且可以在JUnit Request Samplers中正确看到该脚本中的类和方法

enter image description here

当我在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

解决方法

您缺少一个重要步骤:您的.jar文件仅包含您的代码,并且不包含htmlunit-driverselenium-java等。因此,我的期望是如果您查看{{3 }},您会发现JMeter找不到与硒相关的类。

快速而肮脏的解决方案将执行jmeter.log file,一旦完成,将所有内容从项目的target/dependencies文件夹复制到JMeter安装的{lib“文件夹(或{{3中的其他位置) }})

重新启动后,您应该会看到测试工作正常。

更好的选择是mvn dependency:copy-dependencies command,其中包含运行测试所需的所有内容

最后但并非最不重要的一点是,您可能会发现JMeter Classpath更加易于使用

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...