TestNG + Selenium –负载测试示例

睾丸硒负荷试验

在本教程中,我们将向您展示如何使用@Test属性invocationCountthreadPoolSize在网站上执行负载测试或压力测试。

使用的工具 :

  1. 测试NG 6.8.7
  2. 硒2.39.0
  3. Maven 3

PS:我们正在使用Selenium库来使浏览器自动访问网站。

1.项目依赖

获取TestNG和Selenium库。

pom.xml
<properties>
	<testng.version>6.8.7</testng.version>
	<selenium.version>2.39.0</selenium.version>
  </properties>

  <dependencies>
	<dependency>
		<groupId>org.testng</groupId>
		<artifactId>testng</artifactId>
		<version>${testng.version}</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.seleniumhq.selenium</groupId>
		<artifactId>selenium-java</artifactId>
		<version>${selenium.version}</version>
	</dependency>
   <dependencies>

2. @Test(invocationCount =?)

invocationCount确定了TestNG应该运行此测试方法多少次。

范例2.1

@Test(invocationCount = 10)
  public void repeatThis() {
    //...
  }

输出– repeatThis()方法将运行10次。

PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis

示例2.2 –使用Selenium打开Firefox浏览器并加载“ Google.com”。 此测试是为了确保页面标题始终为“ Google”。

package com.mkyong.testng.examples.loadtest;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestMultipleThreads {

	@Test(invocationCount = 5)
	public void loadTestThisWebsite() {

		WebDriver driver = new FirefoxDriver();		
		driver.get("http://www.google.com");
		System.out.println("Page Title is " + driver.getTitle());
		Assert.assertEquals("Google", driver.getTitle());
		driver.quit();

	}
}

输出–您会注意到Firefox浏览器将提示并关闭5次。

Page Title is Google
Page Title is Google
Page Title is Google
Page Title is Google
Page Title is Google
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite
PASSED: loadTestThisWebsite

3. @Test(invocationCount =?,threadPoolSize =?)

threadPoolSize属性告诉TestNG创建线程池以通过多个线程运行测试方法。 使用线程池,将大大减少测试方法的运行时间。

例3.1 –启动一个包含3个线程的线程池,并运行3次测试方法。

@Test(invocationCount = 3, threadPoolSize = 3)
  public void testThreadPools() {

	System.out.printf("Thread Id : %s%n", Thread.currentThread().getId());
		
  }

输出–测试方法运行3次,每个方法都有自己的线程。

[ThreadUtil] Starting executor timeOut:0ms workers:3 threadPoolSize:3
Thread Id : 10
Thread Id : 12
Thread Id : 11
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools

示例3.2 –启动一个包含3个线程的线程池,并运行10次测试方法。

@Test(invocationCount = 10, threadPoolSize = 3)
  public void testThreadPools() {

	System.out.printf("Thread Id : %s%n", Thread.currentThread().getId());
		
  }

输出–测试方法运行10次,并且线程被重用。

[ThreadUtil] Starting executor timeOut:0ms workers:10 threadPoolSize:3
Thread Id : 10
Thread Id : 11
Thread Id : 12
Thread Id : 10
Thread Id : 11
Thread Id : 12
Thread Id : 10
Thread Id : 11
Thread Id : 12
Thread Id : 10
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools

4.负载测试示例

通过结合TestNG多线程和功能强大的Selenium浏览器自动化。 您可以创建一个简单而强大的负载测试,如下所示:

TestMultipleThreads.java
package com.mkyong.testng.examples.loadtest;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestMultipleThreads {
	
  @Test(invocationCount = 100, threadPoolSize = 5)
  public void loadTest() {

	System.out.printf("%n[START] Thread Id : %s is started!", 
                                  Thread.currentThread().getId());
		
	WebDriver driver = new FirefoxDriver();
	driver.get("http://yourwebsite.com");
		
	//perform whatever actions, like login, submit form or navigation
		
	System.out.printf("%n[END] Thread Id : %s", 
                                  Thread.currentThread().getId());
		
	driver.quit();

  }
}

输出–上面的测试将启动5个线程池,并将100个URL请求发送到指定的网站。

[ThreadUtil] Starting executor timeOut:0ms workers:100 threadPoolSize:5

[START] Thread Id : 11 is started!
[START] Thread Id : 14 is started!
[START] Thread Id : 10 is started!
[START] Thread Id : 12 is started!
[START] Thread Id : 13 is started!
[END] Thread Id : 11
[START] Thread Id : 11 is started!
[END] Thread Id : 10
[START] Thread Id : 10 is started!
[END] Thread Id : 13
[START] Thread Id : 13 is started!
[END] Thread Id : 14
[START] Thread Id : 14 is started!
[END] Thread Id : 12
[START] Thread Id : 12 is started!
[END] Thread Id : 13
......

常见问题
问:对于使用Selenium和TestNG进行负载测试,为什么一次只提示一个浏览器?
答:您的测试方法完成得太快,请尝试放置Thread.sleep(5000)来延迟执行,现在,您应该注意到多个浏览器同时提示。 (仅用于演示目的)。

下载源代码

下载– TestNG-LoadTest-Example.zip (15 kb)

参考文献

  1. 硒网站
  2. 维基百科:负载测试

翻译自: https://mkyong.com/unittest/testng-selenium-load-testing-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值