TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Learn to Use TestNG Threads for Selenium Load Testing
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Selenium Tutorial

Learn to Use TestNG Threads for Selenium Load Testing

Last updated: Feb 25, 2024 10:56 am
By Meenakshi Agarwal
Share
8 Min Read
How to Use TestNG Threads for Selenium Load Testing
How to Use TestNG Threads for Selenium Load Testing
SHARE

It’s a simple TestNG tutorial that explains how to use TestNG threads for Selenium load testing. Also, it’s true that we can utilize TestNG to perform load testing by creating multiple threads. We can even re-execute a test the number of times we want.

Contents
1. POM file used to run the samples.2. TestNG example using @Test(invocationCount=?) attribute.3. TestNG example using @Test(invocationCount=?, threadPoolSize=?) attribute.4. TestNG example to generate load using multiple threads.

TestNG is indeed a quite resourceful test automation framework. And integrating it with Webdriver only makes it better to perform automated testing. In this post, we are going to expose two powerful attributes of the @Test annotation of TestNG.

These are <invocationCount> and <threadPoolSize>. The first attribute specifies the exact number of times a test method will get called.  Whereas the latter sets the total number of threads that will run to call the test method.

Also, you should know that we can include both the above @Test attributes individually in a test and can use them in combination as well. In this TestNG tutorial, we’ll give you examples of using both these attributes.

Next, if you are a beginner and starting to learn TestNG and Selenium, then please go through the below two posts. They’ll help you in ramping up quickly on using these tools.

  • Step-by-Step Guide to Create Your First WebDriver Project
  • How to Write a TestNG-WebDriver Test Case from Scratch

How to use TestNG threads for Selenium load testing.

Before diving into the core part of this TestNG tutorial, first get to know about the tools required to build the code samples.

1. TestNG 6.9.10
2. WebDriver 2.53.1
3. Maven 4.0.0

If you haven’t already used Maven, then just read this post to set up Maven with TestNG and Selenium in five minutes.

Now, first of all, we’ll create a POM file to download all the above tools and run the test project.

1. POM file used to run the samples.

You can copy this POM file into your project.

<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.techbeamers</groupId>
 <artifactId>loadtesting</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>Load Testing</name>
 <description>Selenium Load Testing Example Using TestNG and Maven</description>

 
 <!-- Add Following Lines in Your POM File -->
 <properties>
  <selenium.version>2.53.1</selenium.version>
  <testng.version>6.9.10</testng.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-java</artifactId>
   <version>${selenium.version}</version>
  </dependency>
  <dependency>
   <groupId>org.testng</groupId>
   <artifactId>testng</artifactId>
   <version>${testng.version}</version>
   <scope>test</scope>
  </dependency>
 </dependencies>

</project>

2. TestNG example using @Test(invocationCount=?) attribute.

The below code will show how the <invocationCount> attribute calls a method multiple times.

package com.techbeamers.loadtesting;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

//--
import org.openqa.selenium.By;
//--
import org.openqa.selenium.WebDriver;
//--
import org.openqa.selenium.WebElement;
//--
import org.openqa.selenium.firefox.FirefoxDriver;
//--
import org.testng.Assert;
//--
import org.testng.annotations.AfterClass;
//--
import org.testng.annotations.BeforeClass;
//--
import org.testng.annotations.Test;

public class LoadTestMultipleTimes {

    private WebDriver driver;

    @BeforeClass
    public void beforeClass() {
    }

    @AfterClass
    public void afterClass() {
    }

    @Test(invocationCount = 2)
    public void testGoogleSearch() {

        driver = new FirefoxDriver();
        Date date = null;
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("http://www.google.com");

        String search_text = "Google Search";
        WebElement search_button = driver.findElement(By.name("btnK"));
        String text = search_button.getAttribute("value");

        date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");
        String timstamp = sdf.format(date);
        System.out.println(timstamp + ": Search button text is " + driver.getTitle());

        Assert.assertEquals(text, search_text, "Text not found!");
        driver.quit();
    }
}

Also Read: Understanding TestNG Assertions

3. TestNG example using @Test(invocationCount=?, threadPoolSize=?) attribute.

In this TestNG example, we are using both attributes together while keeping their value the same. It’ll let each test method call run on a separate thread. To check, copy/paste the below code in your project.

We’ve added thread ID and timestamp in the log messages so that you can identify the thread and the execution time of the test running.

package com.techbeamers.loadtesting;

