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

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

问题描述

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

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

解决方案

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

  • 使用 的替代方案.

    先决条件

    与 Selenium RC 案例的唯一区别是 Selenium 设置准备:

    1.1.下载并将 selenium-server-standalone-${version}.jar 放到 JMeter 类路径中,例如%JMETER_HOME%/lib/.
    注意:无需启动 Selenium 服务器.

    所有其他步骤与上述场景中的相同.

    <代码>包 org.openqa.selenium.example;导入 junit.framework.TestCase;导入 org.junit.Before;导入 org.junit.Test;导入 org.junit.After;导入 org.openqa.selenium.*;导入 org.openqa.selenium.WebDriver;导入 org.openqa.selenium.WebElement;导入 org.openqa.selenium.firefox.FirefoxDriver;导入 org.openqa.selenium.firefox.FirefoxProfile;公共类 selenium 扩展了 TestCase {公共静态 WebDriver 驱动程序;@前公共无效设置(){FirefoxProfile 配置文件 = 新的 FirefoxProfile();驱动程序 = 新的 FirefoxDriver(配置文件);}@测试public void testSelenium() 抛出异常 {driver.get("http://www.google.com/");Assert.assertEquals("Google", driver.getTitle());}@后公共无效撕裂(){驱动程序退出();}}

    <小时>

    更新.

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

    • + Groovy.
      对于性能考虑,这种方法似乎比使用更可取上面描述的 BeanShell 采样器.

      1. Selenium 设置准备与上述案例完全相同:下载 Selenium 库,放入 JMeter 类路径,启动 Selenium 服务器(在 Selenium RC 的情况下).
      2. 为 JSR223 采样器添加 Groovy 支持:

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

      3. 配置 JSR233 采样器:

        3.1.将 JSR233 采样器添加到线程组;
        3.2.在采样器的设置中将 Script Language 设置为 groovy
        3.3.将您的 selenium 测试场景放入 Script 部分(Java 代码将被接受):

      硒RC

      import com.thoughtworks.selenium.*;导入 java.util.regex.Pattern;布尔结果 = true;尝试 {selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com/");硒.开始();selenium.windowMaximize();硒.open("/");selenium.waitForPageToLoad("30000");if (!selenium.isTextPresent("Google")) 结果 = false;} 捕捉(异常前){ex.printStackTrace();log.error(ex.getMessage());SampleResult.setSuccessful(false);SampleResult.setResponseCode("500");SampleResult.setResponseMessage(ex.getMessage());} 最后 {硒.停止();}SampleResult.setSuccessful(result);返回结果;

      Selenium WebDriver

      import org.openqa.selenium.By;导入 org.openqa.selenium.WebDriver;导入 org.openqa.selenium.WebElement;导入 org.openqa.selenium.htmlunit.HtmlUnitDriver;布尔结果 = true;尝试 {驱动程序 = 新 HtmlUnitDriver();driver.setJavascriptEnabled(true);driver.get("http://www.google.com/");if (!driver.getTitle().contains("Google")) 结果 = false;} 捕捉(异常前){ex.printStackTrace();log.error(ex.getMessage());SampleResult.setSuccessful(false);SampleResult.setResponseCode("500");SampleResult.setResponseMessage(ex.getMessage());} 最后 {驱动程序退出();}SampleResult.setSuccessful(result);返回结果;

      <小时>

      BeanShell/JSR223 Sampler 案例的常见说明:

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

      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?

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

      解决方案

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


      JUnit Request Sampler

      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.

      Selenium RC


      1. Prepare Selenium test project and setup.

        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. Export Selenium test-plan as .jar and save it to %JMETER_HOME%/lib/junit/.

        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. Configure JUnit Request sampler

        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 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 - 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

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

      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

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

      Selenium WebDriver


      This case is an alternative to WebDriver Sampler mentioned in another answer below.

      Prerequisites

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

      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.

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


      BeanShell Sampler

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

      1. Selenium setup preparation is completely identical to described above cases: download Selenium libraries, put to JMeter classpath, start Selenium server (in case of Selenium RC).
      2. Put your selenium test-scenario into BeanShell Sampler:

      Selenium 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

      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. Selenium setup preparation is completely identical to described above cases: download Selenium libraries, put to JMeter classpath, start Selenium server (in case of Selenium RC).
      2. Add Groovy support for JSR223 Sampler:

        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.

      3. Configure JSR233 Sampler:

        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):

      Selenium 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;
      


      Common notes for BeanShell / JSR223 Sampler cases:

      • 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 运行 Selenium 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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