DevOps AI
  • Home
  • Bảo mật
  • AI & Automation
  • DevOps & Cloud
  • Bộ đề luyện thi cloud
No Result
View All Result
DevOpsAI
  • Home
  • Bảo mật
  • AI & Automation
  • DevOps & Cloud
  • Bộ đề luyện thi cloud
No Result
View All Result
DevOpsAI
No Result
View All Result
Home AI & Automation

TestNG Singleton Design Pattern: Run tests parallel

Huyen Tran by Huyen Tran
1 Tháng 5, 2025
in AI & Automation
1
TestNG Singleton Design Pattern: Run tests parallel
Share on FacebookShare on Twitter

Khi sử dụng Singleton Design Pattern TestNG để quản lý trình điều khiển Driver trong một framework phổ biến như là Selenium WebDriver, bạn cần nghĩ đến việc chạy test song song. Trong ví dụ ở bài  Sử dụng Singleton design pattern trong Automation test bạn sẽ thấy khi chạy chỉ có duy nhất một driver instance, vì vậy bạn không thể thực thi việc chạy test song song. Vậy chúng ta sẽ làm thế nào đây?

Tuy nhiên như bạn biết đấy, Java có hỗ trợ một giải pháp, đó là sử dụng cấu trúc ThreadLocal.

Trong bài này tôi sẽ chia sẽ với cách bạn cách run test parallel (chạy song song) với Singleton design pattern sử dụng ThreadLocal.

Trước khi bắt đầu bạn cần biết:

  • Java Maven project: bạn xem bài Tạo Selenium Automation Test với maven – Intellij IDEA
  • Singleton Design Pattern: bạn xem bài Sử dụng Singleton design pattern trong Automation test nhé
  • WebDriverManager: Đây là một thư viện Java nguồn mở thực hiện việc quản lý (Download, setup và bảo trì) các trình điều khiển driver theo yêu cầu của Selenium WebDriver (ví dụ: chromedriver, geckodriver, msedgedriver, v.v.) một cách hoàn toàn tự động.
  • TestNG: dùng để thực hiện việc chạy test song song. Bạn tìm hiểu thêm ở đây.

Giờ chúng ta bắt đầu nhé.

Related Post

Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent

Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent

16 Tháng 6, 2025
Selenium Tutorial – Java

Selenium Tutorial – Java

1 Tháng 5, 2025

Detox – gray box end-to-end testing automation framework cho ứng dụng React Native

1 Tháng 5, 2025

Cách xử lý các sự kiện bàn phím và chuột trong Selenium sử dụng class Actions

1 Tháng 5, 2025

ThreadLocal là gì?

Java documentation định nghĩa ThreadLocal như sau.

Class cung cấp các biến thread-local thì những biến này khác với nhứng bản sao thông thường của chúng ở mỗi luồng truy cập một (thông qua phương thức get() hoặc set() của nó) được khởi tạo một cách độc lập.

Có nghĩa là:

  • Cấu trúc TheadLocal cho phép chúng ta lưu trữ dữ liệu mà chỉ một luồng (thread) cụ thể mới có thể truy cập được.
  • Tiếp đến, khi muốn sử dụng giá trị này từ một luồng (thread), chúng ta chỉ cần gọi phương thức get() hoặc set().

Nói một cách đơn giản, bạn có thể xem như ThreadLocal lưu trữ dữ liệu bên trong map với thread là key.

Dựa vào định nghĩa trên để ứng dụng vào quản lý driver trong automation test là mỗi luồng sẽ có bản sao trình điều khiển driver riêng, giúp cho việc kiểm tra song song có thể thực hiện được. Đây là cách mà chúng ta có thể sử dụng Singleton design pattern để quản lý phiên bản trình điều khiển driver.

Ví dụ:

  • Tạo một ThreadLocal có giá trị là WebDriver sẽ được gói trong 1 Thread cụ thể như sau:
ThreadLocal<WebDriver> driver = new ThreadLocal<>();
  • Để lưu dữ liệu vào chúng ta sẽ dụng phương thức set()