//--
import java.text.SimpleDateFormat;
//--
import java.util.Date;
//--
import java.util.concurrent.TimeUnit;
//--
import org.openqa.selenium.By;
//--
import org.openqa.selenium.WebDriver;
//--
import org.openqa.selenium.WebElement;
//--
import org.openqa.selenium.firefox.FirefoxDriver;
//--
import org.testng.Assert;
//--
import org.testng.annotations.AfterClass;
//--
import org.testng.annotations.BeforeClass;
//--
import org.testng.annotations.Test;

public class LoadTestSingleTestPerThread {

    @BeforeClass
    public void beforeClass() {
    }

    @AfterClass
    public void afterClass() {
    }

    @Test(invocationCount = 2, threadPoolSize = 2)
    public void testGoogleSearch() {

        System.out.printf("%n[+] Thread Id : %s is started!\n", Thread.currentThread().getId());
        WebDriver driver = new FirefoxDriver();
        Date date = null;
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("http://www.google.com");

        String search_text = "Google Search";
        WebElement search_button = driver.findElement(By.name("btnK"));
        String text = search_button.getAttribute("value");

        date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");
        String timstamp = sdf.format(date);
        System.out.println(timstamp + " [Thread-" + Thread.currentThread().getId() + "]- Search button text is "
                + driver.getTitle());

        Assert.assertEquals(text, search_text, "Text not found!");

        System.out.printf("%n[-] Thread Id : %s Ended.\n", Thread.currentThread().getId());
        driver.quit();
    }
}

4. TestNG example to generate load using multiple threads.

Here, we’ll show how to use a limited thread pool for processing a large number of Web requests. In this example, we are using a test that sends a request to Google and verifies the text at the search button.

This test will create a number of threads as specified by the <threadPoolSize> attribute. And, each thread will send multiple Web requests as a part of the load test.

package com.techbeamers.loadtesting;

//--
import java.text.SimpleDateFormat;
//--
import java.util.Date;
//--
import java.util.concurrent.TimeUnit;
//--
import org.openqa.selenium.By;
//--
import org.openqa.selenium.WebDriver;
//--
import org.openqa.selenium.WebElement;
//--
import org.openqa.selenium.firefox.FirefoxDriver;
//--
import org.testng.Assert;
//--
import org.testng.annotations.AfterTest;
//--
import org.testng.annotations.BeforeTest;
//--
import org.testng.annotations.Test;

public class LoadTestMultipleTestPerThread {

    @BeforeTest
    public void beforeTest() {
    }

    @AfterTest
    public void afterTest() {
    }

    @Test(invocationCount = 10, threadPoolSize = 5)
    public void testGoogleSearch() {

        System.out.printf("%n[+] Thread Id : %s is started!\n", Thread.currentThread().getId());
        WebDriver driver = new FirefoxDriver();
        Date date = null;
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.get("http://www.google.com");

        String search_text = "Google Search";
        WebElement search_button = driver.findElement(By.name("btnK"));
        String text = search_button.getAttribute("value");

        date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");
        String timstamp = sdf.format(date);
        System.out.println(timstamp + " [Thread-" + Thread.currentThread().getId() + "]- Search button text is "
                + driver.getTitle());

        Assert.assertEquals(text, search_text, "Text not found!");

        System.out.printf("%n[-] Thread Id : %s Ended.\n", Thread.currentThread().getId());
        driver.quit();
    }
}

Take This Quiz: Selenium WebDriver TestNG Quiz

Summary – How to Use TestNG Threads for Selenium Load Testing.

We are hopeful that this post will enable you to use TestNG and Selenium for load testing. Also, you would’ve learned to use TestNG threads for running WebDriver tests.

It would be great if you let us know your feedback on this post. Also, you can ask us to write on a topic of your choice. We’ll add it to our writing plan.

If you enjoyed the post, then please don’t leave without sharing it with friends and on social media.

Keep Learning,

TechBeamers.

You Might Also Like

20 Demo Sites for Automation Testing

Page Object Model (POM) and Page Factory Guide in Selenium Java

Selenium 4 Relative Locators Guide

Selenium Version 4 Features – What’s New?

How to Inspect Element in Safari, Android, and iPhone

TAGGED:TestNG Examples
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Web Testing Interview Questions and Answers Web Testing Interview Questions for QA Engineers
Next Article How to Use TestNG Factory Method for Load Testing Learn to Use TestNG Factory Method for Load Testing

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x