-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
339 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,4 @@ logs/ | |
*.log | ||
*.avi | ||
*.mp4 | ||
test-output/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.anhtester.utils; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class LogUtils { | ||
//Initialize Log4j instance | ||
private static final Logger Log = LogManager.getLogger(LogUtils.class); | ||
|
||
//Info Level Logs | ||
public static void info(String message) { | ||
Log.info(message); | ||
} | ||
|
||
public static void info(Object object) { | ||
Log.info(object); | ||
} | ||
|
||
//Warn Level Logs | ||
public static void warn(String message) { | ||
Log.warn(message); | ||
} | ||
|
||
public static void warn(Object object) { | ||
Log.warn(object); | ||
} | ||
|
||
//Error Level Logs | ||
public static void error(String message) { | ||
Log.error(message); | ||
} | ||
|
||
public static void error(Object object) { | ||
Log.error(object); | ||
} | ||
|
||
//Fatal Level Logs | ||
public static void fatal(String message) { | ||
Log.fatal(message); | ||
} | ||
|
||
public static void fatal(Object message) { | ||
Log.fatal(message); | ||
} | ||
|
||
//Debug Level Logs | ||
public static void debug(String message) { | ||
Log.debug(message); | ||
} | ||
|
||
public static void debug(Object object) { | ||
Log.debug(object); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name=PropertiesConfigLog4j2 | ||
property.filename=logs/applog.log | ||
appenders=console, file | ||
|
||
appender.console.type=Console | ||
appender.console.name=STDOUT | ||
appender.console.layout.type=PatternLayout | ||
appender.console.layout.pattern=[%level] %d{dd-MM-yyyy HH:mm:ss} [%t] %c{1} - %msg%n | ||
|
||
appender.file.type=File | ||
appender.file.name=LOGFILE | ||
appender.file.fileName=${filename} | ||
appender.file.layout.type=PatternLayout | ||
appender.file.layout.pattern=[%level] %d{dd-MM-yyyy HH:mm:ss} [%t] %c{1} - %msg%n | ||
|
||
loggers=file | ||
logger.file.name=LOGFILE | ||
logger.file.level=info | ||
logger.file.appenderRefs=file | ||
logger.file.appenderRef.file.ref=LOGFILE | ||
|
||
rootLogger.level=info | ||
rootLogger.appenderRefs=stdout, file | ||
rootLogger.appenderRef.stdout.ref=STDOUT | ||
rootLogger.appenderRef.file.ref=LOGFILE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration status="DEBUG"> | ||
<Appenders> | ||
<Console name="LogToConsole" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="[%-5level] %d{dd-MM-yyyy HH:mm:ss} [%t] %c{1} - %msg%n"/> | ||
</Console> | ||
<File name="LogToFile" fileName="logs/app-xml.log"> | ||
<PatternLayout pattern="[%-5level] %d{dd-MM-yyyy HH:mm:ss} [%t] %c{1} - %msg%n"/> | ||
</File> | ||
</Appenders> | ||
<Loggers> | ||
<Logger name="anhtester.com" level="debug" additivity="false"> | ||
<AppenderRef ref="LogToFile"/> | ||
<AppenderRef ref="LogToConsole"/> | ||
</Logger> | ||
<Root level="info"> | ||
<AppenderRef ref="LogToFile"/> | ||
<AppenderRef ref="LogToConsole"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
68 changes: 68 additions & 0 deletions
68
src/test/java/com/anhtester/Bai15_TestListener/CategoryTest_Listener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.anhtester.Bai15_TestListener; | ||
|
||
import com.anhtester.common.BaseTest; | ||
import com.anhtester.globals.TokenGlobal; | ||
import com.anhtester.listeners.TestListener; | ||
import io.restassured.http.ContentType; | ||
import io.restassured.path.json.JsonPath; | ||
import io.restassured.response.Response; | ||
import io.restassured.specification.RequestSpecification; | ||
import org.testng.Assert; | ||
import org.testng.TestListenerAdapter; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
import org.testng.reporters.TestHTMLReporter; | ||
|
||
import java.io.File; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
//@Listeners(TestListener.class) | ||
//@Listeners(value = {TestListener.class, TestListenerAdapter.class, TestHTMLReporter.class}) | ||
public class CategoryTest_Listener extends BaseTest { | ||
|
||
int CATEGORY_ID; | ||
|
||
@Test(priority = 1) | ||
public void testAddNewCategory() { | ||
System.out.println("Create Category"); | ||
String dataFile = "src/test/resources/testdata/CreateCategory.json"; | ||
|
||
RequestSpecification request = given(); | ||
request.baseUri("https://api.anhtester.com/api") | ||
.accept(ContentType.JSON) | ||
.contentType(ContentType.JSON) | ||
.header("Authorization", "Bearer " + TokenGlobal.TOKEN) | ||
.body(new File(dataFile)); | ||
|
||
Response response = request.post("/category"); | ||
|
||
response.prettyPrint(); | ||
response.then().statusCode(200); | ||
|
||
CATEGORY_ID = Integer.parseInt(response.path("response.id").toString()); | ||
System.out.println(CATEGORY_ID); | ||
|
||
} | ||
|
||
@Test(priority = 2) | ||
public void getCategoryById() { | ||
System.out.println("Get Category By Id"); | ||
|
||
RequestSpecification request = given(); | ||
request.baseUri("https://api.anhtester.com/api") | ||
.accept(ContentType.JSON) | ||
.contentType(ContentType.JSON) | ||
.header("Authorization", "Bearer " + TokenGlobal.TOKEN); | ||
|
||
System.out.println("CATEGORY_ID: " + CATEGORY_ID); | ||
Response response = request.get("/category/" + CATEGORY_ID); | ||
|
||
response.prettyPrint(); | ||
response.then().statusCode(200); | ||
|
||
JsonPath jsonPath = response.jsonPath(); | ||
Assert.assertEquals(jsonPath.get("response.name"), "Auto API A5", "The Category Name not match."); | ||
|
||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/test/java/com/anhtester/Bai16_Log4j2/CategoryTest_Log4j2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.anhtester.Bai16_Log4j2; | ||
|
||
import com.anhtester.common.BaseTest; | ||
import com.anhtester.globals.TokenGlobal; | ||
import com.anhtester.listeners.TestListener; | ||
import com.anhtester.utils.LogUtils; | ||
import io.restassured.http.ContentType; | ||
import io.restassured.path.json.JsonPath; | ||
import io.restassured.response.Response; | ||
import io.restassured.specification.RequestSpecification; | ||
import org.testng.Assert; | ||
import org.testng.TestListenerAdapter; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
import org.testng.reporters.TestHTMLReporter; | ||
|
||
import java.io.File; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
//@Listeners(TestListener.class) | ||
@Listeners(value = {TestListener.class, TestListenerAdapter.class, TestHTMLReporter.class}) | ||
public class CategoryTest_Log4j2 extends BaseTest { | ||
|
||
int CATEGORY_ID; | ||
|
||
@Test(priority = 1) | ||
public void testAddNewCategory() { | ||
LogUtils.info("Create Category"); | ||
String dataFile = "src/test/resources/testdata/CreateCategory.json"; | ||
|
||
RequestSpecification request = given(); | ||
request.baseUri("https://api.anhtester.com/api") | ||
.accept(ContentType.JSON) | ||
.contentType(ContentType.JSON) | ||
.header("Authorization", "Bearer " + TokenGlobal.TOKEN) | ||
.body(new File(dataFile)); | ||
|
||
Response response = request.post("/category"); | ||
LogUtils.info(response.prettyPrint()); | ||
response.prettyPrint(); | ||
response.then().statusCode(200); | ||
|
||
CATEGORY_ID = Integer.parseInt(response.path("response.id").toString()); | ||
LogUtils.info(CATEGORY_ID); | ||
|
||
} | ||
|
||
@Test(priority = 2) | ||
public void getCategoryById() { | ||
LogUtils.info("Get Category By Id"); | ||
|
||
RequestSpecification request = given(); | ||
request.baseUri("https://api.anhtester.com/api") | ||
.accept(ContentType.JSON) | ||
.contentType(ContentType.JSON) | ||
.header("Authorization", "Bearer " + TokenGlobal.TOKEN); | ||
|
||
LogUtils.info("CATEGORY_ID: " + CATEGORY_ID); | ||
Response response = request.get("/category/" + CATEGORY_ID); | ||
LogUtils.info(response.prettyPrint()); | ||
response.prettyPrint(); | ||
response.then().statusCode(200); | ||
|
||
JsonPath jsonPath = response.jsonPath(); | ||
Assert.assertEquals(jsonPath.get("response.name"), "Auto API A5", "The Category Name not match."); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.anhtester.listeners; | ||
|
||
import com.anhtester.globals.ConfigsGlobal; | ||
import com.anhtester.utils.LogUtils; | ||
import org.testng.ITestContext; | ||
import org.testng.ITestListener; | ||
import org.testng.ITestResult; | ||
|
||
public class TestListener implements ITestListener { | ||
@Override | ||
public void onStart(ITestContext result) { | ||
LogUtils.info("\n **********************************"); | ||
//Read Properties - loadAllFiles() | ||
//Select Database - lưu trữ vào biến toàn cục | ||
//Connect tới bên thứ 3 cho thuê reports chẳng hạn | ||
} | ||
|
||
@Override | ||
public void onFinish(ITestContext result) { | ||
//Tổng kết lại tình hình chạy test | ||
//In ra logs cho biết là đã kết thức và chạy được bao nhiêu cái pass/fail | ||
//Gửi mail đến mail chung để nắm bắt tình hình - Lấy ra các biến toàn cục hoặc file logs, report | ||
LogUtils.info("Đã gửi mail đến [email protected]"); | ||
LogUtils.info("Tổng số test cases: " + ConfigsGlobal.TCS_TOTAL); | ||
LogUtils.info("Số test cases passed: " + ConfigsGlobal.PASSED_TOTAL); | ||
LogUtils.info("Số test cases failed: " + ConfigsGlobal.FAILED_TOTAL); | ||
} | ||
|
||
@Override | ||
public void onTestStart(ITestResult result) { | ||
LogUtils.info("Đang chạy test case: " + result.getName()); | ||
//Mình sẽ bắt cái tên TCs để ghi logs và ghi vào report (Allure report) | ||
ConfigsGlobal.TCS_TOTAL++; | ||
} | ||
|
||
@Override | ||
public void onTestSuccess(ITestResult result) { | ||
//Cộng 1 đơn vị vào 1 biến toàn cục để nắm bắt số lượng tcs pass | ||
LogUtils.info("Test case passed: " + result.getName()); | ||
ConfigsGlobal.PASSED_TOTAL++; | ||
} | ||
|
||
@Override | ||
public void onTestFailure(ITestResult result) { | ||
//Cộng 1 đơn vị vào 1 biến toàn cục để nắm bắt số lượng tcs fail | ||
LogUtils.error("Test case failed: " + result.getName()); | ||
LogUtils.error(result.getThrowable()); | ||
ConfigsGlobal.FAILED_TOTAL++; | ||
} | ||
|
||
@Override | ||
public void onTestFailedButWithinSuccessPercentage(ITestResult result) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
|
||
@Override | ||
public void onTestSkipped(ITestResult result) { | ||
// TODO Auto-generated method stub | ||
|
||
} | ||
} |
Oops, something went wrong.