运行脚本硒使用JMeter [英] Running Selenium scripts with JMeter

查看:584
本文介绍了运行脚本硒使用JMeter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有硒自动化脚本准备与功能流,现在我想结合使用JMeter的脚本进行负载测试。结果
这可能吗?结果
如果是的话如何整合两者兼而有之?

I have Selenium automation scripts ready with functional flow, now I want to integrate those scripts with JMeter for load-testing.
Is that possible?
If so how to integrate both?

我的第一个目标是利用硒运行自动化脚本比JMeter的运行这些脚本的负载或性能测试。

My first aim is to run the automation script using selenium than run those scripts in jmeter for load or performance testing.

推荐答案

下面是可能的方式来从JMeter的运行Selenium测试情况:

Below are possible ways to run Selenium test-cases from JMeter:

  • using JUnit Request Sampler;
  • using BeanShell Sampler;
  • using JSR223 Sampler + Groovy.

结果

运行Selenium测试这种方法也许有用的,如果你想重新使用已经自动化(Java)的硒方案而不是重新编写JS-脚本的的webdriver取样

Running Selenium tests this way maybe useful if you want to re-use already automated (Java) Selenium scenarios instead of re-writing JS-scripts for WebDriver Sampler.


  1. prepare Selenium测试项目和设置。

  1. Prepare Selenium test project and setup.

1.1。硒下载Java客户端库,并把硒爪哇 - $ {}版本的.jar 来JMeter的classpath中,例如%JMETER_HOME%/ lib目录/ 。结果
1.2。硒服务器应达和听力:

1.1. Download Selenium Java client libraries and put selenium-java-${version}.jar to JMeter classpath, e.g. %JMETER_HOME%/lib/.
1.2. Selenium server should be up and listening:

java -jar selenium-server-standalone-${version}.jar

1.3。出口硒测试计划,.jar和将它保存到%JMETER_HOME%/ lib中/ JUnit的/

注意:您的测试类应该扩展测试用例 SeleneseTestCase 允许JMeter的挑了这个测试计划,测试用例的名字应以测试)开始。结果
注意:默认 SeleneseTestCase 扩展了JUnit 3.x的测试用例,也 SeleneseTestCase 预计外部硒服务器运行着。

NOTE: Your test class should extend TestCase or SeleneseTestCase to allow JMeter pick up this test plan, test case's name should start with "test").
NOTE: By default SeleneseTestCase extends JUnit 3.x TestCase, also SeleneseTestCase expects external Selenium server to be running.

采样请求

2.1。在JMeter的测试计划将 JUnit的请求采样。结果
类名据一位来自Selenium测试计划中设置。结果
设置测试方法来测试即将耗尽。结果
默认情况下,保留其他参数。

2.1. In JMeter test-plan add JUnit Request sampler.
Set Class Name according to one from the Selenium test plan.
Set Test Method to test that is about to run.
Leave other parameters by default.

的JUnit 3.x中主场迎战4.x版结果
 JUnit的请求采样可以同时处理JUnit3-和JUnit4风格的类和方法。要设置取样搜索JUnit 4测试( @Test 注释)检查搜索Junit4注释(而不是JUnit 3中)在设置复选框以上。结果
 下面JUnit4注解确认:

JUnit 3.x vs. 4.x
JUnit Request Sampler can process both JUnit3- and JUnit4-style classes and methods. To set Sampler to search for JUnit 4 tests (@Test annotations) check Search for Junit4 annotations (instead of JUnit 3) checkbox in settings above.
The following JUnit4 annotations are recognized:

@Test - 用于查找试验方法和类。 预期和超时属性的支持。结果
  @Before - 一样对待设置()在JUnit3结果
  @After - 一样对待拆解()在JUnit3结果
  @BeforeClass,@AfterClass - 视为测试方法,使他们可独立为需要运行

@Test - used to find test methods and classes. The "expected" and "timeout" attributes are supported.
@Before - treated the same as setUp() in JUnit3
@After - treated the same as tearDown() in JUnit3
@BeforeClass, @AfterClass - treated as test methods so they can be run independently as required

