以下是从 JMeter 运行 Selenium 测试用例的可能方法:
- using JUnit 请求采样器;
- using BeanShell 采样器;
- using JSR223采样器 + Groovy.
JUnit 请求采样器
如果您想重用已经自动化的 (Java) Selenium 场景而不是重新编写 JS 脚本,那么以这种方式运行 Selenium 测试可能很有用。WebDriver 采样器.
硒RC
-
准备 Selenium 测试项目和设置。
1.1.下载 Selenium Java 客户端库并放入selenium-java-${version}.jar
到 JMeter 类路径,例如%JMETER_HOME%/lib/
.
1.2. Selenium 服务器应该启动并监听:
java -jar selenium-server-standalone-${version}.jar
1.3.将 Selenium 测试计划导出为 .jar 并将其保存到%JMETER_HOME%/lib/junit/
.
NOTE:你的测试课程应该延长TestCase
or SeleneseTestCase
允许 JMeter 选择此测试计划、测试用例的名称应该以“测试”开头).
NOTE:默认情况下SeleneseTestCase
扩展 JUnit 3.xTestCase
, also SeleneseTestCase
期望外部 Selenium 服务器正在运行。
-
配置JUnit 请求采样器
2.1.在 JMeter 测试计划中添加JUnit 请求采样器.
Set Class Name
根据 Selenium 测试计划中的一项。
Set Test Method
测试即将运行的内容。
其他参数保留默认即可。
JUnit 3.x 与 4.x
JUnit Request Sampler 可以处理 JUnit3 和 JUnit4 样式的类和方法。设置 Sampler 来搜索 JUnit 4 测试(@Test
注释)检查Search for Junit4 annotations (instead of JUnit 3)
上面设置中的复选框。
可以识别以下 JUnit4 注释:
@Test - 用于查找测试方法和类。支持“expected”和“timeout”属性。
@Before - 与 JUnit3 中的 setUp() 处理相同
@After - 与 JUnit3 中的tearDown() 处理方式相同
@BeforeClass、@AfterClass - 被视为测试方法,以便它们可以根据需要独立运行
您已准备好使用 JMeter 开始 Selenium 测试。
JUnit 请求采样器的 Java 代码:
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 注释:
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 采样器下面另一个答案提到过。
先决条件
与 Selenium RC 案例的唯一区别是 Selenium 设置准备:
1.1.下载并放置selenium-server-standalone-${version}.jar
到 JMeter 类路径,例如%JMETER_HOME%/lib/
.
NOTE:无需启动 Selenium 服务器。
所有其他步骤与上述场景中的相同。
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.
使用 Selenium + JUnit + JMeter 捆绑包的另一个优点和分步指南:
- 将 Selenium 与 JMeter 集成以进行负载测试
- 将 Jmeter 与 Selenium 代码集成
- 使用 Selenium 和 JMeter 进行性能测试
- 在 JMeter 下运行 Selenium 测试
- 如何将 JUnit4 – Webdriver 测试集成到 JMeter 中
BeanShell 采样器
在这种情况下,selenium 测试场景直接在 JMeter 中执行BeanShell 采样器.
- Selenium 设置准备工作与上述情况完全相同:下载 Selenium 库,放入 JMeter 类路径,启动 Selenium 服务器(在 Selenium RC 的情况下)。
- 将您的硒测试场景放入 BeanShell Sampler 中:
硒RC
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
在这种情况下,selenium 测试场景是通过执行的JSR223采样器 + Groovy.
For 性能考虑这种方法似乎比使用上面描述的 BeanShell Sampler 更可取。
- Selenium 设置准备工作与上述情况完全相同:下载 Selenium 库,放入 JMeter 类路径,启动 Selenium 服务器(在 Selenium RC 的情况下)。
-
添加对 JSR223 Sampler 的 Groovy 支持:
2.1. 下载最新的 Groovy二进制分布;
2.2.复制groovy-all-${VERSION}.jar
从发行版的“embeddable”文件夹并将其拖放到%JMETER_HOME%/lib/
;
2.3.重新启动 JMeter。
-
配置 JSR233 采样器:
3.1.将 JSR233 Sampler 添加到线程组;
3.2.放Script Language
to groovy
在采样器的设置中;
3.3.将您的硒测试场景放入Script
部分(Java 代码将被接受):
硒RC
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 Sampler 案例的常见注释:
- 将外部 .bsh / .groovy 文件与测试场景一起使用(
Script file
字段),而不是直接在采样器中使用 Beanshell / Groovy 代码进行密集测试。
- 由于 BeanShell / JSR233 采样器可以访问 JMeter 的变量,因此您可以直接在测试场景中设置测试(=采样器执行)状态(例如通过
IsSuccess = STATUS
or SampleResult.setSuccessful(STATUS)
,参见上面的代码),而不使用响应断言。