❅
❅
❆
❅
❆
❅
❆
❅
❆
❆

  • 0939206009
  • thaian.it15@gmail.com
  • Facebook
  • Youtube
  • Zalo
Anh Tester Logo
  • Khoá học
    • All Courses
    • Website Testing
    • API Testing
    • Desktop Testing
    • Mobile Testing
    • Programming Language
    • CI/CD for Tester
    • Performance Testing
  • 💥Khai giảng
  • tools
    • TestGenAI - AI Test Cases Generator
    • Mobile Apps Demo
    • Automation Framework Selenium TestNG
    • Automation Framework Cucumber TestNG
    • Gherkin Convert Functions in Katalon
    • Convert object from Selenium Java to Playwright Python
    • Website Demo CRM
    • Website Demo HRM
    • Website Demo HRM GO
    • Website Demo POS
    • Website Demo eCommerce CMS
  • blog
    • Selenium C#
    • Selenium Java
    • Katalon Tools
    • Jenkins CI/CD
    • SQL cho Tester
    • Manual Testing
    • Tài liệu cho Tester
    • Automation Testing
    • akaAT Tools
    • Cucumber TestNG
    • API Testing with Postman
    • Apache Maven
    • AI in Software Testing
    • Lịch khai giảng
  • Liên hệ
  • Log in
    Sign up

[Selenium Java] Bài 27: Đọc data test từ Properties file trong Selenium Java

  • Blog
  • Selenium Java
[Selenium Java] Bài 27: Đọc data test từ Properties file trong Selenium Java
Download Document Video

[Selenium Java] Bài 27: Đọc data test từ Properties file trong Selenium Java

  • Anh Tester
  • Selenium Java
  • 8040
Anh Tester hướng dẫn các bạn đọc data test từ Properties file trong Selenium Java

🔆 Kế thừa các phần trước là Page Object và TestNG Framework


1. Tạo tệp có phần mở rộng là .properties

Trong dự án của bạn, tạo một tập tin có tên là configs với phần mở rộng là .properties

Nhấp chuột phải vào folder nào đó (trong hình là resources) và tạo một tệp configs.properties

Đây là bước đầu tiên chúng ta sẽ thực hiện bằng cách khởi tạo giá trị trong file properties theo cấu trúc "key"="value" như bên dưới.

[Selenium Java] Bài 27: Đọc data test từ Properties file trong Selenium Java | Anh Tester

browser=chrome
email=tld01@mailinator.com
password=123456


Khi mình gọi ra thì mình gọi cái Key chứ không gọi Value nhe =))

Cách gọi thì đọc tiếp nè

2. Tạo một class Java để xây dựng hàm Get và Set properties

Tạo một lớp mới có tên là “PropertiesFile”.

[Selenium Java] Bài 27: Đọc data test từ Properties file trong Selenium Java | Anh Tester


Để đọc và ghi trên
tệp .properties thì mình viết một đoạn mã đơn giản tuân theo quy tắc đọc file trong Java

package anhtester.com.common.ultilities;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class PropertiesFile {

    private static Properties properties;
    private static FileInputStream fileIn;
    private static FileOutputStream fileOut;

    //Lấy đường dẫn đến project hiện tại
    static String projectPath = System.getProperty("user.dir") + "/";
    //Tạo đường dẫn đến file configs.properties mặc định
    private static String propertiesFilePathRoot = "src/test/resources/configs.properties";

    public static void setPropertiesFile() {
        properties = new Properties();
        try {
            //Khởi tạo giá trị cho đối tượng của class FileInputStream
            fileIn = new FileInputStream(projectPath + propertiesFilePathRoot);
            //Load properties file
            properties.load(fileIn);
        } catch (Exception exp) {
            System.out.println(exp.getMessage());
            System.out.println(exp.getCause());
            exp.printStackTrace();
        }
    }

    //Xây dựng hàm Get Value từ Key của file properties đã setup bên trên
    public static String getPropValue(String KeyProp) {
        String value = null;
        try {
            //get values from properties file
            value = properties.getProperty(KeyProp);
            System.out.println(value);
            return value;
        } catch (Exception exp) {
            System.out.println(exp.getMessage());
            System.out.println(exp.getCause());
            exp.printStackTrace();
        }
        return value;
    }

    //Xây dựng hàm Set Value với Key tương ứng vào trong file properties đã setup bên trên
    public static void setPropValue(String KeyProp, String Value) {
        try {
            //Khởi tạo giá trị cho đối tượng của class FileOutputStream
            fileOut = new FileOutputStream(projectPath + propertiesFilePathRoot);
            //Load properties file hiện tại và thực hiện mapping value với key tương ứng
            properties.setProperty(KeyProp, Value);
            //Lưu key và value vào properties file
            properties.store(fileOut, "Set new value in properties file");
            System.out.println("Set new value in file properties success.");
        } catch (Exception exp) {
            System.out.println(exp.getMessage());
            System.out.println(exp.getCause());
            exp.printStackTrace();
        }
    }

}

 
Hàm
setPropertiesFile() để khởi tạo giá trị cho đối tượng properties của class Properties với đường dẫn đến file configs.properties trên.

