TestNG + Selenium負載測試
在本教程中,我們將演示如何使用[@Test](https://github.com/Test "@Test")
屬性invocationCount
和threadPoolSize
在網站上執行負載測試或壓力測試。
使用的工具 :
- TestNG 6.8.7
- Selenium 2.39.0
- Maven 3
我們使用Selenium
庫自動化瀏覽器來訪問網站。創建一個用於測試的Maven項目:TestngSelenium 。
1. 項目依賴文件配置
獲取TestNG和Selenium
庫。如下 pom.xml
所示 -
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yiibai</groupId>
<artifactId>TestngSelenium</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestngSelenium</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<testng.version>6.8.7</testng.version>
<selenium.version>3.4.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>
</project>
2. @Test(invocationCount =?)
這個invocationCount
確定TestNG應該運行這個測試方法的次數。
示例 2.1
package com.yiibai;
import org.testng.annotations.Test;
public class TestRepeatThis {
@Test(invocationCount = 10)
public void repeatThis() {
System.out.println("repeatThis " );
}
}
輸出 - repeatThis()
方法將運行10
次,結果如下所示 -
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-1220889222\testng-customsuite.xml
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
===============================================
Default test
Tests run: 10, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 10, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@6442b0a6: 32 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@22f71333: 15 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@57829d67: 63 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3d24753a: 109 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6108b2d7: 0 ms
示例2.2 - 使用Selenium打開Firefox瀏覽器並加載「yiibai.com」。 此測試是確保頁面標題始終爲「易百教程™ - 專注於IT教程和實例」。
創建一個類文件:TestMultipleThreads.java,其代碼如下所示:
package com.yiibai;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
public class TestMultipleThreads {
@Test(invocationCount = 3)
public void loadTestThisWebsite() {
//System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
//WebDriver driver = new ChromeDriver();
// System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.yiibai.com");
System.out.println("Page Title is " + driver.getTitle());
AssertJUnit.assertEquals("Google", driver.getTitle());
driver.quit();
}
}
輸出結果如下 - 您會注意到Firefox瀏覽器將提示關閉3
次。
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-1220889222\testng-customsuite.xml
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
PASSED: repeatThis
===============================================
Default test
Tests run: 10, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 10, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@6442b0a6: 32 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@22f71333: 15 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@57829d67: 63 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3d24753a: 109 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6108b2d7: 0 ms
3. @Test(invocationCount = ?, threadPoolSize = ?)
threadPoolSize
屬性告訴TestNG創建一個線程池以通過多個線程運行測試方法。 使用線程池,會大大降低測試方法的運行時間。
示例3.1 - 啓動一個包含3
個線程的線程池,並運行測試方法3
次。
package com.yiibai;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
public class TestMultipleThreads2 {
@Test(invocationCount = 3, threadPoolSize = 3)
public void testThreadPools() {
System.out.printf("Thread Id : %s%n", Thread.currentThread().getId());
}
}
執行上面代碼,輸出以下結果 -
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-103220503\testng-customsuite.xml
[ThreadUtil] Starting executor timeOut:0ms workers:3 threadPoolSize:3
Thread Id : 13
Thread Id : 11
Thread Id : 12
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@6442b0a6: 19 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@22f71333: 5 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@57829d67: 50 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3d24753a: 46 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6108b2d7: 5 ms
示例3.2 - 啓動包含3
個線程的線程池,並運行測試方法10
次。
package com.yiibai;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
public class TestMultipleThreads3 {
@Test(invocationCount = 10, threadPoolSize = 3)
public void testThreadPools() {
System.out.printf("Thread Id : %s%n", Thread.currentThread().getId());
}
}
執行上面代碼,輸出以下結果 -
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--548867586\testng-customsuite.xml
[ThreadUtil] Starting executor timeOut:0ms workers:10 threadPoolSize:3
Thread Id : 13
Thread Id : 11
Thread Id : 12
Thread Id : 11
Thread Id : 13
Thread Id : 12
Thread Id : 13
Thread Id : 11
Thread Id : 13
Thread Id : 11
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
PASSED: testThreadPools
===============================================
Default test
Tests run: 10, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 10, Failures: 0, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.XMLReporter@6442b0a6: 18 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@22f71333: 6 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@57829d67: 39 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3d24753a: 105 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6108b2d7: 11 ms
4. 負載測試示例
通過結合TestNG多線程和Selenium強大的瀏覽器自動化。 您可以創建一個簡單而強大的負載測試,創建一個類文件:TestMultipleThreadsLoad.java,其代碼如下所示:
輸出 - 以上測試將啓動一個線程池5
,併發送100
個URL請求到指定的網站。
[TestNG] Running:
C:\Users\Administrator\AppData\Local\Temp\testng-eclipse--404588393\testng-customsuite.xml
[ThreadUtil] Starting executor timeOut:0ms workers:100 threadPoolSize:5
[START] Thread Id : 14 is started!
[START] Thread Id : 11 is started!
[START] Thread Id : 13 is started!
[START] Thread Id : 12 is started!
[START] Thread Id : 15 is started!1493821695786 geckodriver INFO Listening on 127.0.0.1:45272
1493821695787 geckodriver INFO Listening on 127.0.0.1:41719
1493821695792 geckodriver INFO Listening on 127.0.0.1:44078
1493821695793 geckodriver INFO Listening on 127.0.0.1:20291
1493821695804 geckodriver INFO Listening on 127.0.0.1:43472
1493821696563 mozprofile::profile INFO Using profile path C:\Users\ADMINI~1\AppData\Local\Temp\rust_mozprofile.1bGOSGl5q8Ao
1493821696589 mozprofile::profile INFO Using profile path C:\Users\ADMINI~1\AppData\Local\Temp\rust_mozprofile.5CndLuoF3lLq
1493821696589 mozprofile::profile INFO Using profile path C:\Users\ADMINI~1\AppData\Local\Temp\rust_mozprofile.8UhBfB3XciVW
1493821696591 mozprofile::profile INFO Using profile path C:\Users\ADMINI~1\AppData\Local\Temp\rust_mozprofile.kGZmelveSBLV
1493821696592 geckodriver::marionette INFO Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe
1493821696594 mozprofile::profile INFO Using profile path C:\Users\ADMINI~1\AppData\Local\Temp\rust_mozprofile.duSn42IvmcXK
1493821696595 geckodriver::marionette INFO Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe
1493821696596 geckodriver::marionette INFO Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe
1493821696598 geckodriver::marionette INFO Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe
1493821696624 geckodriver::marionette INFO Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe
1493821696626 geckodriver::marionette INFO Connecting to Marionette on localhost:56795
1493821696627 geckodriver::marionette INFO Connecting to Marionette on localhost:56799
1493821696628 geckodriver::marionette INFO Connecting to Marionette on localhost:56796
1493821696631 geckodriver::marionette INFO Connecting to Marionette on localhost:56798
1493821696632 geckodriver::marionette INFO Connecting to Marionette on localhost:56797
一些問題
問:對於使用Selenium和TestNG的負載測試,爲什麼一次僅提示一個瀏覽器?
答:您的測試方法完成得太快,嘗試放置Thread.sleep(5000)
方法來延遲執行,現在您應該注意到多個瀏覽器同時提示(僅供演示用途)。