您已经准备好开始使用JMeter您的Selenium测试。

  • You are ready to start your Selenium test with JMeter.

    Java的code为JUnit的请求采样:

    Java code for JUnit Request sampler:

    的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标注:

    Test script written in JUnit 4 uses JUnit annotations:

    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


    这情况下另一个答案提到的webdriver采样的替代品。

    prerequisites

    使用Selenium RC情况下,唯一的区别是硒设置preparation:

    The only difference with Selenium RC case is Selenium setup preparation:

    1.1。下载并把硒的服务器单机 - $ {}版本的.jar 来JMeter的classpath中,例如%JMETER_HOME%/ lib目录/ 。结果
    注意:有没有必要启动Selenium服务器

    1.1. Download and put selenium-server-standalone-${version}.jar to JMeter classpath, e.g. %JMETER_HOME%/lib/.
    NOTE: There is no need to start the Selenium server.

    所有其它步骤是相同的​​如上述的情形。

    All the other steps are the same as in the scenario described above.

    
    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。

    另一个好点,一步一步的指导使用硒+的JUnit + JMeter的包:

    Another good points and step-by-step guides to use Selenium + JUnit + JMeter bundle:

    • Integrating Selenium with JMeter for Load Testing
    • Integrating Jmeter with Selenium Code
    • Performance testing with Selenium and JMeter
    • Running Selenium tests under JMeter
    • How to integrate a JUnit4 – Webdriver test into JMeter

    在这种情况下,硒的测试场景中JMeter的 BeanShell的采样。

    In this case selenium test-scenario is executed directly in JMeter's BeanShell Sampler.


    1. 硒设置preparation是完全等同于上述情况说明:下载硒库,投入到JMeter的classpath中,启动硒服务器(在硒区局的情况下)

    2. 把你的硒测试场景到BeanShell的采样:

    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;
    

    硒的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集锦,Groovy的

    在这种情况下,硒的测试场景是通过 JSR223采样 +的 Groovy的结果。
    对于<一个href=\"http://blazemeter.com/blog/beanshell-vs-jsr223-vs-java-jmeter-scripting-its-performance\">performance考虑这种做法似乎更加preferable比使用上述BeanShell的采样。


    JSR223 Sampler + Groovy

    In this case selenium test-scenario is executed via JSR223 Sampler + Groovy.
    For performance considerations this approach seems to be more preferable than using BeanShell Sampler described above.


    1. 硒设置preparation是完全等同于上述情况说明:下载硒库,投入到JMeter的classpath中,启动硒服务器(在硒区局的情况下)

    2. 添加JSR223采样Groovy的支持:

    2.1。 下载最新的Groovy 二进制分发;结果
    2.2。复制常规 - 所有 - $ {VERSION}的.jar 从分布的嵌入文件夹并将其拖放到%JMETER_HOME%/ lib目录/ ;结果
    2.3。重启JMeter的。

    2.1. download latest Groovy binary distribution;
    2.2. copy groovy-all-${VERSION}.jar from "embeddable" folder of distribution and drop it to %JMETER_HOME%/lib/;
    2.3. restart JMeter.

    配置JSR233采样:

    Configure JSR233 Sampler:

    3.1。添加JSR233采样主题小组;结果
    3.2。设置脚本语言采样器的设置常规;结果
    3.3。把你的硒测试场景为剧本部分(Java的code将被接受):

    3.1. add JSR233 Sampler to Thread Group;
    3.2. set Script Language to groovy in sampler's settings;
    3.3. put your selenium test-scenario into Script section (Java code will be accepted):

    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;
    

    硒的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采样情况下的常见注意事项:


    • 使用外部.bsh / .groovy作为以代替用取样器BeanShell的/ Groovy的code直接密集的测试试验方案(脚本文件字段)的文件。

    • 由于BeanShell的/ JSR233取样必须JMeter的变量访问您可以直接在测试场景中设置的测试(=采样执行)状态(通过如 IsSuccess = STATUS SampleResult.setSuccessful(STATUS),见code以上),而无需使用响应断言。

    • Use external .bsh / .groovy files with test-scenario (Script file field) instead of using Beanshell / Groovy code directly in sampler for intensive testing.
    • Since BeanShell / JSR233 Samplers have access to JMeter's variables you can set test (= sampler execution) status directly in test-scenario (via e.g. IsSuccess = STATUS or SampleResult.setSuccessful(STATUS), see code above), without using Response Assertion.

    这篇关于运行脚本硒使用JMeter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