Skip to content

Commit 0465d88

Browse files
committed
Bài 26 Log4j2 ghi logs vào file
1 parent 75b4044 commit 0465d88

File tree

19 files changed

+704
-144
lines changed

19 files changed

+704
-144
lines changed

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
<dependencies>
1818

19+
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
20+
<dependency>
21+
<groupId>org.seleniumhq.selenium</groupId>
22+
<artifactId>selenium-java</artifactId>
23+
<version>4.33.0</version>
24+
</dependency>
25+
1926
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
2027
<dependency>
2128
<groupId>org.testng</groupId>
@@ -90,6 +97,19 @@
9097
<version>2.19.0</version>
9198
</dependency>
9299

100+
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
101+
<dependency>
102+
<groupId>org.apache.logging.log4j</groupId>
103+
<artifactId>log4j-core</artifactId>
104+
<version>2.25.0</version>
105+
</dependency>
106+
107+
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
108+
<dependency>
109+
<groupId>org.apache.logging.log4j</groupId>
110+
<artifactId>log4j-api</artifactId>
111+
<version>2.25.0</version>
112+
</dependency>
93113
</dependencies>
94114

95115
</project>

src/main/java/com/anhtester/keywords/MobileUI.java

Lines changed: 109 additions & 108 deletions
Large diffs are not rendered by default.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.anhtester.utils;
2+
3+
import org.apache.logging.log4j.LogManager;
4+
import org.apache.logging.log4j.Logger;
5+
6+
public class LogUtils {
7+
//Initialize Log4j instance
8+
private static final Logger logger = LogManager.getLogger(LogUtils.class);
9+
10+
//Info Level Logs
11+
public static void info(String message) {
12+
logger.info(message);
13+
}
14+
15+
public static void info(Object object) {
16+
logger.info(object);
17+
}
18+
19+
//Warn Level Logs
20+
public static void warn(String message) {
21+
logger.warn(message);
22+
}
23+
24+
public static void warn(Object object) {
25+
logger.warn(object);
26+
}
27+
28+
//Error Level Logs
29+
public static void error(String message) {
30+
logger.error(message);
31+
}
32+
33+
public static void error(Object object) {
34+
logger.error(object);
35+
}
36+
37+
//Fatal Level Logs
38+
public static void fatal(String message) {
39+
logger.fatal(message);
40+
}
41+
42+
//Debug Level Logs
43+
public static void debug(String message) {
44+
logger.debug(message);
45+
}
46+
47+
public static void debug(Object object) {
48+
logger.debug(object);
49+
}
50+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name=PropertiesConfigLog4j2
2+
property.filename=exports/logs/app-properties.log
3+
appenders=console, file
4+
appender.console.type=Console
5+
appender.console.name=STDOUT
6+
appender.console.layout.type=PatternLayout
7+
appender.console.layout.pattern=[%level] %d{dd-MM-yyyy HH:mm:ss} [%t] %c{1} - %msg%n
8+
appender.file.type=File
9+
appender.file.name=LOGFILE
10+
appender.file.fileName=${filename}
11+
appender.file.layout.type=PatternLayout
12+
appender.file.layout.pattern=[%level] %d{dd-MM-yyyy HH:mm:ss} [%t] %c{1} - %msg%n
13+
loggers=file
14+
logger.file.name=LOGFILE
15+
logger.file.level=info
16+
logger.file.appenderRefs=file
17+
logger.file.appenderRef.file.ref=LOGFILE
18+
rootLogger.level=info
19+
rootLogger.appenderRefs=stdout, file
20+
rootLogger.appenderRef.stdout.ref=STDOUT
21+
rootLogger.appenderRef.file.ref=LOGFILE

src/main/resources/log4j2_OFF.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="DEBUG">
3+
<Appenders>
4+
<Console name="LogToConsole" target="SYSTEM_OUT">
5+
<PatternLayout pattern="[%-5level] %d{dd-MM-yyyy HH:mm:ss} [%t] %c{1} - %msg%n"/>
6+
</Console>
7+
<File name="LogToFile" fileName="exports/logs/app-xml.log">
8+
<PatternLayout pattern="[%-5level] %d{dd-MM-yyyy HH:mm:ss} [%t] %c{1} - %msg%n"/>
9+
</File>
10+
</Appenders>
11+
<Loggers>
12+
<Logger name="Log4j2" level="debug" additivity="false">
13+
<AppenderRef ref="LogToFile"/>
14+
<AppenderRef ref="LogToConsole"/>
15+
</Logger>
16+
<Root level="info">
17+
<AppenderRef ref="LogToFile"/>
18+
<AppenderRef ref="LogToConsole"/>
19+
</Root>
20+
</Loggers>
21+
</Configuration>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.anhtester.Bai26_Log4j2.pages;
2+
3+
import com.anhtester.drivers.DriverManager;
4+
import io.appium.java_client.pagefactory.AndroidFindBy;
5+
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
6+
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
7+
import org.openqa.selenium.WebElement;
8+
import org.openqa.selenium.support.PageFactory;
9+
10+
public class BasePage {
11+
12+
//Constructor bắt buộc để init elements
13+
public BasePage() {
14+
PageFactory.initElements(new AppiumFieldDecorator(DriverManager.getDriver()), this);
15+
}
16+
17+
//Element/Locators thuộc chung cho nhiều trang
18+
@AndroidFindBy(accessibility = "Date")
19+
@iOSXCUITFindBy(accessibility = "Date")
20+
public WebElement menuDate;
21+
22+
@AndroidFindBy(accessibility = "Menu")
23+
@iOSXCUITFindBy(accessibility = "Menu")
24+
public WebElement menuMenu;
25+
26+
@AndroidFindBy(accessibility = "Wallet")
27+
@iOSXCUITFindBy(accessibility = "Wallet")
28+
public WebElement menuWallet;
29+
30+
@AndroidFindBy(accessibility = "Profile")
31+
@iOSXCUITFindBy(accessibility = "Profile")
32+
public WebElement menuProfile;
33+
34+
@AndroidFindBy(accessibility = "Config")
35+
@iOSXCUITFindBy(accessibility = "Config")
36+
public WebElement menuConfig;
37+
38+
@AndroidFindBy(accessibility = "Open navigation menu")
39+
@iOSXCUITFindBy(accessibility = "Open navigation menu")
40+
public WebElement openNavigationLeftMenu;
41+
42+
@AndroidFindBy(accessibility = "Web view")
43+
@iOSXCUITFindBy(accessibility = "Web view")
44+
public WebElement itemWebView;
45+
46+
@AndroidFindBy(accessibility = "Back")
47+
@iOSXCUITFindBy(accessibility = "Back")
48+
public WebElement buttonBack;
49+
50+
//Các hàm xử lý chung cho nhiều trang đều có
51+
public void clickMenuDate() {
52+
menuDate.click();
53+
}
54+
55+
public MenuPage clickMenuMenu() {
56+
menuMenu.click();
57+
return new MenuPage();
58+
}
59+
60+
public void clickMenuWallet() {
61+
menuWallet.click();
62+
}
63+
64+
public ProfilePage clickMenuProfile() {
65+
menuProfile.click();
66+
return new ProfilePage();
67+
}
68+
69+
public ConfigPage clickMenuConfig() {
70+
menuConfig.click();
71+
return new ConfigPage();
72+
}
73+
74+
public void clickOpenNavigationLeftMenu() {
75+
openNavigationLeftMenu.click();
76+
}
77+
78+
public void clickItemWebView() {
79+
itemWebView.click();
80+
}
81+
82+
public void clickButtonBack() {
83+
buttonBack.click();
84+
}
85+
86+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.anhtester.Bai26_Log4j2.pages;
2+
3+
import com.anhtester.drivers.DriverManager;
4+
import io.appium.java_client.pagefactory.AndroidFindBy;
5+
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
6+
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
7+
import org.openqa.selenium.WebElement;
8+
import org.openqa.selenium.support.PageFactory;
9+
10+
public class ConfigPage extends BasePage {
11+
public ConfigPage() {
12+
PageFactory.initElements(new AppiumFieldDecorator(DriverManager.getDriver()), this);
13+
}
14+
15+
@AndroidFindBy(xpath = "//android.widget.Button[contains(@content-desc,\"Product management\")]")
16+
@iOSXCUITFindBy(xpath = "")
17+
private WebElement itemProductManagement;
18+
19+
@AndroidFindBy(accessibility = "Server database")
20+
@iOSXCUITFindBy(accessibility = "Server database")
21+
private WebElement itemServerDatabase;
22+
23+
@AndroidFindBy(xpath = "//android.widget.Button[contains(@content-desc,\"Tables management\")]")
24+
@iOSXCUITFindBy(xpath = "")
25+
private WebElement itemTableManagement;
26+
27+
@AndroidFindBy(accessibility = "Logout")
28+
@iOSXCUITFindBy(accessibility = "Logout")
29+
private WebElement itemLogout;
30+
31+
public ProductPage openProductManagement() {
32+
itemProductManagement.click();
33+
34+
return new ProductPage();
35+
}
36+
37+
public ServerPage openServerDatabase() {
38+
itemServerDatabase.click();
39+
40+
return new ServerPage();
41+
}
42+
43+
public TablePage openTableManagement() {
44+
itemTableManagement.click();
45+
46+
return new TablePage();
47+
}
48+
49+
public LoginPage logout() {
50+
itemLogout.click();
51+
52+
return new LoginPage();
53+
}
54+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.anhtester.Bai26_Log4j2.pages;
2+
3+
import com.anhtester.drivers.DriverManager;
4+
import com.anhtester.keywords.MobileUI;
5+
import io.appium.java_client.pagefactory.AndroidFindBy;
6+
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
7+
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
8+
import org.openqa.selenium.WebElement;
9+
import org.openqa.selenium.support.PageFactory;
10+
11+
public class LoginPage extends BasePage {
12+
13+
// Constructor
14+
public LoginPage() {
15+
PageFactory.initElements(new AppiumFieldDecorator(DriverManager.getDriver()), this);
16+
}
17+
18+
//Element/Locators thuộc chính trang này (màn hình này)
19+
@AndroidFindBy(xpath = "//android.view.View[@content-desc=\"Mobile App Flutter Beta\"]/following-sibling::android.widget.EditText[1]")
20+
@iOSXCUITFindBy(accessibility = "username")
21+
private WebElement usernameField;
22+
23+
@AndroidFindBy(xpath = "//android.view.View[@content-desc=\"Mobile App Flutter Beta\"]/following-sibling::android.widget.EditText[2]")
24+
@iOSXCUITFindBy(accessibility = "password")
25+
private WebElement passwordField;
26+
27+
@AndroidFindBy(xpath = "//android.widget.Button[@content-desc=\"Sign in\"]")
28+
@iOSXCUITFindBy(id = "loginBtn")
29+
private WebElement loginButton;
30+
31+
@AndroidFindBy(accessibility = " Invalid email or password")
32+
@iOSXCUITFindBy(accessibility = " Invalid email or password")
33+
private WebElement errorMessage;
34+
35+
//Các hàm xử lý trong chính nội bộ trang này (màn hình này)
36+
37+
public MenuPage login(String username, String password) {
38+
MobileUI.clickElement(usernameField); // Click vào username field
39+
MobileUI.setText(usernameField, username); // Nhập username
40+
MobileUI.clickElement(passwordField); // Click vào password field
41+
MobileUI.setText(passwordField, password); // Nhập password
42+
MobileUI.clickElement(loginButton); // Click nút login
43+
44+
return new MenuPage();
45+
}
46+
47+
public MenuPage verifyLoginSuccess() {
48+
// Sử dụng MobileUI để verify
49+
MobileUI.verifyElementPresentAndDisplayed(menuMenu, "The Table page not display. (Menu not found)");
50+
return new MenuPage();
51+
}
52+
53+
public void verifyLoginFail() {
54+
// Sử dụng MobileUI để verify
55+
MobileUI.verifyElementPresentAndDisplayed(errorMessage, "The error message not display.");
56+
System.out.println(MobileUI.getElementAttribute(errorMessage, "content-desc"));
57+
MobileUI.verifyElementAttribute(errorMessage, "content-desc", " Invalid email or password",
58+
"The content of error message not display.");
59+
}
60+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.anhtester.Bai26_Log4j2.pages;
2+
3+
import com.anhtester.drivers.DriverManager;
4+
import com.anhtester.keywords.MobileUI;
5+
import io.appium.java_client.pagefactory.AndroidFindBy;
6+
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
7+
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
8+
import org.openqa.selenium.WebElement;
9+
import org.openqa.selenium.support.PageFactory;
10+
11+
import java.util.List;
12+
13+
public class MenuPage extends BasePage {
14+
// Constructor
15+
public MenuPage() {
16+
PageFactory.initElements(new AppiumFieldDecorator(DriverManager.getDriver()), this);
17+
}
18+
19+
//Element/Locators thuộc chính trang này (màn hình này)
20+
@AndroidFindBy(xpath = "//android.widget.EditText")
21+
@iOSXCUITFindBy(accessibility = "")
22+
private WebElement inputSearch;
23+
24+
@AndroidFindBy(xpath = "(//android.view.View[contains(@content-desc,\"Table\")])[1]")
25+
@iOSXCUITFindBy(accessibility = "")
26+
private WebElement firstItemTable;
27+
28+
@AndroidFindBy(xpath = "//android.view.View[contains(@content-desc,\"Table\")]")
29+
@iOSXCUITFindBy(xpath = "")
30+
private List<WebElement> listItemTable;
31+
32+
public void searchTable(String tableName) {
33+
clickMenuMenu();
34+
MobileUI.clickElement(inputSearch); // Click vào ô tìm kiếm
35+
MobileUI.setText(inputSearch, tableName); // Nhập từ khóa tìm kiếm
36+
}
37+
38+
public void checkTableResultTotal(int expectedTotal) {
39+
List<WebElement> listTables = listItemTable;
40+
System.out.println("Table total: " + listTables.size());
41+
//Assert.assertTrue(listTables.size() >= expectedTotal);
42+
MobileUI.assertTrueCondition(listTables.size() >= expectedTotal, "The table total is not correct.");
43+
}
44+
45+
public void clickFirstItemTable() {
46+
MobileUI.clickElement(firstItemTable);
47+
}
48+
}

0 commit comments

Comments
 (0)