Skip to content

Commit 804a7cc

Browse files
committed
Bài 22 - JSON File - device.json
1 parent fa0d3cf commit 804a7cc

File tree

7 files changed

+104
-8
lines changed

7 files changed

+104
-8
lines changed

src/main/java/com/anhtester/helpers/PropertiesHelpers.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.anhtester.helpers;
22

3+
import java.io.File;
34
import java.io.FileInputStream;
45
import java.io.FileOutputStream;
56
import java.io.IOException;
@@ -117,4 +118,30 @@ public static void setValue(String filePropertiesRelativePath, String key, Strin
117118
}
118119
}
119120

121+
public static void removeKey(String filePropertiesRelativePath, String key) {
122+
try {
123+
// Đọc file properties
124+
Properties properties = new Properties();
125+
File file = new File(SystemHelpers.getCurrentDir() + filePropertiesRelativePath);
126+
FileInputStream inputStream = new FileInputStream(file);
127+
properties.load(inputStream);
128+
inputStream.close();
129+
130+
// Xoá key nếu tồn tại
131+
if (properties.containsKey(key)) {
132+
properties.remove(key);
133+
System.out.println("🔑 Đã xoá key: " + key);
134+
} else {
135+
System.out.println("⚠️ Key không tồn tại: " + key);
136+
}
137+
138+
// Ghi lại file
139+
FileOutputStream outputStream = new FileOutputStream(file);
140+
properties.store(outputStream, null);
141+
outputStream.close();
142+
} catch (IOException e) {
143+
e.printStackTrace();
144+
}
145+
}
146+
120147
}

src/test/java/com/anhtester/Bai21_Config_File/DemoPropertiesFile.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@ public void testReadPropertiesFile() {
1818
public void testSetValueToPropertiesFile() {
1919
PropertiesHelpers.setValue(
2020
"src/test/resources/test_data/data.properties",
21-
"platform",
22-
"Android");
21+
"PLATFORM",
22+
"iOS");
23+
24+
PropertiesHelpers.setValue(
25+
"src/test/resources/test_data/data.properties",
26+
"DEVICE",
27+
"Pixel 8");
28+
29+
PropertiesHelpers.removeKey("src/test/resources/test_data/data.properties",
30+
"platform");
31+
PropertiesHelpers.removeKey("src/test/resources/test_data/data.properties",
32+
"device");
2333
}
2434
}

src/test/java/com/anhtester/Bai22_JSON_File/DemoJsonFile.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public void testReadJsonFile() {
1313
JsonHelpers.getValueJsonObject("account", "username");
1414
JsonHelpers.getValueJsonObject("account", "password");
1515

16+
JsonHelpers.getValueJsonObject("table", "table_name");
17+
1618
JsonHelpers.getValueJsonObject("product", "product_name");
1719
JsonHelpers.getValueJsonObject("product", "product_price");
1820
JsonHelpers.getValueJsonObject("product", "hidden_product");
@@ -57,7 +59,7 @@ public void testUpdateValueToJsonFile() {
5759
"profile", "job_title", "Tester");
5860

5961
//Update value in JSON array
60-
JsonHelpers.updateValueJsonArray_FilePath("src/test/resources/test_data/sample_products.json", "20000", 0, "price", "amount");
61-
62+
JsonHelpers.updateValueJsonArray_FilePath("src/test/resources/test_data/sample_products.json", "24Gb", 1, "specs", "ram");
63+
6264
}
6365
}

src/test/resources/configs/device.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@
2929
}
3030
},
3131
"ios": {
32+
"devices": {
33+
"iphone14": {
34+
"platformName": "iOS",
35+
"platformVersion": "17",
36+
"automationName": "XCUITest",
37+
"deviceName": "iPhone 14 Pro Max",
38+
"appPackage": "com.anhtester.mobile_app.taurus",
39+
"appActivity": "com.anhtester.mobile_app.taurus.MainActivity",
40+
"appPath": "path to app",
41+
"noReset": false,
42+
"fullReset": false,
43+
"autoGrantPermissions": true
44+
},
45+
"iphone15": {
46+
"platformName": "iOS",
47+
"platformVersion": "18",
48+
"automationName": "XCUITest",
49+
"deviceName": "iPhone 15 Pro Max",
50+
"appPackage": "com.anhtester.mobile_app.taurus",
51+
"appActivity": "com.anhtester.mobile_app.taurus.MainActivity",
52+
"appPath": "path to app",
53+
"noReset": false,
54+
"fullReset": false,
55+
"autoGrantPermissions": true
56+
}
57+
}
3258
}
3359
}
3460
}
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
1-
{"platform":"Android","device":"Pixel 8","account":{"username":"admin","password":"admin"},"table":{"table_name":"Table 1","hidden_table":"false"},"product":{"product_name":"Product 3","product_price":140000,"hidden_product":"false","product_image":"src/test/resources/test_data/product1.png"},"profile":{"name":"Anh Tester","email":"[email protected]","phone":"0939206009","address":"HCM","job_title":"Tester"},"database_name":"Data 4","database_index":4,"database":{"database_name":"Data 1","database_index":1}}
1+
{
2+
"platform": "Android",
3+
"device": "Pixel 8",
4+
"account": {
5+
"username": "admin",
6+
"password": "admin"
7+
},
8+
"table": {
9+
"table_name": "Table 1",
10+
"hidden_table": "false"
11+
},
12+
"product": {
13+
"product_name": "Product 3",
14+
"product_price": 140000,
15+
"hidden_product": "false",
16+
"product_image": "src/test/resources/test_data/product1.png"
17+
},
18+
"profile": {
19+
"name": "Anh Tester",
20+
"email": "[email protected]",
21+
"phone": "0939206009",
22+
"address": "HCM",
23+
"job_title": "Tester"
24+
},
25+
"database_name": "Data 4",
26+
"database_index": 4,
27+
"database": {
28+
"database_name": "Data 1",
29+
"database_index": 1
30+
}
31+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
#Wed Apr 16 15:09:42 ICT 2025
2-
platform=Android
1+
#Thu Apr 17 20:20:57 ICT 2025
2+
PLATFORM = iOS
3+
DEVICE = Pixel 8

src/test/resources/test_data/sample_products.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"name": "MacBook Pro M2",
1919
"specs": {
2020
"cpu": "Apple M2",
21-
"ram": "16GB",
21+
"ram": "24Gb",
2222
"storage": "1TB SSD"
2323
},
2424
"price": {

0 commit comments

Comments
 (0)