開発者ドキュメント

TestNG Selenium – 負荷テストの例


testng-selenium-load-test、width = 215、height = 240

このチュートリアルでは、@Testアトリビュート

invocationCount`と

threadPoolSize`を使用してウェブサイトで負荷テストやストレステストを実行する方法を説明します。

使用されるツール:

  1. TestNG 6.8.7

  2. セレン2.39.0

  3. Maven 3

__P.S私たちはSeleniumライブラリを使用して、ブラウザを自動化してWebサイトにアクセスしています。

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のスレッドプールが開始され、指定された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 : For load testing with Selenium and TestNG, why only one browser is
prompts out at a time? +
A : Your test method is completed too fast, try putting a
`Thread.sleep(5000)` to delay the execution, now, you should notice
multiple browsers prompt out simultaneously. (For demonstration purpose
only).

=== Download Source Code

Download –
link://wp-content/uploads/2014/01/TestNG-LoadTest-Example.zip[TestNG-LoadTest-Example.zip](15 kb)

=== References

. http://docs.seleniumhq.org/[Selenium website]. http://en.wikipedia.org/wiki/Load__testing[Wikipedia : Load Testing]
link://tag/load-test/[load test]link://tag/selenium/[selenium]link://tag/testng/[testng]
モバイルバージョンを終了