TestNG Selenium - 負荷テストの例

TestNG + Selenium –負荷テストの例

testng-selenium-load-test

このチュートリアルでは、@ Test属性invocationCountおよびthreadPoolSizeを使用して、Webサイトで負荷テストまたはストレステストを実行する方法を示します。

使用ツール:

  1. TestNG 6.8.7

  2. セレン2.39.0

  3. メーベン3

P.S We are using the Selenium library to automate browsers to access a website.

1. プロジェクトの依存関係

TestNGおよびSeleniumライブラリを入手してください。

pom.xml

  
    6.8.7
    2.39.0
  

  
    
        org.testng
        testng
        ${testng.version}
        test
    
    
        org.seleniumhq.selenium
        selenium-java
        ${selenium.version}
    
   

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

Example 2.2 – Seleniumを使用してFirefoxブラウザーを開き、「Google.com」をロードします。 このテストでは、ページタイトルが常に「Google」であることを確認します。

package com.example.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に指示します。 スレッドプールを使用すると、テストメソッドの実行時間が大幅に短縮されます。

Example 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

Example 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.example.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のスレッドプールが開始され、指定されたWebサイトに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
......

FAQs
Q:SeleniumとTestNGを使用した負荷テストで、一度に1つのブラウザーしかプロンプトが表示されないのはなぜですか?
A:テスト方法の完了が速すぎるので、%を入力してみてください(t3)s実行を遅らせるために、複数のブラウザが同時にプロンプ​​トを出すことに気付くはずです。 (デモ目的のみ)。

ソースコードをダウンロード

ダウンロード–TestNG-LoadTest-Example.zip(15 kb)