在本教程中,我們將演示如何使用@Test
屬性invocationCount
和threadPoolSize
在網(wǎng)站上執(zhí)行負(fù)載測試或壓力測試。
使用的工具 :
我們使用Selenium
庫自動化瀏覽器來訪問網(wǎng)站。創(chuàng)建一個(gè)用于測試的Maven項(xiàng)目:TestngSelenium 。
獲取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>
這個(gè)invocationCount
確定TestNG應(yīng)該運(yùn)行這個(gè)測試方法的次數(shù)。
示例 2.1
package com.yiibai;
import org.testng.annotations.Test;
public class TestRepeatThis {
@Test(invocationCount = 10)
public void repeatThis() {
System.out.println("repeatThis " );
}
}
輸出 - repeatThis()
方法將運(yùn)行10
次,結(jié)果如下所示 -
[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”。 此測試是確保頁面標(biāo)題始終為“易百教程? - 專注于IT教程和實(shí)例”。
創(chuàng)建一個(gè)類文件: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();
}
}
輸出結(jié)果如下 - 您會注意到Firefox瀏覽器將提示關(guān)閉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
threadPoolSize
屬性告訴TestNG創(chuàng)建一個(gè)線程池以通過多個(gè)線程運(yùn)行測試方法。 使用線程池,會大大降低測試方法的運(yùn)行時(shí)間。
示例3.1 - 啟動一個(gè)包含3
個(gè)線程的線程池,并運(yùn)行測試方法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());
}
}
執(zhí)行上面代碼,輸出以下結(jié)果 -
[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
個(gè)線程的線程池,并運(yùn)行測試方法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());
}
}
執(zhí)行上面代碼,輸出以下結(jié)果 -
[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
通過結(jié)合TestNG多線程和Selenium強(qiáng)大的瀏覽器自動化。 您可以創(chuàng)建一個(gè)簡單而強(qiáng)大的負(fù)載測試,創(chuàng)建一個(gè)類文件:TestMultipleThreadsLoad.java,其代碼如下所示:
輸出 - 以上測試將啟動一個(gè)線程池5
,并發(fā)送100
個(gè)URL請求到指定的網(wǎng)站。
[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的負(fù)載測試,為什么一次僅提示一個(gè)瀏覽器?
答:您的測試方法完成得太快,嘗試放置Thread.sleep(5000)
方法來延遲執(zhí)行,現(xiàn)在您應(yīng)該注意到多個(gè)瀏覽器同時(shí)提示(僅供演示用途)。