driver.set(new ChromeDriver());
  • Để lấy dữ liệu ra  chúng ta sử dụng phương thức get()
driver.get();
  • Khi sử dụng ThreadLocal xong, chúng sử dụng phương thức remove() để xoá dữ liệu trong ThreadLocal

driver.remove();

driver.remove();

Tạo một Singleton class sử dụng ThreadLocal quản lý WebDriver

Maven dependences cần tải về

  1. Selenium
  2. WebDriverManager
  3. TestNG

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>SingletonDesignPattern_Selenium</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
    </properties>
    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>3.6.0</version>
        </dependency>
       
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.5</version>
        </dependency>
    </dependencies>
</project>

Tạo Singleton class

Chúng ta sẽ tạo một Singleton class tên SingletonThreadLocalDriver với:

  1. Biến private static  thuộc cấu trúc ThreadLocal có giá trị là WebDriver
  2. private  contructor để ngăn việc tạo mới một instance của Driver
  3. Phương thức public cho phép truy cập driver instance
  4. Phương thức quit() làm nhiệm vụ thoát hết tất cả browser sau khi làm việc và gỡ bỏ  giá trị WebDriver hiện tại  của Thread-local.

Class SingletonThreadLocalDriver.java

package co.devopsify.singletondesignpatter;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SingletonThreadLocalDriver {
    // 1. Using ThreadLocal to manage the driver
    private static ThreadLocal<WebDriver> driver = new ThreadLocal<>();

    // 2. Define private Constructor to prevent the creation of new instances of Driver
    private SingletonThreadLocalDriver(){};

    // 3. Define Public method to access the driver instance
    public static WebDriver getInstance(String browser){
        if(driver.get()==null){
            switch (browser){
                case "edge":{
                    WebDriverManager.edgedriver().setup();
                    driver.set(new EdgeDriver());
                    break;
                }
                case "firefox":{
                    WebDriverManager.firefoxdriver().setup();
                    driver.set(new FirefoxDriver());
                    break;
                }
                default:{
                    WebDriverManager.chromedriver().setup();
                    driver.set(new ChromeDriver());
                    break;
                }
            }
        }
        return driver.get();
    }

    // 4. Define method to quit the driver and remove the current thread's value for this thread-local variable
    public static void quit() {
        driver.get().quit();
        driver.remove();
    }
}

Tạo class TestNG

Ví dụ một lớp test BrowserNavigation với các phương thức test được đánh dấu annotation @Test của TestNG:

  • test1() – cho phép truy cập trang web https://devopsify.co/ với trình duyệt mặc định (chrome)
  • test2() – cho phép truy cập trang web https://devopsify.co/ với trình duyệt firefox
  • test3() – cho phép truy cập trang web https://devopsify.co/ với trình duyệt chrome

Lưu ý: Trong class này chúng ta định nghĩa phương thức tearDown() được đánh dấu annotation @AfterMethod với nhiệm vụ đóng tất cả trình duyệt và loại bỏ webDriver hiện tại của Thread-Local. Phương thức này sẽ được gọi sau khi thực hiện từng phương thức @test (vd: test1(), test2(), test3()), có nghĩa là với ví dụ này phươngt thức tearDown() sẽ được thực thi 3 lần. 

Class BrowserNavigation.java

package co.devopsify.singletondesignpatter;

import org.testng.annotations.*;

public class BrowserNavigation {
    @Test
    void test1() {
        System.out.println("Run test 1");
        SingletonThreadLocalDriver.getInstance("").get("https://devopsify.co/");
    }

    @Test
    void test2() {
        System.out.println("Run test 2");
        SingletonThreadLocalDriver.getInstance("firefox").get("https://devopsify.co/");
    }
    @Test
    void test3() {
        System.out.println("Run test 3");
        SingletonThreadLocalDriver.getInstance("chrome").get("https://devopsify.co/");
    }

    @AfterMethod
    void tearDown(){
        SingletonThreadLocalDriver.quit();
    }
}

