Rah*_*tta 26 java junit selenium automated-tests jmeter
我已准备好功能流的Selenium自动化脚本,现在我想将这些脚本与JMeter集成以进行负载测试.
那可能吗?
如果是这样如何整合两者?
我的第一个目标是使用selenium运行自动化脚本,而不是在jmeter中运行这些脚本以进行负载或性能测试.
Ali*_*lik 27
以下是从JMeter运行Selenium测试用例的可能方法:
如果您想重新使用已经自动化(Java)的Selenium场景而不是为WebDriver Sampler重写JS脚本,那么以这种方式运行Selenium测试可能很有用.
准备Selenium测试项目和设置.
1.1.下载Selenium Java客户端库并放入selenium-java-${version}.jar
JMeter类路径,例如%JMETER_HOME%/lib/
.
1.2.Selenium服务器应该启动并监听:
java -jar selenium-server-standalone-${version}.jar
Run Code Online (Sandbox Code Playgroud)
1.3.将Selenium测试计划导出为.jar并将其保存到%JMETER_HOME%/lib/junit/
.
注意:您的测试类应该扩展TestCase
或SeleneseTestCase
允许JMeter选择此测试计划,测试用例的名称应以"test"开头.
注意:默认情况下SeleneseTestCase
扩展JUnit 3.x TestCase
,也SeleneseTestCase
期望外部Selenium服务器正在运行.
2.1.在JMeter测试计划中添加JUnit Request采样器.根据Selenium测试计划中的一个
设置Class Name
.
设置Test Method
为测试即将运行.
默认情况下保留其他参数.
JUnit 3.x与4.x
JUnit请求采样器可以处理JUnit3和JUnit4样式的类和方法.要设置Sampler以搜索上面设置中的JUnit 4测试(@Test
注释)Search for Junit4 annotations (instead of JUnit 3)
复选框.
识别以下JUnit4注释:
@Test - 用于查找测试方法和类.支持"预期"和"超时"属性.
@Before - 在JUnit3
@After中处理与setUp()相同的处理 - 在JUnit3
@BeforeClass中处理与tearDown()相同,@ AfterClass - 作为测试方法处理,以便它们可以根据需要独立运行
您已准备好使用JMeter开始Selenium测试.
JUnit Request采样器的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();
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这个案例是下面另一个答案中提到的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();
}
}
Run Code Online (Sandbox Code Playgroud)
UPD.
使用Selenium + JUnit + JMeter包的另一个好点和分步指南:
在这种情况下,selenium测试场景直接在JMeter的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;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,selenium测试场景通过JSR223 Sampler + Groovy执行.
出于性能考虑,这种方法似乎比使用上述BeanShell Sampler更优选.
为JSR223 Sampler添加Groovy支持:
2.1.下载最新的Groovy二进制发行版;
2.2.groovy-all-${VERSION}.jar
从"embeddable"文件夹中复制并将其删除%JMETER_HOME%/lib/
;
2.3.重启JMeter.
配置JSR233采样器:
3.1.将JSR233 Sampler添加到Thread Group;
3.2.设置Script Language
为groovy
采样器的设置;
3.3.将您的selenium测试场景放入Script
部分(将接受Java代码):
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;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
BeanShell/JSR223采样器案例的常见注意事项:
Script file
字段)的外部.bsh/.groovy文件,而不是直接在采样器中使用Beanshell/Groovy代码进行密集测试.IsSuccess = STATUS
或SampleResult.setSuccessful(STATUS)
,参见上面的代码),而不使用Response Assertion.有更简单的方法来运行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()
Run Code Online (Sandbox Code Playgroud)运行测试
有关代码语法和最佳实践的更多详细信息,您可以尝试将Selenium与JMeter的WebDriver Sampler一起使用.
归档时间: |
|
查看次数: |
41052 次 |
最近记录: |