使用 JMeter 运行 Selenium 脚本

发布于 2024-12-10 20:04:33 字数 158 浏览 1 评论 0 原文

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

我的第一个目标是使用 selenium 运行自动化脚本,而不是在 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

权谋诡计 2024-12-17 20:04:33

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

JUnit Request Sampler

如果您想重用已经自动化的 (Java) Selenium 场景而不是为 WebDriver 采样器

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 测试计划导出为 .jar 并将其保存到 %JMETER_HOME%/lib/junit/

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

  2. 配置JUnit 请求采样器

    2.1。在 JMeter 测试计划中添加 JUnit 请求采样器
    根据 Selenium 测试计划中的一个设置类名称
    设置测试方法来测试即将运行的测试。
    其他参数默认即可。

    在此处输入图像描述

    JUnit 3.x 与 4.x
    JUnit Request Sampler 可以处理 JUnit3 和 JUnit4 样式的类和方法。要将 Sampler 设置为搜索 JUnit 4 测试(@Test 注释),请选中上面设置中的搜索 Junit4 注释(而不是 JUnit 3) 复选框。
    可以识别以下 JUnit4 注释:

    <块引用>

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

  3. 您已准备好使用 JMeter 开始 Selenium 测试。

JUnit 请求采样器的 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();
    }
}

更新

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


BeanShell Sampler

在本例中,selenium 测试场景直接在 JMeter 的 BeanShell Sampler 中执行。

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

在本例中为 selenium测试场景通过 JSR223 采样器 + Groovy.
对于性能考虑,这种方法似乎比使用更可取BeanShell 采样器如上所述。

  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 添加到线程组;
    3.2.在采样器设置中将脚本语言设置为groovy
    3.3.将您的 selenium 测试场景放入 Script 部分(将接受 Java 代码):

在此处输入图像描述

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;

BeanShell / JSR223 Sampler 案例的常见注释:

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

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.

    enter image description here

    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:

enter image description here

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

enter image description here

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.
上课铃就是安魂曲 2024-12-17 20:04:33

有更简单的方法来运行 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("打开页面...");
        WDS.browser.get('http://duckduckgo.com')
        var searchField = WDS.browser.findElement(pkg.By.id('search_form_input_homepage'))
        搜索字段.click()
        WDS.log.info("点击搜索字段")
        searchField.sendKeys(['blazemeter'])
        WDS.log.info("插入 blazemeter 关键字")
        var Button = WDS.browser.findElement(pkg.By.id('search_button_homepage'))
        按钮.单击()
        WDS.log.info("点击搜索按钮");
        var link = WDS.browser.findElement(pkg.By.ByCssSelector('#r1-0 > div.links_main > h2 > a.large > b'))
        链接.click()
        WDS.log.info("点击了 blazemeter 链接");
        WDS.log.info(WDS.name + '正在整理...');
        WDS.sampleResult.sampleEnd()
    
  • 运行您的测试

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

There is easier way to run Selenium scripts.

  1. Download WebDriver plugin and move to lib/ folder.
  2. Add jp@gc - Firefox Driver Config and jp@gc - Web Driver Sampler to your test tree

Jmeter test tree

  • Add this code

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

For more detailed information about code syntax and best practises you can try Using Selenium with JMeter's WebDriver Sampler article.

分開簡單 2024-12-17 20:04:33

不需要将 Selenium 与 JMeter 一起使用。 Selenium 脚本一次将获取一个浏览器实例。然而,JMeter 不使用浏览器的真实实例来生成负载。

请告诉我是否可以使用 Selenium 脚本从 UI 角度生成 5000 个 vuser 的负载。也许可以。但我们是说 Selenium 脚本现在需要同一系统上的 5000 个浏览器实例吗?测试仍然会运行还是会挂起系统? JMeter 也已经有很多作为记录器的选择。从“负载”测试的角度来看,它提供了出色的统计数据。

暂时,如果我们认为了解 Selenium 的用户不知道如何在 JMeter 中编写脚本,因此会出现学习曲线。但对于 JMeter 来说,这甚至不是真的。这是因为首先不需要创建逻辑序列或程序之类的东西。

There should not be a need to use Selenium with JMeter. Selenium script will take one instance of a browser at a time. Whereas, JMeter does not use a real instance of a browser to generate load.

Please let me know if Selenium script can be used to generate a load from UI standpoint for 5000 vusers. It probably can. But then are we saying that the Selenium script would now require 5000 instances of a browser on the same system? Will the test still run or hang the system instead? JMeter already has great options as a Recorder as well. It provides great stats from "load" testing standpoint.

For a moment if we think that users who know Selenium won't know how to script in JMeter and hence a learning curve. But in case of JMeter this is even not true. It's because there is no need to create something like a logical sequence or a program in the first place.

鱼忆七猫命九 2024-12-17 20:04:33

所以基本上你首先用selenium记录你的脚本,然后使用jmeter重新记录selenium测试用例。 :-)

http://codenaut.blogspot.com/2011/06/ Icefaces-load-testing.html

So basically you record your script with selenium first and then re-record the selenium test cases using jmeter. :-)

http://codenaut.blogspot.com/2011/06/icefaces-load-testing.html

寂寞陪衬 2024-12-17 20:04:33

虽然接受的答案确实对我来说非常有效,但 Selenium RC 已经过时了。这是更新后的 Selenium WebDriver 方法。

注意:可以从已接受的答案中重新使用该设置。

JUnit 请求采样器(Jupiter 测试代码)

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import junit.framework.TestCase;

public class SimpleGoogleSearch extends TestCase {

    @Test
    public void testSelenium() {

        // Replace the following line with the path to your chromedriver.
        System.setProperty("webdriver.chrome.driver",
                "path to your chrome driver");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");
        WebElement searchField = driver.findElement(By.cssSelector("input[class = 'gLFyf gsfi']"));
        searchField.sendKeys("google");
        searchField.submit();
        assertEquals("google - Google Search", driver.getTitle());
        driver.close();
        driver.quit();

    }

}

JUnit Sampler

JSR223 采样器 + Groovy

在此处输入图像描述

While the accepted answer did work for me perfectly, Selenium RC is outdated. So here's the updated Selenium WebDriver approach.

NOTE : The setup can be re-used from the accepted answer.

JUnit Request Sampler (Jupiter Test code)

import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import junit.framework.TestCase;

public class SimpleGoogleSearch extends TestCase {

    @Test
    public void testSelenium() {

        // Replace the following line with the path to your chromedriver.
        System.setProperty("webdriver.chrome.driver",
                "path to your chrome driver");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");
        WebElement searchField = driver.findElement(By.cssSelector("input[class = 'gLFyf gsfi']"));
        searchField.sendKeys("google");
        searchField.submit();
        assertEquals("google - Google Search", driver.getTitle());
        driver.close();
        driver.quit();

    }

}

JUnit Sampler

JSR223 Sampler + Groovy

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文