Executing a test on Headless (HtmlUnitDriver) mode, passes on Eclipse IDE but fails when executed in JMeter

huangapple go评论100阅读模式
英文:

Executing a test on Headless (HtmlUnitDriver) mode, passes on Eclipse IDE but fails when executed in JMeter

问题

以下是翻译好的部分:

  1. 我在Eclipse IDE中编写了一个简单的java-selenium-maven脚本将其导出为JAR文件并尝试从JMeter中执行
  2. 我有两个版本的这个脚本一个是普通的`ChromeDriver`另一个是无头的它使用了`HtmlUnitDriver`
  3. 这里是第二个版本因为它是出问题的那个
  4. package testing1;
  5. import org.junit.After;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.WebDriver;
  10. import org.openqa.selenium.WebElement;
  11. import org.openqa.selenium.htmlunit.HtmlUnitDriver;
  12. public class NewTestHeadless {
  13. @Test
  14. public void testGoogleSearch() throws InterruptedException {
  15. WebDriver driver = new HtmlUnitDriver();
  16. driver.get("http://www.google.com/");
  17. Thread.sleep(5000);
  18. WebElement searchBox = driver.findElement(By.name("q"));
  19. searchBox.sendKeys("ChromeDriver");
  20. searchBox.submit();
  21. Thread.sleep(5000);
  22. driver.quit();
  23. }
  24. @Before
  25. public void beforeT() {
  26. System.out.println("BEFOREEEE");
  27. }
  28. @After
  29. public void afterT() {
  30. System.out.println("AFTEERRRR");
  31. }
  32. }

