Skip to content

Commit 2c52b09

Browse files
committed
Bài 27 - Read Properties file
1 parent eadaf3e commit 2c52b09

File tree

13 files changed

+191
-9
lines changed

13 files changed

+191
-9
lines changed
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package com.anhtester.constants;
22

3+
import com.anhtester.helpers.PropertiesHelper;
4+
35
public class ConfigData {
4-
public static String URL = "https://crm.anhtester.com/admin/authentication";
5-
public static String EMAIL = "admin@example.com";
6-
public static String PASSWORD = "123456";
6+
// public static String URL = "https://crm.anhtester.com/admin/authentication";
7+
// public static String EMAIL = "admin@example.com";
8+
// public static String PASSWORD = "123456";
9+
10+
static {
11+
PropertiesHelper.loadAllFiles();
12+
}
13+
14+
public static String URL = PropertiesHelper.getValue("url");
15+
public static String EMAIL = PropertiesHelper.getValue("email");
16+
public static String PASSWORD = PropertiesHelper.getValue("password");
717
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.anhtester.helpers;
2+
3+
import com.anhtester.helpers.SystemHelper;
4+
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.util.LinkedList;
9+
import java.util.Properties;
10+
11+
public class PropertiesHelper {
12+
13+
private static Properties properties;
14+
private static String linkFile;
15+
private static FileInputStream file;
16+
private static FileOutputStream out;
17+
private static String relPropertiesFilePathDefault = "src/test/resources/configs/configs.properties";
18+
19+
public static Properties loadAllFiles() {
20+
LinkedList<String> files = new LinkedList<>();
21+
// Add tất cả file Properties vào đây theo mẫu
22+
files.add("src/test/resources/configs/configs.properties");
23+
files.add("src/test/resources/configs/file1.properties");
24+
files.add("src/test/resources/configs/file2.properties");
25+
26+
try {
27+
properties = new Properties();
28+
29+
for (String f : files) {
30+
Properties tempProp = new Properties();
31+
linkFile = SystemHelper.getCurrentDir() + f;
32+
file = new FileInputStream(linkFile);
33+
tempProp.load(file);
34+
properties.putAll(tempProp);
35+
}
36+
return properties;
37+
} catch (IOException ioe) {
38+
return new Properties();
39+
}
40+
}
41+
42+
public static void setFile(String relPropertiesFilePath) {
43+
properties = new Properties();
44+
try {
45+
linkFile = SystemHelper.getCurrentDir() + relPropertiesFilePath;
46+
file = new FileInputStream(linkFile);
47+
properties.load(file);
48+
file.close();
49+
} catch (Exception e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
54+
public static void setDefaultFile() {
55+
properties = new Properties();
56+
try {
57+
linkFile = SystemHelper.getCurrentDir() + relPropertiesFilePathDefault;
58+
file = new FileInputStream(linkFile);
59+
properties.load(file);
60+
file.close();
61+
} catch (Exception e) {
62+
e.printStackTrace();
63+
}
64+
}
65+
66+
public static String getValue(String key) {
67+
String keyValue = null;
68+
try {
69+
if (file == null) {
70+
properties = new Properties();
71+
linkFile = SystemHelper.getCurrentDir() + relPropertiesFilePathDefault;
72+
file = new FileInputStream(linkFile);
73+
properties.load(file);
74+
file.close();
75+
}
76+
// Lấy giá trị từ file đã Set
77+
keyValue = properties.getProperty(key);
78+
} catch (Exception e) {
79+
System.out.println(e.getMessage());
80+
}
81+
return keyValue;
82+
}
83+
84+
public static void setValue(String key, String keyValue) {
85+
try {
86+
if (file == null) {
87+
properties = new Properties();
88+
file = new FileInputStream(SystemHelper.getCurrentDir() + relPropertiesFilePathDefault);
89+
properties.load(file);
90+
file.close();
91+
out = new FileOutputStream(SystemHelper.getCurrentDir() + relPropertiesFilePathDefault);
92+
}
93+
//Ghi vào cùng file Prop với file lấy ra
94+
out = new FileOutputStream(linkFile);
95+
System.out.println(linkFile);
96+
properties.setProperty(key, keyValue);
97+
properties.store(out, null);
98+
out.close();
99+
} catch (Exception e) {
100+
System.out.println(e.getMessage());
101+
}
102+
}
103+
104+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.anhtester.helpers;
2+
3+
import java.io.File;
4+
5+
public class SystemHelper {
6+
7+
public static String getCurrentDir(){
8+
String path = System.getProperty("user.dir") + File.separator;
9+
return path;
10+
}
11+
12+
}

src/test/java/com/anhtester/Bai26_CustomDriverParallelExecution/testcases/DashboardTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.anhtester.Bai26_CustomDriverParallelExecution.pages.LoginPage;
66
import com.anhtester.common.BaseTest;
77
import com.anhtester.constants.ConfigData;
8+
import com.anhtester.helpers.PropertiesHelper;
89
import org.testng.annotations.Test;
910

1011
public class DashboardTest extends BaseTest {
@@ -29,7 +30,7 @@ public void testOpenCustomerPage(){
2930
@Test
3031
public void testAdminRole(){
3132
loginPage = new LoginPage();
32-
dashboardPage = loginPage.loginCRM("admin@example.com", "123456");
33+
dashboardPage = loginPage.loginCRM(PropertiesHelper.getValue("email"), PropertiesHelper.getValue("password"));
3334
loginPage.verifyLoginSuccess();
3435
dashboardPage.verifyMenuReportDisplay();
3536
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.anhtester.Bai27_ReadPropertiesFile;
2+
3+
import com.anhtester.constants.ConfigData;
4+
import com.anhtester.helpers.PropertiesHelper;
5+
import org.testng.annotations.Test;
6+
7+
public class DemoReadProperties {
8+
@Test
9+
public void testReadPropertiesFile(){
10+
PropertiesHelper.loadAllFiles();
11+
12+
System.out.println(ConfigData.URL);
13+
System.out.println(PropertiesHelper.getValue("email"));
14+
System.out.println(PropertiesHelper.getValue("password"));
15+
16+
//Gộp nhiều file
17+
System.out.println(PropertiesHelper.getValue("key1"));
18+
System.out.println(PropertiesHelper.getValue("key2"));
19+
20+
//Set file
21+
PropertiesHelper.setFile("src/test/resources/configs/configs.properties");
22+
PropertiesHelper.setValue("message", "Add customer successfully");
23+
}
24+
}

src/test/java/com/anhtester/common/BaseTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
package com.anhtester.common;
22

33
import com.anhtester.drivers.DriverManager;
4+
import com.anhtester.helpers.PropertiesHelper;
45
import org.openqa.selenium.WebDriver;
56
import org.openqa.selenium.chrome.ChromeDriver;
67
import org.openqa.selenium.edge.EdgeDriver;
78
import org.openqa.selenium.firefox.FirefoxDriver;
8-
import org.testng.annotations.AfterMethod;
9-
import org.testng.annotations.BeforeMethod;
10-
import org.testng.annotations.Optional;
11-
import org.testng.annotations.Parameters;
9+
import org.testng.annotations.*;
1210

1311
public class BaseTest {
1412

13+
@BeforeSuite
14+
public void beforeSuite(){
15+
//Chỉ cần load 1 lần là đã lưu giá trị vào bộ nhớ tạm, áp dụng cho toàn phiên chạy
16+
PropertiesHelper.loadAllFiles();
17+
}
18+
1519
@BeforeMethod
1620
@Parameters({"browser"})
1721
public void createDriver(@Optional("chrome") String browserName) {
18-
WebDriver driver = setupBrowser(browserName); //Khởi tạo loại browser và gán vào driver
22+
WebDriver driver = setupBrowser(PropertiesHelper.getValue("browser")); //Khởi tạo loại browser và gán vào driver
1923
//new WebUI(driver);
2024
DriverManager.setDriver(driver); //Mang giá trị driver đã khởi tạo vào trong ThreadLocal
2125
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Wed Nov 08 21:44:40 ICT 2023
2+
browser=firefox
3+
email=admin@example.com
4+
message=Add customer successfully
5+
password=123456
6+
url=https\://crm.anhtester.com/admin/authentication
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
key1 = 12345
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#Wed Nov 08 21:43:09 ICT 2023
2+
browser=firefox
3+
email=admin@example.com
4+
key1=abcd
5+
key2=56789
6+
message=Add customer successfully
7+
password=123456
8+
url=https\://crm.anhtester.com/admin/authentication
Loading

0 commit comments

Comments
 (0)