我已经准备好了带有功能流的 Selenium 自动化脚本,现在我想将这些脚本与 JMeter 集成以进行负载测试。
那可能吗?
如果是这样,如何整合两者?
我的第一个目标是使用 selenium 运行自动化脚本,而不是在 jmeter 中运行这些脚本进行负载或性能测试。
我已经准备好了带有功能流的 Selenium 自动化脚本,现在我想将这些脚本与 JMeter 集成以进行负载测试。
那可能吗?
如果是这样,如何整合两者?
我的第一个目标是使用 selenium 运行自动化脚本,而不是在 jmeter 中运行这些脚本进行负载或性能测试。
有更简单的方法来运行 Selenium 脚本。
添加此代码
var pkg = JavaImporter(org.openqa.selenium)
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait)
var wait = new support_ui.WebDriverWait(WDS.browser, 5000)
WDS.sampleResult.sampleStart()
WDS.log.info("Opening page...");
WDS.browser.get('http://duckduckgo.com')
var searchField = WDS.browser.findElement(pkg.By.id('search_form_input_homepage'))
searchField.click()
WDS.log.info("Clicked search field")
searchField.sendKeys(['blazemeter'])
WDS.log.info("Inserted blazemeter keyword")
var button = WDS.browser.findElement(pkg.By.id('search_button_homepage'))
button.click()
WDS.log.info("Clicked search button");
var link = WDS.browser.findElement(pkg.By.ByCssSelector('#r1-0 > div.links_main > h2 > a.large > b'))
link.click()
WDS.log.info("Clicked blazemeter link");
WDS.log.info(WDS.name + ' finishing...');
WDS.sampleResult.sampleEnd()
运行你的测试
有关代码语法和最佳实践的更多详细信息,您可以尝试在 JMeter 的 WebDriver Sampler文章中使用 Selenium。
应该不需要将 Selenium 与 JMeter 一起使用。Selenium 脚本一次将采用一个浏览器实例。而 JMeter 不使用浏览器的真实实例来生成负载。
请告诉我是否可以使用 Selenium 脚本从 UI 的角度为 5000 个 vuser 生成负载。大概可以。但是,我们是说 Selenium 脚本现在需要在同一系统上有 5000 个浏览器实例吗?测试会继续运行还是挂起系统?JMeter 已经有很好的选项作为记录器。它从“负载”测试的角度提供了很好的统计数据。
有一段时间,如果我们认为了解 Selenium 的用户不会知道如何在 JMeter 中编写脚本,因此需要学习曲线。但在 JMeter 的情况下,这甚至不是真的。这是因为首先不需要创建逻辑序列或程序之类的东西。
所以基本上你先用 selenium 记录你的脚本,然后使用 jmeter 重新记录 selenium 测试用例。:-)
http://codenaut.blogspot.com/2011/06/icefaces-load-testing.html
虽然接受的答案确实对我有用,但Selenium RC已经过时了。所以这里是更新的Selenium WebDriver方法。
注意:可以从接受的答案中重新使用设置。
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import junit.framework.TestCase;
public class SimpleGoogleSearch extends TestCase {
@Test
public void testSelenium() {
// Replace the following line with the path to your chromedriver.
System.setProperty("webdriver.chrome.driver",
"path to your chrome driver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
WebElement searchField = driver.findElement(By.cssSelector("input[class = 'gLFyf gsfi']"));
searchField.sendKeys("google");
searchField.submit();
assertEquals("google - Google Search", driver.getTitle());
driver.close();
driver.quit();
}
}
以下是从 JMeter 运行 Selenium 测试用例的可能方法:
JUnit 请求采样器
如果您想重用已经自动化的 (Java) Selenium 场景,而不是为WebDriver Sampler重新编写 JS 脚本,以这种方式运行 Selenium 测试可能会很有用。
硒RC
准备 Selenium 测试项目和设置。
1.1. 下载 Selenium Java 客户端库并放入
selenium-java-${version}.jar
JMeter 类路径,例如%JMETER_HOME%/lib/
.1.2. Selenium 服务器应该已启动并正在侦听:
1.3. 将 Selenium 测试计划导出为 .jar 并将其保存到
%JMETER_HOME%/lib/junit/
.注意:你的测试类应该扩展
TestCase
或SeleneseTestCase
允许 JMeter 选择这个测试计划,测试用例的名称应该以 "test" 开头)。注意:默认情况下
SeleneseTestCase
扩展 JUnit 3.xTestCase
,还SeleneseTestCase
需要运行外部 Selenium 服务器。配置JUnit 请求采样器
2.1. 在 JMeter 测试计划中添加JUnit Request sampler。
设置
Class Name
根据一个从Selenium测试计划。设置
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 注释:
您已准备好使用 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 Sampler的替代方案。
先决条件
与 Selenium RC 案例的唯一区别是 Selenium 设置准备:
1.1. 下载并放入
selenium-server-standalone-${version}.jar
JMeter 类路径,例如%JMETER_HOME%/lib/
.注意:无需启动 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(); } }
更新。
使用 Selenium + JUnit + JMeter 包的另一个优点和分步指南:
豆壳采样器
在这种情况下 selenium test-scenario 直接在 JMeter 的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 test-scenario 是通过JSR223 Sampler + Groovy执行的。
出于性能考虑,这种方法似乎比使用上述 BeanShell Sampler 更可取。
添加对 JSR223 采样器的 Groovy 支持:
2.1. 下载最新的 Groovy二进制发行版;
2.2.
groovy-all-${VERSION}.jar
从分发的“embeddable”文件夹复制并将其放到%JMETER_HOME%/lib/
;2.3. 重新启动 JMeter。
配置 JSR233 采样器:
3.1. 将 JSR233 采样器添加到线程组;
3.2. 设置
Script Language
到groovy
采样器的设置;3.3. 将您的 selenium 测试场景放入
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 案例的常见注意事项:
Script file
字段)一起使用,而不是直接在采样器中使用 Beanshell / Groovy 代码进行密集测试。IsSuccess = STATUS
或SampleResult.setSuccessful(STATUS)
,请参阅上面的代码),而无需使用响应断言。