Tạo file testng.xml điều khiển việc chạy test

Chúng ta sẽ cho chạy test song song các phương thức test được định nghĩa trong Class BrowserNavigation với 

<suite name="Parallel Test Suite" parallel="methods">

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Parallel Test Suite" parallel="methods">
    <test name="Test example with Singleton Driver">
        <classes>
            <class name="co.devopsify.singletondesignpatter.BrowserNavigation"/>
        </classes>
    </test>
</suite>

Khi chạy test suite trên, chúng ta sẽ thấy 3 test được chạy song song trên 3 browser khác nhau:

  • test1() – trình duyệt Chrome được bật lên và truy cập vào trang web https://devopsify.co/
  • test2() – trình duyệt firefox được bật lên và truy cập vào trang web https://devopsify.co/
  • test3() – trình duyệt Chrome được bật lên và truy cập vào trang web https://devopsify.co/
  • Sau khi chạy xong 3 browser tuần tự sẽ được đóng lại.

Cheers! 🙂

Tags: automation testdesign patternsingletontest paralleltestng
Huyen Tran

Huyen Tran

Related Posts

Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent
AI & Automation

Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent

by devopsify
16 Tháng 6, 2025
Selenium Tutorial – Java
AI & Automation

Selenium Tutorial – Java

by Huyen Tran
1 Tháng 5, 2025
Detox – gray box end-to-end testing automation framework cho ứng dụng React Native
AI & Automation

Detox – gray box end-to-end testing automation framework cho ứng dụng React Native

by Huyen Tran
1 Tháng 5, 2025
Next Post
Upload file sử dụng Selenium WebDriver

Upload file sử dụng Selenium WebDriver

Comments 1

  1. Pingback: Selenium Tutorial - Java - DevOpsify

Để lại một bình luận Hủy

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Recommended

Các website demo hay được sử dụng cho thực hành Automation Test

Các website demo hay được sử dụng cho thực hành Automation Test

11 Tháng 6, 2025
Hướng dẫn cài đặt Kubernetes trên Ubuntu 22.04

Hướng dẫn cài đặt Kubernetes trên Ubuntu 22.04

1 Tháng 5, 2025
Cài đặt Maven trên Windows

Cài đặt Maven trên Windows

11 Tháng 6, 2025

Postwoman test API nhanh chóng mà không cần cài đặt?

25 Tháng 4, 2025
Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent

Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent

16 Tháng 6, 2025
MCP server 2025 tốt nhất : Hướng dẫn chọn & bảo mật

MCP server 2025 tốt nhất : Hướng dẫn chọn & bảo mật

16 Tháng 6, 2025
DevOpsify Check Tool hỗ trợ MCP – Tự động hóa kiểm tra qua AI Claude & VS Code

DevOpsify Check Tool hỗ trợ MCP – Tự động hóa kiểm tra qua AI Claude & VS Code

13 Tháng 6, 2025
GitHub Action DevOpsify Check Tool – Tự động kiểm tra bảo mật & hiệu suất

GitHub Action DevOpsify Check Tool – Tự động kiểm tra bảo mật & hiệu suất

11 Tháng 6, 2025
DevOpsify

Cộng đồng DevOps Việt Nam chia sẽ kiến thức giúp tăng tốc quá trình phát triển ứng dụng và tự động hóa trong lĩnh vực Cloud DevOps & AI.

Bài viết mới

  • Sử dụng VS Code và Playwright MCP tự động test demo website Demoblaze thông qua GitHub Copilot Agent
  • MCP server 2025 tốt nhất : Hướng dẫn chọn & bảo mật
  • DevOpsify Check Tool hỗ trợ MCP – Tự động hóa kiểm tra qua AI Claude & VS Code

Categories

  • AI & Automation
  • Bảo mật
  • Chưa phân loại
  • DevOps & Cloud
  • Tin tức
No Result
View All Result
  • Home
  • Bảo mật
  • AI & Automation
  • DevOps & Cloud
  • Bộ đề luyện thi cloud

© 2025 DevOpsify