Nội dung bài học

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ị emailpassword 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é !!!


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

    Đườ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ẻ kiến thức lên trang

Bạn có thể đăng bài để chia sẻ kiến thức, bài viết của chính bạn lên trang Anh Tester Blog

Danh sách bài học