Tiếp theo cái hàm getPropValue() để đọc file đã setup bên trên và lấy giá trị ra theo Key trong file đã tạo.

Cuối cùng là hàm setPropValue() để gán ngược giá trị vói key tương ứng vào lại file properties trên.

Tức nhiên chổ này chúng ta muốn chỉ định file nào thì phải vào class PropertiesFile để chỉ định đường dẫn ở biến mà mình đã tạo default:

private static String propertiesFilePathRoot = "src/test/resources/configs.properties";


Đổi cái link chổ này là chơi tới nơi luôn =))

Rồi tiếp theo là gọi hàm ra sử dụng thôi nào

3. Gọi hàm để đọc properties file

Các bạn nhìn thấy là 3 cái hàm trong class PropertiesFile là trạng thái static nên khi mình gọi thì lấy luôn cái tên class để chấm gọi chứ không cần khởi tạo đối tượng của class.

Đầu tiên phải gọi hàm setPropertiesFile() để khởi tạo file

PropertiesFile.setPropertiesFile();

Trong TestNG nên là cái này để ở @BeforeClass hoặc @BeforeTest hay @BeforeSuite gì cũng được. Nghĩa là cho nó chạy trước cái hàm getPropValue() là được.

Tiếp theo muốn ĐỌC giá trị từ file properties thì gọi hàm getPropValue()

PropertiesFile.getPropValue("browser")

Cái browser là cái key trong file properties đó nhe. Hiểu bậy toang luôn đi =))

Vậy thì khi gọi ra nó sẽ trả về hết quả kiểu String thôi. Cụ thể cái giá trị của key "browser" là "chrome" đã tạo bên trên nên nó sẽ trả về giá trị là "chrome"

Ví dụ: mình gọi hàm signIn của phần bài trước để truyền giá trị email và password từ properties file

package anhtester.com.testcases;

import anhtester.com.common.BaseSetup;
import anhtester.com.common.ultilities.PropertiesFile;
import anhtester.com.pages.SignInPage;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class PropertiesTest {

    private WebDriver driver;
    private SignInPage signInPage;

    @BeforeClass
    public void createDriver() {
        // Gọi hàm để khởi tạo file properties
        PropertiesFile.setPropertiesFile();

        // Đọc data từ file properties với key là "browser"
        driver = new BaseSetup().setupDriver(PropertiesFile.getPropValue("browser"));
    }

    @Test
    public void signinCRM(){
        signInPage = new SignInPage(driver);
        driver.get("https://crm.anhtester.com");

        // Đọc data từ file properties
        signInPage.signIn(PropertiesFile.getPropValue("email"),PropertiesFile.getPropValue("password"));

    }

    @AfterClass
    public void closeDriver() {
        driver.quit();
    }
}


Bạn muốn giá trị của các key là gì thì vào file đổi thôi rồi chạy lại là xong phim.

Giờ mới phần SET giá trị ngược vào file thì gọi hàm setPropValue() rồi truyền 2 tham số tuần tự là "key" và "value" tương ứng là xong

Ví dụ: tạo hàm thêm test case mới là signinCRM_AfterSetValue() để set giá trị xong chạy lại cái signin bên trên xem nó khác giá trị không nhen.

package anhtester.com.testcases;

import anhtester.com.common.BaseSetup;
import anhtester.com.common.ultilities.PropertiesFile;
import anhtester.com.pages.SignInPage;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class PropertiesTest {

    private WebDriver driver;
    private SignInPage signInPage;

    @BeforeClass
    public void createDriver() {
        // Gọi hàm để khởi tạo file properties
        PropertiesFile.setPropertiesFile();

        // Đọc data từ file properties với key là "browser"
        driver = new BaseSetup().setupDriver(PropertiesFile.getPropValue("browser"));
    }

    @Test
    public void signinCRM(){
        signInPage = new SignInPage(driver);
        driver.get("https://crm.anhtester.com");

        // Đọc data từ file properties
        signInPage.signIn(PropertiesFile.getPropValue("email"),PropertiesFile.getPropValue("password"));

    }

    @Test
    public void signinCRM_AfterSetValue(){
        signInPage = new SignInPage(driver);
        driver.get("https://crm.anhtester.com");

        //Set giá trị vào file properties
        PropertiesFile.setPropValue("email", "admin02@mailinator.com");

        // Đọc data từ file properties
        signInPage.signIn(PropertiesFile.getPropValue("email"),PropertiesFile.getPropValue("password"));

    }

    @AfterClass
    public void closeDriver() {
        driver.quit();
    }
}


Khi đó thì thấy là từ email là "tld01@mailinator.com" nó chuyển thành admin02@mailinator.com rồi đúng không =))

Ùm nếu đúng thiệt dị thì được rồi đó

Mở file properties ra coi luôn cho máu

[Selenium Java] Bài 27: Đọc data test từ Properties file trong Selenium Java | Anh Tester

Nếu mà key bị trùng thì nó ghi đè giá trị.

Còn nếu không trùng key thì nó tạo ra thêm 1 dòng mới với key mới và giá trị tương ứng

Ví dụ: mình set 1 biến mới là "pin" với giá trị là "123"

