鍍金池/ 教程/ Java/ TestNG + Selenium
TestNG參數(shù)測(cè)試(DataProvider)
TestNG配置注解實(shí)例
TestNG預(yù)期異常測(cè)試
TestNG + Selenium負(fù)載測(cè)試
TestNG參數(shù)化測(cè)試
TestNG參數(shù)測(cè)試(XML和DataProvider)
TestNG配置注釋
TestNG套件測(cè)試
TestNG依賴測(cè)試
TestNG忽略測(cè)試
TestNG忽略測(cè)試
TestNG分組測(cè)試
TestNG預(yù)期異常測(cè)試
TestNG + Selenium
TestNG參數(shù)測(cè)試實(shí)例
TestNG超時(shí)測(cè)試
TestNG Hello World入門示例
JUnit 4 Vs TestNG比較
TestNG + Spring集成示例
TestNG分組測(cè)試
TestNG教程
TestNG超時(shí)測(cè)試
JUnit 4 Vs TestNG比較
TestNG基本注解
TestNG依賴性測(cè)試
TestNG套件測(cè)試
TestNG + Spring集成測(cè)試

TestNG + Selenium

在本教程中,我們將演示如何使用@Test屬性invocationCountthreadPoolSize在網(wǎng)站上執(zhí)行負(fù)載測(cè)試或壓力測(cè)試。

使用的工具 :

  • TestNG 6.8.7
  • Selenium 2.39.0
  • Maven 3

我們使用Selenium庫自動(dòng)化瀏覽器來訪問網(wǎng)站。創(chuàng)建一個(gè)用于測(cè)試的Maven項(xiàng)目:TestngSelenium 。

1. 項(xiàng)目依賴文件配置

獲取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 =?)

這個(gè)invocationCount確定TestNG應(yīng)該運(yùn)行這個(gè)測(cè)試方法的次數(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”。 此測(cè)試是確保頁面標(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é)果如下 - 您會(huì)注意到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

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

threadPoolSize屬性告訴TestNG創(chuàng)建一個(gè)線程池以通過多個(gè)線程運(yùn)行測(cè)試方法。 使用線程池,會(huì)大大降低測(cè)試方法的運(yùn)行時(shí)間。

示例3.1 - 啟動(dòng)一個(gè)包含3個(gè)線程的線程池,并運(yùn)行測(cè)試方法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 - 啟動(dòng)包含3個(gè)線程的線程池,并運(yùn)行測(cè)試方法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

4. 負(fù)載測(cè)試示例

通過結(jié)合TestNG多線程和Selenium強(qiáng)大的瀏覽器自動(dòng)化。 您可以創(chuàng)建一個(gè)簡(jiǎn)單而強(qiáng)大的負(fù)載測(cè)試,創(chuàng)建一個(gè)類文件:TestMultipleThreadsLoad.java,其代碼如下所示:



輸出 - 以上測(cè)試將啟動(dòng)一個(gè)線程池5,并發(fā)送100個(gè)URL請(qǐng)求到指定的網(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

一些問題

:對(duì)于使用Selenium和TestNG的負(fù)載測(cè)試,為什么一次僅提示一個(gè)瀏覽器?
:您的測(cè)試方法完成得太快,嘗試放置Thread.sleep(5000)方法來延遲執(zhí)行,現(xiàn)在您應(yīng)該注意到多個(gè)瀏覽器同時(shí)提示(僅供演示用途)。