更新时间:2023-10-20 14:43:46
下面是可能的方式来从JMeter的运行Selenium测试情况:
Below are possible ways to run Selenium test-cases from JMeter:
结果
运行Selenium测试这种方法也许有用的,如果你想重新使用已经自动化(Java)的硒方案而不是重新编写JS-脚本的的webdriver取样。
Running Selenium tests this way maybe useful if you want to re-use already automated (Java) Selenium scenarios instead of re-writing JS-scripts for WebDriver Sampler.
prepare Selenium测试项目和设置。
Prepare Selenium test project and setup.
1.1。硒下载Java客户端库,并把硒爪哇 - $ {}版本的.jar
来JMeter的classpath中,例如%JMETER_HOME%/ lib目录/
。结果
1.2。硒服务器应达和听力:
1.1. Download Selenium Java client libraries and put selenium-java-${version}.jar
to JMeter classpath, e.g. %JMETER_HOME%/lib/
.
1.2. Selenium server should be up and listening:
java -jar selenium-server-standalone-${version}.jar
1.3。出口硒测试计划,.jar和将它保存到%JMETER_HOME%/ lib中/ JUnit的/
。
注意:您的测试类应该扩展测试用例
或 SeleneseTestCase
允许JMeter的挑了这个测试计划,测试用例的名字应以测试)开始。结果
注意:默认 SeleneseTestCase
扩展了JUnit 3.x的测试用例
,也 SeleneseTestCase
预计外部硒服务器运行着。
NOTE: Your test class should extend TestCase
or SeleneseTestCase
to allow JMeter pick up this test plan, test case's name should start with "test").
NOTE: By default SeleneseTestCase
extends JUnit 3.x TestCase
, also SeleneseTestCase
expects external Selenium server to be running.
2.1。在JMeter的测试计划将 JUnit的请求采样。结果类名
据一位来自Selenium测试计划中设置。结果
设置测试方法
来测试即将耗尽。结果
默认情况下,保留其他参数。
2.1. In JMeter test-plan add JUnit Request sampler.
Set Class Name
according to one from the Selenium test plan.
Set Test Method
to test that is about to run.
Leave other parameters by default.
的JUnit 3.x中主场迎战4.x版结果
JUnit的请求采样可以同时处理JUnit3-和JUnit4风格的类和方法。要设置取样搜索JUnit 4测试( @Test
注释)检查搜索Junit4注释(而不是JUnit 3中)
在设置复选框以上。结果
下面JUnit4注解确认:
JUnit 3.x vs. 4.x
JUnit Request Sampler can process both JUnit3- and JUnit4-style classes and methods. To set Sampler to search for JUnit 4 tests (@Test
annotations) check Search for Junit4 annotations (instead of JUnit 3)
checkbox in settings above.
The following JUnit4 annotations are recognized:
@Test - 用于查找试验方法和类。 预期和超时属性的支持。结果
@Before - 一样对待设置()在JUnit3结果
@After - 一样对待拆解()在JUnit3结果
@BeforeClass,@AfterClass - 视为测试方法,使他们可独立为需要运行
@Test - used to find test methods and classes. The "expected" and "timeout" attributes are supported.
@Before - treated the same as setUp() in JUnit3
@After - treated the same as tearDown() in JUnit3
@BeforeClass, @AfterClass - treated as test methods so they can be run independently as required
您已经准备好开始使用JMeter您的Selenium测试。
You are ready to start your Selenium test with JMeter.
Java的code为JUnit的请求采样:
Java code for JUnit Request sampler:
的JUnit 3.X
package com.example.tests;
import com.thoughtworks.selenium.*;
public class selenium extends SeleneseTestCase {
private static Selenium selenium;
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
selenium.start();
selenium.windowMaximize();
}
public void testSelenium() throws Exception {
selenium.open("/");
selenium.waitForPageToLoad("30000");
Assert.assertEquals("Google", selenium.getTitle());
}
public void tearDown() throws Exception {
selenium.close();
}
}
JUnit的4.x版
写在JUnit 4测试脚本使用JUnit标注:
Test script written in JUnit 4 uses JUnit annotations:
package com.example.tests;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class selenium extends SeleneseTestCase {
private static Selenium selenium;
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
selenium.start();
selenium.windowMaximize();
}
@Test
public void testSelenium() throws Exception {
selenium.open("/");
selenium.waitForPageToLoad("30000");
Assert.assertEquals("Google", selenium.getTitle());
}
@After
public void tearDown() throws Exception {
selenium.stop();
}
}
这情况下另一个答案提到的webdriver采样的替代品。
prerequisites
使用Selenium RC情况下,唯一的区别是硒设置preparation:
The only difference with Selenium RC case is Selenium setup preparation:
1.1。下载并把硒的服务器单机 - $ {}版本的.jar
来JMeter的classpath中,例如%JMETER_HOME%/ lib目录/
。结果
注意:有没有必要启动Selenium服务器
1.1. Download and put selenium-server-standalone-${version}.jar
to JMeter classpath, e.g. %JMETER_HOME%/lib/
.
NOTE: There is no need to start the Selenium server.
所有其它步骤是相同的如上述的情形。
All the other steps are the same as in the scenario described above.
package org.openqa.selenium.example;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class selenium extends TestCase {
public static WebDriver driver;
@Before
public void setUp() {
FirefoxProfile profile = new FirefoxProfile();
driver = new FirefoxDriver(profile);
}
@Test
public void testSelenium() throws Exception {
driver.get("http://www.google.com/");
Assert.assertEquals("Google", driver.getTitle());
}
@After
public void tearDown() {
driver.quit();
}
}
UPD。
另一个好点,一步一步的指导使用硒+的JUnit + JMeter的包:
Another good points and step-by-step guides to use Selenium + JUnit + JMeter bundle:
在这种情况下,硒的测试场景中JMeter的 BeanShell的采样。
In this case selenium test-scenario is executed directly in JMeter's BeanShell Sampler.
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
Boolean result = true;
try {
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com/");
selenium.start();
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("30000");
if (!selenium.isTextPresent("Google")) result = false;
} catch (Exception ex) {
ex.printStackTrace();
IsSuccess = false;
ResponseCode = "500";
ResponseMessage = ex.getMessage();
} finally {
selenium.stop();
}
IsSuccess = result;
return result;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
Boolean result = true;
try {
driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("http://www.google.com/");
if (!driver.getTitle().contains("Google")) result = false;
} catch (Exception ex) {
ex.printStackTrace();
IsSuccess = false;
ResponseCode = "500";
ResponseMessage = ex.getMessage();
} finally {
driver.quit();
}
IsSuccess = result;
return result;
在这种情况下,硒的测试场景是通过 JSR223采样 +的 Groovy的结果。
对于performance考虑这种做法似乎更加preferable比使用上述BeanShell的采样。
In this case selenium test-scenario is executed via JSR223 Sampler + Groovy.
For performance considerations this approach seems to be more preferable than using BeanShell Sampler described above.
添加JSR223采样Groovy的支持:
2.1。 下载最新的Groovy 二进制分发;结果
2.2。复制常规 - 所有 - $ {VERSION}的.jar
从分布的嵌入文件夹并将其拖放到%JMETER_HOME%/ lib目录/
;结果
2.3。重启JMeter的。
2.1. download latest Groovy binary distribution;
2.2. copy groovy-all-${VERSION}.jar
from "embeddable" folder of distribution and drop it to %JMETER_HOME%/lib/
;
2.3. restart JMeter.
配置JSR233采样:
Configure JSR233 Sampler:
3.1。添加JSR233采样主题小组;结果
3.2。设置脚本语言
到采样器的设置常规
;结果
3.3。把你的硒测试场景为剧本
部分(Java的code将被接受):
3.1. add JSR233 Sampler to Thread Group;
3.2. set Script Language
to groovy
in sampler's settings;
3.3. put your selenium test-scenario into Script
section (Java code will be accepted):
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
Boolean result = true;
try {
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com/");
selenium.start();
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("30000");
if (!selenium.isTextPresent("Google")) result = false;
} catch (Exception ex) {
ex.printStackTrace();
log.error(ex.getMessage());
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage(ex.getMessage());
} finally {
selenium.stop();
}
SampleResult.setSuccessful(result);
return result;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
Boolean result = true;
try {
driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get("http://www.google.com/");
if (!driver.getTitle().contains("Google")) result = false;
} catch (Exception ex) {
ex.printStackTrace();
log.error(ex.getMessage());
SampleResult.setSuccessful(false);
SampleResult.setResponseCode("500");
SampleResult.setResponseMessage(ex.getMessage());
} finally {
driver.quit();
}
SampleResult.setSuccessful(result);
return result;
有关的BeanShell / JSR223采样情况下的常见注意事项:
脚本文件
字段)的文件。 IsSuccess = STATUS
或 SampleResult.setSuccessful(STATUS)
,见code以上),而无需使用响应断言。Script file
field) instead of using Beanshell / Groovy code directly in sampler for intensive testing.IsSuccess = STATUS
or SampleResult.setSuccessful(STATUS)
, see code above), without using Response Assertion.