PropertiesFile.setPropValue("pin", "123");



[Selenium Java] Bài 27: Đọc data test từ Properties file trong Selenium Java | Anh Tester

Hết rồi !!!


Mà đọc lần có 1 file thấy hơi cực hen =))

Còn phần mở rộng sẽ là đọc nhiều file properties một lượt 5 10 file gì đó chẵn hạn. Các bạn nghiên cứu thử. An làm rồi mà giờ này buồn ngủ quá. Gần 2h sáng. Nào rảnh An chỉ tiếp cho.

Yeah cảm ơn các bạn rất nhiều. Hãy chia sẻ để cùng nhau học tập tiến bộ hơn mỗi ngày nhé !!!

  • Tags:
  • properties file
  • get data
  • set data

Chia sẻ bài viết

Facebook Linkedin Telegram Pinterest Share with Zalo Zalo

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

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

  • Anh Tester

    Anh Tester

    Đườ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

    • Facebook
    • Youtube
    • Zalo

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: new

Filename: post/post_detail.php

Line Number: 384

Backtrace:

File: /home/anhtest2/public_html/application/views/frontend/post/post_detail.php
Line: 384
Function: _error_handler

File: /home/anhtest2/public_html/application/views/frontend/layout/layout_view.php
Line: 569
Function: view

File: /home/anhtest2/public_html/application/core/MY_Controller.php
Line: 34
Function: view

File: /home/anhtest2/public_html/application/controllers/frontend/Post.php
Line: 59
Function: render

File: /home/anhtest2/public_html/index.php
Line: 315
Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Trying to get property 'slug' of non-object

Filename: post/post_detail.php

Line Number: 384

Backtrace:

File: /home/anhtest2/public_html/application/views/frontend/post/post_detail.php
Line: 384
Function: _error_handler

File: /home/anhtest2/public_html/application/views/frontend/layout/layout_view.php
Line: 569
Function: view

File: /home/anhtest2/public_html/application/core/MY_Controller.php
Line: 34
Function: view

File: /home/anhtest2/public_html/application/controllers/frontend/Post.php
Line: 59
Function: render

File: /home/anhtest2/public_html/index.php
Line: 315
Function: require_once

https://anhtester.com//blog/selenium-java/" data-width="100%" data-numposts="4">

Search Blogs

Related Blogs

Tạo dữ liệu giả với DataFaker

Tạo dữ liệu giả với DataFaker

May-18-2023 by Anh Tester
[Selenium Java] Cài đặt môi trường Java JDK và IDE để code

[Selenium Java] Cài đặt môi trường Java JDK và IDE để code

Sep-05-2022 by Anh Tester
Setting and custom Plugin Theme on IntelliJ IDE

Setting and custom Plugin Theme on IntelliJ IDE

Jul-28-2022 by Anh Tester
Xử lý phân trang trên Data Table trong Selenium Java

Xử lý phân trang trên Data Table trong Selenium Java

May-04-2022 by Anh Tester
Upload File trong Selenium Java

Upload File trong Selenium Java

May-04-2022 by Anh Tester
[Selenium Java] Bài 32: Quản lý source code Selenium Java trên Github/Gitlab

[Selenium Java] Bài 32: Quản lý source code Selenium Java trên Github/Gitlab

Mar-16-2022 by Anh Tester
Tạo project Selenium Java và TestNG Framework với Maven trên IntelliJ IDE

Tạo project Selenium Java và TestNG Framework với Maven trên IntelliJ IDE

Jan-09-2022 by Anh Tester
Tạo project Selenium Java với IntelliJ IDE

Tạo project Selenium Java với IntelliJ IDE

Jan-09-2022 by Anh Tester
[Selenium Java] Bài 31: Cài đặt và sử dụng Extent Report Allure Report

[Selenium Java] Bài 31: Cài đặt và sử dụng Extent Report Allure Report

Dec-21-2021 by Anh Tester
[Selenium Java] Bài 30: Sử dụng Log4j để ghi Log vào file trong Selenium Java

[Selenium Java] Bài 30: Sử dụng Log4j để ghi Log vào file trong Selenium Java

Dec-17-2021 by Anh Tester
view all

Blog Tags

  • Selenium
  • Xpath
  • Locator
  • Jenkins
  • Testing
  • Tester
  • Thuật ngữ
  • Lộ trình
  • Khóa học
  • Mindset
  • QA
  • QC
  • Checklist
  • Website
  • Mobile
  • Question
  • Answer
  • Phỏng vấn
  • Extension
  • Cucumber
  • Gherkin
  • Agile
  • Scrum
  • Document
  • Testing Level
  • Automation Test
  • Test Cases
  • Trường hợp
  • Katalon
  • JMeter
  • Postman
  • API
  • Manual Test
  • Developer

Anh Tester

Anh Tester profile
Đườ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

Connect me on

  • Facebook
  • Youtube
  • Zalo


Liên hệ

  • 0939206009
  • thaian.it15@gmail.com
  • Anh Tester
  • Donate for Anh Tester
QR Facebook Group
QR Discord Group

Copyright © 2021-2025 Anh Tester Automation Testing