TestNG + Selenium - Exemple de test de charge
Dans ce tutoriel, nous allons vous montrer comment utiliser les attributs @TestinvocationCount
etthreadPoolSize
pour effectuer un test de charge ou un test de résistance sur un site Web.
Les outils utilisés :
-
TestNG 6.8.7
-
Sélénium 2.39.0
-
Maven 3
P.S We are using the Selenium library to automate browsers to access a website.
1. Dépendance du projet
Obtenez les bibliothèques TestNG et 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 =?)
CeinvocationCount
a déterminé combien de fois TestNG doit exécuter cette méthode de test.
Exemple 2.1
@Test(invocationCount = 10) public void repeatThis() { //... }
Sortie - La méthoderepeatThis()
s'exécutera 10 fois.
PASSED: repeatThis PASSED: repeatThis PASSED: repeatThis PASSED: repeatThis PASSED: repeatThis PASSED: repeatThis PASSED: repeatThis PASSED: repeatThis PASSED: repeatThis PASSED: repeatThis
Example 2.2 - Utilise Selenium pour ouvrir un navigateur Firefox et charger «Google.com». Ce test consiste à s'assurer que le titre de la page est toujours «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(); } }
Sortie - Vous remarquerez que le navigateur Firefox s'affiche et se ferme 5 fois.
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 =?)
L'attributthreadPoolSize
indique à TestNG de créer un pool de threads pour exécuter la méthode de test via plusieurs threads. Avec le pool de threads, cela réduira considérablement le temps d'exécution de la méthode de test.
Example 3.1 - Démarre un pool de threads, qui contient 3 threads, et exécutez la méthode de test 3 fois.
@Test(invocationCount = 3, threadPoolSize = 3) public void testThreadPools() { System.out.printf("Thread Id : %s%n", Thread.currentThread().getId()); }
Sortie - La méthode de test a été exécutée 3 fois et chacune a reçu son propre thread.
[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 - Démarre un pool de threads, qui contient 3 threads, et exécutez la méthode de test 10 fois.
@Test(invocationCount = 10, threadPoolSize = 3) public void testThreadPools() { System.out.printf("Thread Id : %s%n", Thread.currentThread().getId()); }
Sortie - La méthode de test s'exécute 10 fois et les threads sont réutilisés.
[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. Exemple de test de charge
En combinant à la fois plusieurs threads TestNG et l'automatisation puissante du navigateur Selenium. Vous pouvez créer un test de charge simple mais puissant comme celui-ci:
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(); } }
Sortie - Le test ci-dessus démarre un pool de threads de 5 et envoie 100 demandes d'URL à un site Web spécifié.
[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: Pour les tests de charge avec Selenium et TestNG, pourquoi un seul navigateur est invité à la fois?
A: Votre méthode de test est terminée trop rapidement, essayez de mettre unThread.sleep(5000)
pour retarder l'exécution, maintenant, vous devriez remarquer que plusieurs navigateurs sont invités simultanément. (À des fins de démonstration uniquement).
Télécharger le code source
Télécharger -TestNG-LoadTest-Example.zip (15 kb)