LoadRunner Controller 支持在测试场景中集成 Selenium 测试脚本。可以在场景中与任何其他协议脚本一起运行一个或多个 Selenium 脚本。Selenium 脚本可以在 Windows 和 Linux Load Generator 上运行。
Controller 可以执行以下 Selenium 测试类型 (目前只有 .java 扩展):
1、简单的 Java 脚本
2、JUnit 脚本
3、TestNG
4、TestNG 使用 XML
当 Selenium 脚本在LoadRunner Controller 中运行时,以下 Selenium 功能会对测试产生影响:
1、批注。对于含 Main 函数的简单 Java 脚本,每次执行均视为一个事务。如果脚本配置为“完成前一直运行”,则只会看到一个事务。如果使用计划执行脚本,则事务数等于迭代次数对于 JUnit 和 TestNG 脚本,事务由批注@Test指示。如果一个脚本执行一次,则事务数等于脚本中找到的@Test批注数。
2、Headless 执行。为了减少内存消耗并改善从 Controller 中运行 Selenium 脚本的体验,建议对 Selenium 脚本使用 Headless 选项。使用此选项后,测试执行期间不会打开可见的浏览器。
ChromeOptions options= new ChromeOptions();
options.addArguments("--headless");
driver = new ChromeDriver(options);
如果不使用此选项,则打开的浏览器数等于或大于运行的 Vuser 数。
在LoadRunner中运行Selenium测试脚本需要满足以下条件:
建议使用专用的计算机来执行 Selenium 脚本。并行运行多个 ChromeDriver 或 FirefoxDriver 实例可能会消耗资源。
在可以运行Selenium 测试测试的电脑上。确保系统中已定义WebDriver环境变量 (ChromeDriver 或 GeckoDriver,具体取决于浏览器)。
LoadRunner Professional 安装包含 Selenium 服务器 jar。如果 Selenium 脚本使用其他依赖项 (Classpath 或 TestNG XML),则在脚本的运行时设置 Selenium 节点中,将参数值设置为该文件名。
在LoadRunner测试场景中使用 Selenium :
1、确保将 Load Generator 计算机设置为运行 Selenium 测试。
2、在 Controller 主工具栏上,单击新建场景按钮 。
3、在“新建场景”对话框中,选择“添加脚本”。从“脚本类型”下拉列表中,选择“Selenium 脚本”。
4、单击“浏览”并选择 Selenium 脚本 (.java文件)。
5、在“新建场景”对话框中单击“确定”。包含 Selenium 脚本的场景将在“设计”选项卡中打开。
6、右键单击脚本名称并选择“运行时设置”。如果脚本需要,请输入 Classpath 或 XML 文件名。
7、场景设置在 Selenium 脚本内进行定义。可在“全局计划”中定义以下设置:
持续时间: 设置为“完成前一直运行”。建议设置。
启动 Vuser。选中此选项后,建议使用有限数量的 Vuser。
8、在“运行”选项卡中,单击开始场景按钮以开始运行场景。
Selenium 脚本示例:
1、使用 headless 参数运行测试。
2、测试运行 2 分钟。
3、每次迭代包含 2 个 @Test 批注=> 2 个事务
// Generated by Selenium IDE
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.chrome.ChromeOptions;
public class Testrec1Test {
private WebDriver driver;
private Map
vars;
JavascriptExecutor js;
@Before
public void setUp() {
/* driver = new ChromeDriver();
js = (JavascriptExecutor) driver;
vars = new HashMap
ChromeOptions options= new ChromeOptions();
options.addArguments("--headless");
driver = new ChromeDriver(options);
js = (JavascriptExecutor) driver;
vars = new HashMap
@After
public void tearDown() {
driver.quit();
@Test
public void test1() {
// Test name: test1
// Step # | name | target | value
// 1 | open | / |
driver.get("//Your_page.com");
// 2 | setWindowSize | 1050x863 |
driver.manage().window().setSize(new Dimension(1050, 863));
// 3 | click | linkText=Example 1 |
driver.findElement(By.linkText("Example 1")).click();
// 4 | click | name=entry |
driver.findElement(By.name("entry")).click();
// 5 | type | name=entry | test
driver.findElement(By.name("entry")).sendKeys("test");
// 6 | click | css=p > input |
driver.findElement(By.cssSelector("p > input")).click();
// 7 | click | css=body |
driver.findElement(By.cssSelector("body")).click();
// 8 | click | css=body |
driver.findElement(By.cssSelector("body")).click();
//close in function
driver.close();
@Test
public void empty_function(){
//dummy test
查看 Selenium 测试结果
Selenium 脚本生成的结果显示在以下 Controller 标准图中:
· 正在运行的 Vuser
· 事务响应时间
· 事务总数/秒 (通过)
· 事务数/秒 (通过)
标准的吞吐量图不显示 Selenium 脚本的结果。无法从 Selenium 脚本结果生成自定义图。
LoadRunner中使用 Selenium 测试时需要注意以下事项:
1、运行时设置中的 Classpath 和 TestNG XML 字段都支持绝对路径和相对路径。相对路径的起始目录是脚本目录 (.java脚本文件所在的位置)。
2、如果有多个 XML 相互连接,则在运行时设置中指定主 XML。
3、如果脚本文件夹中有多个 XML 配置文件,并且在运行时设置中没有指定 XML,则执行所有 XML 文件。
4、如果来自 XML 字段 (在运行时设置中) 的输入无效,则仅执行在 Controller 中上载的第一个脚本。
5、使用 Linux Load Generator 时,建议为 Selenium.java脚本定义附加属性。
以上就是我们为您整理的如何在LoadRunner中运行Selenium测试脚本的全部内容,了解更多LoadRunner性能测试工具相关内容可查看我的主页。
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.