NỘI DUNG BÀI HỌC
🔆 Kế thừa các phần trước là Page Object Model 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.
browser=chrome
email=teamleader@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 class Helpers đặt trong package helpers để lưu các hàm xử lý bổ trợ cho các class khác.
public static String getCurrentDir() {
String current = System.getProperty("user.dir") + "/";
return current;
}
Tạo thêm một lớp mới có tên là "PropertiesHelper" . Có thể đặt nó ở package helper luôn
Để đọ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.helpers;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Properties;
public class PropertiesHelper {
private static Properties properties;
private static String linkFile;
private static FileInputStream file;
private static FileOutputStream out;
private static String relPropertiesFilePathDefault = "src/main/resources/configs.properties";
public static Properties loadAllFiles() {
LinkedList<String> files = new LinkedList<>();
// Add tất cả file Properties vào đây theo mẫu
files.add("src/main/resources/configs.properties");
files.add("src/main/resources/dataTest.properties");
try {
properties = new Properties();
for (String f : files) {
Properties tempProp = new Properties();
linkFile = Helpers.getCurrentDir() + f;
file = new FileInputStream(linkFile);
tempProp.load(file);
properties.putAll(tempProp);
}
return properties;
} catch (IOException ioe) {
return new Properties();
}
}
public static void setFile(String relPropertiesFilePath) {
properties = new Properties();
try {
linkFile = Helpers.getCurrentDir() + relPropertiesFilePath;
file = new FileInputStream(linkFile);
properties.load(file);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setDefaultFile() {
properties = new Properties();
try {
linkFile = Helpers.getCurrentDir() + relPropertiesFilePathDefault;
file = new FileInputStream(linkFile);
properties.load(file);
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getValue(String key) {
String keyval = null;
try {
if (file == null) {
properties = new Properties();
linkFile = Helpers.getCurrentDir() + relPropertiesFilePathDefault;
file = new FileInputStream(linkFile);
properties.load(file);
file.close();
}
// Lấy giá trị từ file đã Set
keyval = properties.getProperty(key);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return keyval;
}
public static void setValue(String key, String keyValue) {
try {
if (file == null) {
properties = new Properties();
file = new FileInputStream(Helpers.getCurrentDir() + relPropertiesFilePathDefault);
properties.load(file);
file.close();
out = new FileOutputStream(Helpers.getCurrentDir() + relPropertiesFilePathDefault);
}
//Ghi vào cùng file Prop với file lấy ra
out = new FileOutputStream(linkFile);
System.out.println(linkFile);
properties.setProperty(key, keyValue);
properties.store(out, null);
out.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Hàm loadAllFiles()
để khởi tạo giá trị cho nhiều file properties setup sẵn trong hàm.
Hàm setFile()
để 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.
Hàm setDefaultFile()
để khởi tạo giá trị cho đối tượng properties của class Properties với đường dẫn có sẵn trong class.
Tiếp theo cái hàm getValue()
để đọ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 setValue()
để 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 PropertiesHelper
để chỉ định đường dẫn ở biến mà mình đã tạo default:
private static String relPropertiesFilePathDefault = "src/main/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 loadAllFiles()
để khởi tạo tất cả các file properties chỉ định trong chính hàm này
PropertiesHelper.loadAllFiles();
Còn nếu muốn chỉ định ở file cụ thể thì gọi hàm setFile
(file path)
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 getValue() là được.
Tiếp theo muốn ĐỌC giá trị từ file properties thì gọi hàm getPropValue()
PropertiesHelper.getValue("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.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
PropertiesHelper.loadAllFiles();
// Đọc data từ file properties với key là "browser"
driver = new SetupBrowser().createBrowser(PropertiesHelper.getValue("browser"));
}
@Test
public void signinCRM(){
signInPage = new SignInPage(driver);
driver.get("https://crm.anhtester.com");
// Đọc data từ file properties
signInPage.signIn(PropertiesHelper.getValue("email"),PropertiesHelper.getValue("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.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
PropertiesHelper.loadAllFiles();
// Đọc data từ file properties với key là "browser"
driver = new SetupBrowser().createBrowser(PropertiesHelper.getValue("browser"));
}
@Test
public void signinCRM(){
signInPage = new SignInPage(driver);
driver.get("https://crm.anhtester.com");
// Đọc data từ file properties
signInPage.signIn(PropertiesHelper.getValue("email"),PropertiesHelper.getValue("password"));
}
@Test
public void signinCRM_AfterSetValue(){
signInPage = new SignInPage(driver);
driver.get("https://crm.anhtester.com");
//Set giá trị vào file properties
PropertiesHelper.setValue("email", "admin@mailinator.com");
// Đọc data từ file properties
signInPage.signIn(PropertiesHelper.getValue("email"),PropertiesHelper.getValue("password"));
}
@AfterClass
public void closeDriver() {
driver.quit();
}
}
Khi đó thì thấy là từ email là "teamleader@mailinator.com" nó chuyển thành admin@mailinator.com rồi đúng không 😜
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"
PropertiesHelper.setValue("pin", "123");