这是我的 pom.xml 文件:

  1. <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">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>selenium.example</groupId>
  4. <artifactId>testing-example-selenium</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <name>testing</name>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.seleniumhq.selenium</groupId>
  10. <artifactId>selenium-java</artifactId>
  11. <version>4.0.0-alpha-2</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.seleniumhq.selenium</groupId>
  15. <artifactId>selenium-server</artifactId>
  16. <version>4.0.0-alpha-2</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>junit</groupId>
  20. <artifactId>junit</artifactId>
  21. <version>4.11</version>
  22. <scope>test</scope>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.seleniumhq.selenium</groupId>
  26. <artifactId>htmlunit-driver</artifactId>
  27. <version>2.43.1</version>
  28. </dependency>
  29. </dependencies>
  30. </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

  1. 请注意,我只提供了代码和XML配置的翻译,其他的问题和内容我都没有回答。
  2. <details>
  3. <summary>英文:</summary>
  4. 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.
  5. I have two versions of this script, one is with normal `ChromeDriver` and the other one is headless and it uses the `HtmlUnitDriver`.
  6. Here is the second one as it is the one that misbehaves:
  7. package testing1;
  8. import org.junit.After;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import org.openqa.selenium.By;
  12. import org.openqa.selenium.WebDriver;
  13. import org.openqa.selenium.WebElement;
  14. import org.openqa.selenium.htmlunit.HtmlUnitDriver;
  15. public class NewTestHeadless {
  16. @Test
  17. public void testGoogleSearch() throws InterruptedException {
  18. WebDriver driver = new HtmlUnitDriver();
  19. driver.get(&quot;http://www.google.com/&quot;);
  20. Thread.sleep(5000); // Let the user actually see something!
  21. WebElement searchBox = driver.findElement(By.name(&quot;q&quot;));
  22. searchBox.sendKeys(&quot;ChromeDriver&quot;);
  23. searchBox.submit();
  24. Thread.sleep(5000); // Let the user actually see something!
  25. driver.quit();
  26. }
  27. @Before
  28. public void beforeT() {
  29. System.out.println(&quot;BEFOREEEE&quot;);
  30. }
  31. @After
  32. public void afterT() {
  33. System.out.println(&quot;AFTEERRRR&quot;);
  34. }
  35. }
  36. This is my `pom.xml` file:
  37. &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
  38. &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  39. &lt;groupId&gt;selenium.example&lt;/groupId&gt;
  40. &lt;artifactId&gt;testing-example-selenium&lt;/artifactId&gt;
  41. &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  42. &lt;name&gt;testing&lt;/name&gt;
  43. &lt;dependencies&gt;
  44. &lt;dependency&gt;
  45. &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
  46. &lt;artifactId&gt;selenium-java&lt;/artifactId&gt;
  47. &lt;version&gt;4.0.0-alpha-2&lt;/version&gt;
  48. &lt;/dependency&gt;
  49. &lt;dependency&gt;
  50. &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
  51. &lt;artifactId&gt;selenium-server&lt;/artifactId&gt;
  52. &lt;version&gt;4.0.0-alpha-2&lt;/version&gt;
  53. &lt;/dependency&gt;
  54. &lt;dependency&gt;
  55. &lt;groupId&gt;junit&lt;/groupId&gt;
  56. &lt;artifactId&gt;junit&lt;/artifactId&gt;
  57. &lt;version&gt;4.11&lt;/version&gt;
  58. &lt;scope&gt;test&lt;/scope&gt;
  59. &lt;/dependency&gt;
  60. &lt;dependency&gt;
  61. &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
  62. &lt;artifactId&gt;htmlunit-driver&lt;/artifactId&gt;
  63. &lt;version&gt;2.43.1&lt;/version&gt;
  64. &lt;/dependency&gt;
  65. &lt;/dependencies&gt;
  66. &lt;/project&gt;
  67. And this is the structure of my project in Eclipse IDE
  68. [![enter image description here][1]][1]
  69. Here is how i exported to JAR file
  70. [![enter image description here][2]][2]
  71. 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
  72. [![enter image description here][3]][3]
  73. When i execute in Eclipse, both test (headless + chrome) passes but when i execute from JMeter, the headless one fails
  74. Any ideas of what might be the problem?
  75. This is the sampler response in the Results Tree listener and the response is empty:
  76. Thread Name:Scenario 27 - Selenium JUnit 5-1
  77. Sample Start:2020-08-25 12:26:16 EEST
  78. Load time:980
  79. Connect Time:0
  80. Latency:0
  81. Size in bytes:0
  82. Sent bytes:0
  83. Headers size in bytes:0
  84. Body size in bytes:0
  85. Sample Count:1
  86. Error Count:1
  87. Data type (&quot;text&quot;|&quot;bin&quot;|&quot;&quot;):text
  88. Response code:1000
  89. Response message:
  90. SampleResult fields:
  91. ContentType:
  92. DataEncoding: windows-1252
  93. [1]: https://i.stack.imgur.com/NYqxX.png
  94. [2]: https://i.stack.imgur.com/MXOns.png
  95. [3]: https://i.stack.imgur.com/Hkezp.png
  96. </details>
  97. # 答案1
  98. **得分**: 1
  99. 你漏掉了一个重要的步骤:你的.jar文件只包含了你的代码,没有包含`htmlunit-driver``selenium-java`等内容,所以我预期如果你查看[jmeter.log文件][1],你会看到JMeter找不到与Selenium相关的类。
  100. 一个简单粗暴的解决方案是执行[`mvn dependency:copy-dependencies`命令][2],一旦完成,将项目的`target/dependencies`文件夹中的所有内容复制到你的JMeter安装目录下的“lib”文件夹中(或[JMeter类路径][3]中的其他位置)。
  101. 重新启动后,你应该能看到你的测试在工作。
  102. 一个更好的选择是[使用Maven Shade插件创建“uber jar”][4],其中包含了运行你的测试所需的所有内容。
  103. 最后但同样重要的是,你可能会发现[JMeter WebDriver采样器][5]更容易使用。
  104. [1]: https://jmeter.apache.org/usermanual/get-started.html#logging
  105. [2]: https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html
  106. [3]: https://jmeter.apache.org/usermanual/get-started.html#classpath
  107. [4]: https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
  108. [5]: https://www.blazemeter.com/blog/jmeter-webdriver-sampler/
  109. <details>
  110. <summary>英文:</summary>
  111. You&#39;re missing one important step: your .jar file has only your code and doesn&#39;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.
  112. 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 &quot;lib&quot; folder of your JMeter installation (or other place in [JMeter Classpath][3])
  113. After restart you should see your test working.
  114. A better option would be [using Maven Shade plugin for creating a &quot;uber jar&quot;][4] containing everyting which is needed for running your test
  115. And last but not the least you may find [JMeter WebDriver Sampler][5] much easier to use
  116. [1]: https://jmeter.apache.org/usermanual/get-started.html#logging
  117. [2]: https://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html
  118. [3]: https://jmeter.apache.org/usermanual/get-started.html#classpath
  119. [4]: https://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
  120. [5]: https://www.blazemeter.com/blog/jmeter-webdriver-sampler/
  121. </details>

huangapple
  • 本文由 发表于 2020年8月25日 17:32:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63575940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定