Running Selenium Scripts with JMeter – 用JMeter运行Selenium脚本

最后修改: 2021年 6月 27日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll discuss the steps to run Selenium scripts with JMeter.

在本教程中,我们将讨论用JMeter运行Selenium脚本的步骤。

2. Selenium Scripts with JMeter

2.使用JMeter的Selenium脚本

JMeter provides an open-source solution for performance and load testing. It can also be used for functional testing. But with the advancement of technologies like CSS, JS, and HTML5, we push more and more logic and behavior down the client. Thus many more things add to the browser execution time. These things include:

JMeter为性能和负载测试提供了一个开源的解决方案。它也可用于功能测试。但随着CSSJS和HTML5等技术的发展,我们将越来越多的逻辑和行为推到客户端。因此,还有很多东西增加了浏览器的执行时间。这些东西包括。

  • Client-side Javascript execution – AJAX, JS templates, etc.
  • CSS transforms – 3D matrix transforms, animations, etc.
  • 3rd party plugins – Facebook likes Double click ads, etc.

In turn, this might affect the overall performance of a website or web application. But JMeter does not have such a matrix to measure these perceived performances. JMeter also cannot measure the user experience at client renderings like load time or page rendition as JMeter is not a real browser.

反过来,这可能会影响网站或网络应用的整体性能。但JMeter没有这样的矩阵来衡量这些感知的性能。JMeter也不能测量客户端渲染时的用户体验,如加载时间或页面渲染,因为JMeter不是一个真正的浏览器。

Web drivers like Selenium can automate the execution and collection of performance metrics discussed above on the client-side (browser in this case). Thus while the JMeter Load Test will put enough load on the system, the JMeter WebDriver plan will get the response times and other behavior from the user experience point of view.

像Selenium这样的Web驱动可以在客户端(本例中为浏览器)自动执行和收集上面讨论的性能指标。因此,当JMeter负载测试将对系统施加足够的负载时,JMeter WebDriver计划将从用户体验的角度获得响应时间和其他行为。

Thus apart from measuring performance, we can also measure other behaviors when we use a WebDriver set with JMeter. So let’s discuss this further.

因此,除了测量性能外,当我们使用JMeter的WebDriver集时,我们还可以测量其他行为。因此,让我们进一步讨论这个问题。

3. Prerequisites

3.先决条件

Following prerequisites should be fulfilled before running a Selenium script with JMeter:

在使用Selenium脚本运行JMeter之前,应该满足以下先决条件。

Now we can move ahead and create a sample JMeter project to run the Selenium script.

现在我们可以继续前进,创建一个JMeter项目样本来运行Selenium脚本。

4. JMeter Project

4 JMeter项目

At this point, we have the environment installed to run the Selenium script in JMeter. Now, let’s create a JMeter Project to configure and test it out. We’ll create a Thread Group that will have a Selenium Web Driver Sampler instance. We’ll include a Selenium script in this sampler and then execute it.

在这一点上,我们已经安装了环境,可以在JMeter中运行Selenium脚本。现在,让我们创建一个JMeter项目来配置和测试它。我们将创建一个线程组,其中有一个Selenium Web驱动采样器实例。我们将在这个采样器中包含一个Selenium脚本,然后执行它。

A detailed description is given below:

以下是详细的描述。

First, we launch our JMeter GUI:

首先,我们启动我们的JMeter GUI。

Then we can add a simple “Thread Group” by clicking “Edit-> Add” and select the Thread Group:

然后我们可以通过点击 “编辑->添加 “并选择线程组来添加一个简单的 “线程组”

Then, we need to add the Chrome driver config. Now, we click on the Chrome driver config in the left pane and specify the “Path to chrome driver”:

然后,我们需要添加Chrome驱动程序配置。现在,我们点击左侧窗格中的Chrome驱动程序配置,并指定 “Chrome驱动程序的路径”。

Please note that the version of Chrome browser should match with the “Chromedrive.exe” version for the script to run successfully.

请注意,Chrome浏览器的版本应与 “Chromedrive.exe “的版本相匹配,这样脚本才能成功运行。

Next, we need to add the web driver sampler to the thread group:

接下来,我们需要将网络驱动采样器添加到线程组。

We can add the script  given below to the thread group:

我们可以将下面给出的脚本添加到线程组中。

WDS.sampleResult.sampleStart()
WDS.browser.get('http://baeldung.com')
WDS.sampleResult.sampleEnd()
WDS.log.info("successfully navigated to http://baeldung.com/");

Finally, let’s add a ‘View Results in Table’  and/or “View Results Tree” listener so that we can view the results of the script execution.

最后,让我们添加一个 “在表中查看结果 “和/或 “查看结果树 “监听器,这样我们就可以查看脚本的执行结果了。

The thread group we created above looks as shown in the following image:

我们上面创建的线程组看起来如下图所示。

5. Running the Selenium Script

5.运行Selenium脚本

Now we have created the thread group with the Selenium script we want to execute. Next, we “Run the Thread Group”.

现在我们已经用我们想要执行的Selenium脚本创建了线程组。接下来,我们 “运行线程组”

The instance of Selenium Web Driver is created, and a new Chrome driver window opens up that opens the homepage of Baeldung:

Selenium Web驱动的实例被创建,并打开一个新的Chrome驱动窗口,打开Baeldung的主页

As we can see from the JMeter Results table above, we have successfully executed the Thread Group that contained a simple Selenium script that opened a new Chrome Browser window and then opened the specified webpage. This way we can execute any Selenium script by adding a WebDriver Sample in the Thread Group and then executing it.

从上面的JMeter结果表可以看出,我们已经成功执行了包含一个简单的Selenium脚本的线程组,该脚本打开了一个新的Chrome浏览器窗口,然后打开了指定的网页。这样我们就可以通过在线程组中添加一个WebDriver样本,然后执行任何Selenium脚本。

 

6. Conclusion

6.结论

In this tutorial, we have illustrated running a Selenium script using JMeter. We executed a Selenium script in JMeter by creating a Thread Group containing the Selenium Web Driver instance.

在本教程中,我们说明了使用JMeter运行Selenium脚本。我们在JMeter中通过创建一个包含Selenium Web驱动实例的线程组来执行Selenium脚本。

The full code for the implementation is available over on GitHub.

实现的完整代码可在GitHub上获得