繁体   English   中英

使用JMeter运行Selenium脚本

[英]Running Selenium scripts with JMeter

我已准备好功能流的Selenium自动化脚本,现在我想将这些脚本与JMeter集成以进行负载测试。
那可能吗?
如果是这样如何整合两者?

我的第一个目标是使用selenium运行自动化脚本,而不是在jmeter中运行这些脚本以进行负载或性能测试。

以下是从JMeter运行Selenium测试用例的可能方法:


JUnit请求采样器

如果您想重新使用已经自动化(Java)的Selenium场景而不是为WebDriver Sampler重写JS脚本,那么以这种方式运行Selenium测试可能很有用。

硒RC


  1. 准备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 test-plan导出为.jar并将其保存到%JMETER_HOME%/lib/junit/

    注意:您的测试类应扩展TestCaseSeleneseTestCase以允许JMeter选择此测试计划,测试用例的名称应以“test”开头
    注意:默认情况下, SeleneseTestCase扩展了JUnit 3.x TestCaseSeleneseTestCase也希望外部Selenium服务器运行。

  2. 配置JUnit请求采样器

    2.1。 在JMeter测试计划中添加JUnit Request采样器
    根据Selenium测试计划中的一个设置Class Name
    设置Test Method以测试即将运行的Test Method
    默认情况下保留其他参数。

    在此输入图像描述

    JUnit 3.x vs. 4.x.
    JUnit Request Sampler可以处理JUnit3和JUnit4样式的类和方法。 要设置Sampler以搜索JUnit 4测试( @Test annotations),请在上面的设置中选中Search for Junit4 annotations (instead of JUnit 3)复选框。
    识别以下JUnit4注释:

    @Test - 用于查找测试方法和类。 支持“预期”和“超时”属性。
    @Before - 处理与JUnit3中的setUp()相同
    @After - 在JUnit3中处理与tearDown()相同的处理
    @BeforeClass,@ AfterClass - 被视为测试方法,因此可以根据需要独立运行

  3. 您已准备好使用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();
    }
}

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();
    }
}

Selenium WebDriver


这个案例是下面另一个答案中提到的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();
    }
}

UPD。

使用Selenium + JUnit + JMeter包的另一个好点和分步指南:


BeanShell采样器

在这种情况下,selenium测试场景直接在JMeter的BeanShell Sampler中执行

  1. Selenium设置准备与上述情况完全相同:下载Selenium库,放入JMeter类路径,启动Selenium服务器(如果是Selenium RC)。
  2. 将您的selenium测试场景放入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;

Selenium WebDriver

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 Sampler + Groovy

在这种情况下,selenium测试场景通过JSR223 Sampler + Groovy执行。
出于性能考虑,这种方法似乎比使用上述BeanShell Sampler更优选。

  1. Selenium设置准备与上述情况完全相同:下载Selenium库,放入JMeter类路径,启动Selenium服务器(如果是Selenium RC)。
  2. 为JSR223 Sampler添加Groovy支持:

    2.1。 下载最新的Groovy二进制发行版;
    2.2。 从分发的“embeddable”文件夹中复制groovy-all-${VERSION}.jar并将其删除到%JMETER_HOME%/lib/ ;
    2.3。 重启JMeter。

  3. 配置JSR233采样器:

    3.1。 将JSR233 Sampler添加到Thread Group;
    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;

Selenium WebDriver

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采样器案例的常见注意事项:

  • 使用带有测试场景( Script file字段)的外部.bsh / .groovy文件,而不是直接在采样器中使用Beanshell / Groovy代码进行密集测试。
  • 由于BeanShell / JSR233采样器可以访问JMeter的变量,因此可以直接在测试场景中设置测试(=采样器执行)状态(通过例如IsSuccess = STATUSSampleResult.setSuccessful(STATUS) ,参见上面的代码),而不使用响应断言。

有更简单的方法来运行Selenium脚本。

  1. 下载WebDriver插件并移至lib /文件夹。
  2. 将jp @ gc - Firefox驱动程序配置和jp @ gc - Web驱动程序采样器添加到测试树中

Jmeter测试树

  • 添加此代码

      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() 
  • 运行测试

有关代码语法和最佳实践的更多详细信息,您可以尝试将Selenium与JMeter的WebDriver Sampler一起使用

不应该将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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM