Skip to content

Commit 04a327c

Browse files
committed
Update framework v2.6.0
1 parent 3f30714 commit 04a327c

File tree

6 files changed

+214
-77
lines changed

6 files changed

+214
-77
lines changed

CHANGELOG.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
v2.6.0
2+
======
3+
May 05, 2025
4+
* Update Selenium 4.32.0 support CDP 136
5+
* Upgrade all libraries latest version
6+
* Update FileHelpers class
7+
* Update SystemHelpers class
8+
* Update ExcelHelpers class
9+
110
v2.5.0
211
======
312
Feb 23, 2025

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.anhtester</groupId>
88
<artifactId>anhtester-selenium-java</artifactId>
9-
<version>2.5.0</version>
9+
<version>2.6.0</version>
1010
<name>AutomationFrameworkSelenium</name>
1111
<url>https://github.com/anhtester/AutomationFrameworkSelenium</url>
1212
<description>Test Automation Framework Selenium Java with TestNG building by Anh Tester</description>
@@ -40,18 +40,18 @@
4040
<apache-poi.version>5.4.1</apache-poi.version>
4141
<commons-io.version>2.19.0</commons-io.version>
4242
<owner.version>1.0.12</owner.version>
43-
<assertj.version>4.0.0-M1</assertj.version>
43+
<assertj.version>3.27.3</assertj.version>
4444
<datafaker.version>2.4.3</datafaker.version>
4545
<monte-screen-recorder.version>0.7.7.0</monte-screen-recorder.version>
4646
<mysql-connector-java.version>8.0.33</mysql-connector-java.version>
4747
<lombok.version>1.18.38</lombok.version>
4848
<javax.mail.version>1.6.2</javax.mail.version>
4949
<zip.version>1.17</zip.version>
50-
<jackson.version>2.19.0</jackson.version>
5150
<java-telegram-bot-api.version>8.3.0</java-telegram-bot-api.version>
5251
<commons-lang3.version>3.17.0</commons-lang3.version>
5352
<mysql-connector-j.version>9.3.0</mysql-connector-j.version>
5453
<json-path.version>2.9.0</json-path.version>
54+
<jackson.version>2.19.0</jackson.version>
5555

5656
<!-- Suite XML path - CRM -->
5757
<suite.signin.simple>src/test/resources/suites/SignIn-simple.xml</suite.signin.simple>

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

Lines changed: 123 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
1212
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
1313

14-
import java.io.File;
15-
import java.io.FileInputStream;
16-
import java.io.FileOutputStream;
17-
import java.io.IOException;
14+
import java.io.*;
15+
import java.util.Arrays;
1816
import java.util.HashMap;
1917
import java.util.Hashtable;
2018
import java.util.Map;
@@ -216,14 +214,128 @@ public Object[][] getDataHashTable(String excelPath, String sheetName, int start
216214

217215
}
218216

219-
public String getTestCaseName(String testCaseName) {
220-
String value = testCaseName;
221-
int position = value.indexOf("@");
222-
value = value.substring(0, position);
223-
position = value.lastIndexOf(".");
217+
// Get data from specific rows
218+
public Object[][] getDataFromSpecificRows(String excelPath, String sheetName, int[] rowNumbers) {
219+
System.out.println("Excel File: " + excelPath);
220+
System.out.println("Sheet Name: " + sheetName);
221+
System.out.println("Reading data from specific rows: " + Arrays.toString(rowNumbers));
224222

225-
value = value.substring(position + 1);
226-
return value;
223+
Object[][] data = null;
224+
225+
try {
226+
File f = new File(excelPath);
227+
228+
if (!f.exists()) {
229+
System.out.println("File Excel path not found.");
230+
throw new FileNotFoundException("File Excel path not found.");
231+
}
232+
233+
fis = new FileInputStream(excelPath);
234+
workbook = WorkbookFactory.create(fis);
235+
sheet = workbook.getSheet(sheetName);
236+
237+
if (sheet == null) {
238+
System.out.println("Sheet name not found.");
239+
throw new RuntimeException("Sheet name not found.");
240+
}
241+
242+
int columns = getColumns();
243+
System.out.println("Column count: " + columns);
244+
245+
// Khởi tạo mảng data với kích thước bằng số lượng dòng được chỉ định
246+
data = new Object[rowNumbers.length][columns];
247+
248+
// Đọc dữ liệu từ các dòng được chỉ định
249+
for (int i = 0; i < rowNumbers.length; i++) {
250+
int rowNum = rowNumbers[i];
251+
// Kiểm tra xem dòng có tồn tại không
252+
if (rowNum > sheet.getLastRowNum()) {
253+
System.out.println("WARNING: Row " + rowNum + " does not exist in the sheet.");
254+
// Gán giá trị rỗng cho dòng không tồn tại
255+
for (int j = 0; j < columns; j++) {
256+
data[i][j] = "";
257+
}
258+
continue;
259+
}
260+
261+
for (int j = 0; j < columns; j++) {
262+
data[i][j] = getCellData(rowNum, j);
263+
}
264+
}
265+
266+
// Đóng workbook và FileInputStream
267+
workbook.close();
268+
fis.close();
269+
270+
} catch (Exception e) {
271+
System.out.println("Exception in getDataFromSpecificRows: " + e.getMessage());
272+
e.printStackTrace();
273+
}
274+
275+
return data;
276+
}
277+
278+
// Get data from specific rows with Hashtable
279+
public Object[][] getDataHashTableFromSpecificRows(String excelPath, String sheetName, int[] rowNumbers) {
280+
System.out.println("Excel File: " + excelPath);
281+
System.out.println("Sheet Name: " + sheetName);
282+
System.out.println("Reading data from specific rows: " + Arrays.toString(rowNumbers));
283+
284+
Object[][] data = null;
285+
286+
try {
287+
File f = new File(excelPath);
288+
289+
if (!f.exists()) {
290+
System.out.println("File Excel path not found.");
291+
throw new FileNotFoundException("File Excel path not found.");
292+
}
293+
294+
fis = new FileInputStream(excelPath);
295+
workbook = WorkbookFactory.create(fis);
296+
sheet = workbook.getSheet(sheetName);
297+
298+
if (sheet == null) {
299+
System.out.println("Sheet name not found.");
300+
throw new RuntimeException("Sheet name not found.");
301+
}
302+
303+
int columns = getColumns();
304+
// Khởi tạo mảng data với kích thước bằng số lượng dòng được chỉ định
305+
data = new Object[rowNumbers.length][1];
306+
307+
// Đọc dữ liệu từ các dòng được chỉ định
308+
for (int i = 0; i < rowNumbers.length; i++) {
309+
int rowNum = rowNumbers[i];
310+
// Kiểm tra xem dòng có tồn tại không
311+
if (rowNum > sheet.getLastRowNum()) {
312+
System.out.println("WARNING: Row " + rowNum + " does not exist in the sheet.");
313+
data[i][0] = new Hashtable<String, String>();
314+
continue;
315+
}
316+
317+
Hashtable<String, String> table = new Hashtable<>();
318+
for (int j = 0; j < columns; j++) {
319+
// Lấy tên cột từ dòng đầu tiên (header)
320+
String columnName = getCellData(0, j);
321+
// Lấy giá trị từ dòng hiện tại và cột j
322+
String cellValue = getCellData(rowNum, j);
323+
// Thêm vào Hashtable
324+
table.put(columnName, cellValue);
325+
}
326+
data[i][0] = table;
327+
}
328+
329+
// Đóng workbook và FileInputStream
330+
workbook.close();
331+
fis.close();
332+
333+
} catch (Exception e) {
334+
System.out.println("Exception in getDataHashTableFromSpecificRows: " + e.getMessage());
335+
e.printStackTrace();
336+
}
337+
338+
return data;
227339
}
228340

229341
public int getRowContains(String sTestCaseName, int colNum) {

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

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.anhtester.utils.LogUtils;
99

1010
import java.io.*;
11+
import java.nio.charset.StandardCharsets;
1112
import java.nio.file.Files;
1213
import java.nio.file.Paths;
1314
import java.nio.file.StandardCopyOption;
@@ -21,48 +22,76 @@ public FileHelpers() {
2122
super();
2223
}
2324

25+
public static String readFile(String filePath) {
26+
StringBuilder content = new StringBuilder();
27+
try (BufferedReader reader = new BufferedReader(
28+
new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8))) {
29+
char[] buffer = new char[8192];
30+
int read;
31+
while ((read = reader.read(buffer)) != -1) {
32+
content.append(buffer, 0, read);
33+
}
34+
} catch (FileNotFoundException e) {
35+
throw new RuntimeException(e);
36+
} catch (IOException e) {
37+
throw new RuntimeException(e);
38+
}
39+
return content.toString();
40+
}
41+
2442
public static void writeTxtFile(String filepath, String text) {
43+
File file = new File(filepath);
44+
2545
try {
26-
File file = new File(filepath);
27-
while (!file.exists()) {
46+
if (!file.exists()) {
2847
file.createNewFile();
2948
}
30-
FileWriter fw = new FileWriter(file, true);
31-
BufferedWriter bw = new BufferedWriter(fw);
32-
bw.write(text + "\n" + "\n");
33-
bw.close();
49+
50+
// Thay mọi kiểu newline về chuẩn Windows \r\n
51+
String normalizedText = text.replaceAll("\\r?\\n", "\r\n");
52+
53+
try (BufferedWriter bw = new BufferedWriter(
54+
new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8))) {
55+
bw.write(normalizedText);
56+
}
57+
3458
} catch (IOException e) {
3559
e.printStackTrace();
3660
}
3761
}
3862

39-
public static void readTxtFile(String filepath) {
40-
try {
41-
File f = new File(filepath);
42-
FileReader fr = new FileReader(f);
43-
BufferedReader br = new BufferedReader(fr);
63+
public static String readTxtFile(String filepath) {
64+
StringBuilder content = new StringBuilder();
65+
try (BufferedReader br = new BufferedReader(
66+
new InputStreamReader(new FileInputStream(filepath), StandardCharsets.UTF_8))) {
4467
String line;
4568
while ((line = br.readLine()) != null) {
46-
System.out.println(line);
69+
content.append(line).append(System.lineSeparator());
4770
}
48-
fr.close();
49-
br.close();
50-
} catch (FileNotFoundException e) {
51-
e.printStackTrace();
5271
} catch (IOException e) {
5372
e.printStackTrace();
5473
}
74+
return content.toString();
5575
}
5676

57-
public static String readLineTxtFile(String filepath, int line) {
58-
List<String> lines;
59-
String value;
60-
try {
61-
lines = Files.readAllLines(new File(filepath).toPath());
62-
value = lines.get(line);
63-
return value;
77+
public static String readLineTxtFile(String filepath, int lineNumber) {
78+
try (BufferedReader br = new BufferedReader(
79+
new InputStreamReader(new FileInputStream(filepath), StandardCharsets.UTF_8))) {
80+
81+
String line;
82+
int currentLine = 0;
83+
84+
while ((line = br.readLine()) != null) {
85+
if (currentLine == lineNumber) {
86+
return line;
87+
}
88+
currentLine++;
89+
}
90+
91+
throw new IndexOutOfBoundsException("Dòng số " + lineNumber + " không tồn tại trong file.");
92+
6493
} catch (IOException e) {
65-
throw new RuntimeException(e);
94+
throw new RuntimeException("Lỗi khi đọc file: " + e.getMessage(), e);
6695
}
6796
}
6897

0 commit comments

Comments
 (0)