NỘI DUNG BÀI HỌC

✅ Properties, Excel, Log4j2, Record Video
✅ Tạo data giả với DataFaker

✅ Cập nhật version cho các thư viện

- Cucumber: 7.12.0
- Selenium: 4.9.1
- Apache Poi: 5.2.3
- Log4j2: 2.20.0
- Common-IO: 2.11.0
- Monte Screen Recorder: 0.7.7.0
- Lombok: 1.18.26
- Allure Report: 2.22.0
- DataFaker: 1.9.0


***Checking for new dependency updates:

mvn versions:display-dependency-updates


***Checking for new plugin updates:

mvn versions:display-plugin-updates


***Updating to the Latest RELEASE:

mvn versions:use-latest-releases



Chúng ta bỏ cái Extent Report đi. Vì sau này sử dụng Cucumber Report dạng HTML rồi.

Lưu ý: Bỏ Extent Report thì nhớ chỉnh lại trong các hàm của keyword WebUI.

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.9.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>5.3.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.8.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.12.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>7.12.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-gherkin -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-gherkin</artifactId>
        <version>7.12.0</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>7.12.0</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.cucumber/gherkin -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>gherkin</artifactId>
        <version>26.2.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>2.0.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.7</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.2.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.11.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.github.stephenc.monte/monte-screen-recorder -->
    <dependency>
        <groupId>com.github.stephenc.monte</groupId>
        <artifactId>monte-screen-recorder</artifactId>
        <version>0.7.7.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.20.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.20.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.26</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
    <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>5.0.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng -->
    <dependency>
        <groupId>io.qameta.allure</groupId>
        <artifactId>allure-testng</artifactId>
        <version>2.22.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-attachments -->
    <dependency>
        <groupId>io.qameta.allure</groupId>
        <artifactId>allure-attachments</artifactId>
        <version>2.22.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.19</version>
    </dependency>
    <dependency>
        <groupId>net.datafaker</groupId>
        <artifactId>datafaker</artifactId>
        <version>1.9.0</version>
    </dependency>
</dependencies>

 


✅ Cập nhật các hàm trong WebUI keyword

Tham khảo các hàm tại đây: anhtester / AutomationFrameworkSelenium

✅ Data Faker là gì?

DataFaker là một thư viện dành cho JavaKotlin để tạo dữ liệu giả, tự động generate data. Điều này rất hữu ích khi tạo dữ liệu thử nghiệm để điền vào cơ sở dữ liệu, để tạo dữ liệu cho thử nghiệm mang tính demo hoặc ẩn danh dữ liệu từ các thông tin dịch vụ nhạy cảm không cần thiết.

Thư viện DataFaker kế thừa và phát triển từ thư viện JavaFaker.

Thư viện này tạo dữ liệu giả tương tự như các trình tạo dữ liệu giả khác, chẳng hạn như:


✅ Cài đặt thư viện Data Faker

Phiên bản mới nhất của Data Faker1.9.0 và được lưu trữ trên Maven Central.

<!-- https://mvnrepository.com/artifact/net.datafaker/datafaker -->
<dependency>
    <groupId>net.datafaker</groupId>
    <artifactId>datafaker</artifactId>
    <version>1.9.0</version>
</dependency>

Website: https://www.datafaker.net/
GitHub: https://github.com/datafaker-net/datafaker


✅ Sử dụng Data Faker

Để sử dụng Data Faker để tạo dữ liệu giả, bạn có thể sử dụng đoạn code sau làm ví dụ:

import net.datafaker.Faker;

Faker faker = new Faker();

//Thông tin người
String name = faker.name().fullName();
String firstName = faker.name().firstName();
String lastName = faker.name().lastName();

//Thông tin đường
String streetAddress = faker.address().streetAddress();

Như vậy là các bạn chỉ cần tạo đối tượng class Faker. Sau đó lấy chấm gọi dùng các hàm nó viết sẵn để lấy dữ liệu giả thôi.

✅ Sử dụng vùng ngôn ngữ trong Data Faker

Bạn có thể sử dụng data của các nước khác nhau thông qua Locate trong Java. Mặc định là English tương ứng ký hiệu locate là "en".

Ví dụ mình muốn dùng tiếng Việt thì dùng ký hiệu "vi".

package com.anhtester.testcases;

import net.datafaker.Faker;
import org.testng.annotations.Test;

import java.util.Locale;

@Test
public void testDataFaker01() {
    Faker faker = new Faker(new Locale("vi"));

    String computer = faker.computer().windows();
    String name = faker.name().fullName();
    String fullAddress = faker.address().fullAddress();
    String email = faker.internet().emailAddress();
    String password = faker.internet().password();

    System.out.println(computer);
    System.out.println(name);
    System.out.println(fullAddress);
    System.out.println(email);
    System.out.println(password);
}

Kết quả:

Windows 11
Nguyễn Vân
Apt. 387 9700 Thảo Valley, Phú Thọ, MS WDHV  0ZR
nguyen.tham@hotmail.com
1e8cj6oj53cf

===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Lưu ý: class Locate là của Java

Các loại data khác bạn có thể xem thêm tại đây Fake Data Providers

🔆 Nâng cao hơn là bạn viết class utils hoặc helpers riêng để sau này gọi dùng cho tiện.

DataFakerHelpers.java

package com.anhtester.helpers;

import com.anhtester.constants.ConstantGlobal;
import net.datafaker.Faker;

import java.util.Locale;

public class DataFakerHelpers {

    private static Faker faker;

    public static Faker createFaker() {
        faker = new Faker(new Locale(ConstantGlobal.LOCATE));
        return faker;
    }

    public static Faker createFakerByLocate(String locateName) {
        faker = new Faker(new Locale(locateName));
        return faker;
    }

    public static Faker getFaker() {
        if (faker == null) {
            faker = createFaker();
        }
        return faker;
    }

    public static Faker getFakerByLocate(String locateName) {
        faker = createFakerByLocate(locateName);
        return faker;
    }

    public static void setFaker(Faker faker) {
        DataFakerHelpers.faker = faker;
    }
}

Note: ConstantGlobal là class lưu các biến static được khai báo giá trị cứng hoặc lấy từ file config properties

public final static String LOCATE = PropertiesHelpers.getValue("LOCATE");

 

Gọi lại sử dụng:

@Test
public void testDataFaker02() {
    String cityName1 = DataFakerHelpers.getFaker().address().cityName();
    String cityName2 = DataFakerHelpers.getFakerByLocate("vi").address().cityName();

    System.out.println(cityName1);
    System.out.println(cityName2);
}

Teacher

Teacher

Anh Tester

Software Quality Engineer

Đường dẫu khó chân vẫn cần bước đi
Đời dẫu khổ tâm vẫn cần nghĩ thấu

Cộng đồng Automation Testing Việt Nam:

🌱 Telegram Automation Testing:   Cộng đồng Automation Testing
🌱 
Facebook Group Automation: Cộng đồng Automation Testing Việt Nam
🌱 
Facebook Fanpage: Cộng đồng Automation Testing Việt Nam - Selenium
🌱 Telegram
Manual Testing:   Cộng đồng Manual Testing
🌱 
Facebook Group Manual: Cộng đồng Manual Testing Việt Nam

Chia sẻ khóa học lên trang

Bạn có thể đăng khóa học của chính bạn lên trang Anh Tester để kiếm tiền

Danh sách bài học