From 12d2fcb8dc423b2d4b62cb8ca4e7614e0089f3e7 Mon Sep 17 00:00:00 2001 From: An Vo Date: Fri, 19 Jul 2024 11:30:18 +0700 Subject: [PATCH] Update test cases --- .gitignore | 7 +- pom.xml | 27 +++---- .../com/anhtester/constants/AppConfig.java | 12 +++- .../com/anhtester/helpers/SystemHelper.java | 6 ++ .../com/anhtester/keywords/WebKeyword.java | 72 ++++++++++++++++--- .../anhtester/managers/BrowserFactory.java | 20 ++---- src/main/resources/log4j2.properties | 4 +- .../java/com/anhtester/common/BaseTest.java | 14 +++- .../anhtester/listeners/AllureListener.java | 16 ++--- .../com/anhtester/listeners/TestListener.java | 24 ++++++- .../com/anhtester/pom/pages/CategoryPage.java | 48 ------------- .../com/anhtester/pom/pages/CommonPage.java | 56 ++++++++++++--- .../com/anhtester/pom/pages/LoginPage.java | 52 -------------- .../pom/pages/admin/CategoryPage.java | 46 ++++++++++++ .../pom/pages/admin/DashboardPage.java | 45 ++++++++++++ .../anhtester/pom/pages/admin/LoginPage.java | 47 ++++++++++++ .../pom/pages/user/LoginUserPage.java | 45 ++++++++++++ .../anhtester/pom/testcases/CategoryTest.java | 25 ------- .../anhtester/pom/testcases/LoginTest.java | 22 ------ .../pom/testcases/admin/CategoryTest.java | 18 +++++ .../pom/testcases/admin/DashboardTest.java | 19 +++++ .../pom/testcases/admin/LoginTest.java | 24 +++++++ .../pom/testcases/user/LoginUserTest.java | 24 +++++++ src/test/resources/config/config.properties | 13 ++-- .../test/resources/suites}/CategoryTest.xml | 2 +- src/test/resources/suites/DashboardTest.xml | 9 +++ .../test/resources/suites}/LoginTest.xml | 2 +- .../test/resources/suites}/SuiteAll.xml | 1 + 28 files changed, 480 insertions(+), 220 deletions(-) delete mode 100644 src/test/java/com/anhtester/pom/pages/CategoryPage.java delete mode 100644 src/test/java/com/anhtester/pom/pages/LoginPage.java create mode 100644 src/test/java/com/anhtester/pom/pages/admin/CategoryPage.java create mode 100644 src/test/java/com/anhtester/pom/pages/admin/DashboardPage.java create mode 100644 src/test/java/com/anhtester/pom/pages/admin/LoginPage.java create mode 100644 src/test/java/com/anhtester/pom/pages/user/LoginUserPage.java delete mode 100644 src/test/java/com/anhtester/pom/testcases/CategoryTest.java delete mode 100644 src/test/java/com/anhtester/pom/testcases/LoginTest.java create mode 100644 src/test/java/com/anhtester/pom/testcases/admin/CategoryTest.java create mode 100644 src/test/java/com/anhtester/pom/testcases/admin/DashboardTest.java create mode 100644 src/test/java/com/anhtester/pom/testcases/admin/LoginTest.java create mode 100644 src/test/java/com/anhtester/pom/testcases/user/LoginUserTest.java rename {suites => src/test/resources/suites}/CategoryTest.xml (77%) create mode 100644 src/test/resources/suites/DashboardTest.xml rename {suites => src/test/resources/suites}/LoginTest.xml (77%) rename {suites => src/test/resources/suites}/SuiteAll.xml (82%) diff --git a/.gitignore b/.gitignore index d3b4b79..62ff131 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ target/ +out/ +bin/ +.vs/ pom.xml.tag pom.xml.releaseBackup pom.xml.versionsBackup @@ -11,6 +14,6 @@ buildNumber.properties .mvn/wrapper/maven-wrapper.jar *.zip .idea/ -report/ logs/ -*.iml +exports/ +*.iml \ No newline at end of file diff --git a/pom.xml b/pom.xml index f9b687d..c7521dc 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.anhtester PlaywrightJava - 1.44.0 + 1.45.0 17 @@ -19,7 +19,7 @@ com.microsoft.playwright playwright - 1.44.0 + 1.45.0 @@ -40,21 +40,21 @@ org.slf4j slf4j-simple - 2.0.11 + 2.0.13 org.apache.poi poi - 5.2.5 + 5.3.0 org.apache.poi poi-ooxml - 5.2.5 + 5.3.0 @@ -89,21 +89,21 @@ com.aventstack extentreports - 5.1.1 + 5.1.2 io.qameta.allure allure-testng - 2.27.0 + 2.28.0 io.qameta.allure allure-attachments - 2.27.0 + 2.28.0 @@ -117,7 +117,7 @@ org.projectlombok lombok - 1.18.32 + 1.18.34 provided @@ -125,7 +125,7 @@ com.google.guava guava - 33.2.0-jre + 33.2.1-jre @@ -135,7 +135,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.12.1 + 3.13.0 @@ -144,13 +144,14 @@ 3.2.5 - suites/SuiteAll.xml + src/test/resources/suites/SuiteAll.xml - -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.21/aspectjweaver-1.9.21.jar" + -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.22.1/aspectjweaver-1.9.22.1.jar" true + false target/allure-results diff --git a/src/main/java/com/anhtester/constants/AppConfig.java b/src/main/java/com/anhtester/constants/AppConfig.java index 6fdcc49..e19b85c 100644 --- a/src/main/java/com/anhtester/constants/AppConfig.java +++ b/src/main/java/com/anhtester/constants/AppConfig.java @@ -1,5 +1,15 @@ package com.anhtester.constants; +import com.anhtester.helpers.PropertiesHelper; + public class AppConfig { - public static final boolean HEADLESS = false; + public static boolean HEADLESS = Boolean.parseBoolean(PropertiesHelper.getValue("HEADLESS")); + public static String URL = PropertiesHelper.getValue("URL"); + public static String BROWSER = PropertiesHelper.getValue("BROWSER"); + public static int VIEWPORT_WIDTH = Integer.parseInt(PropertiesHelper.getValue("VIEWPORT_WIDTH")); + public static int VIEWPORT_HEIGHT = Integer.parseInt(PropertiesHelper.getValue("VIEWPORT_HEIGHT")); + public static boolean VIDEO_RECORD = Boolean.parseBoolean(PropertiesHelper.getValue("VIDEO_RECORD")); + public static boolean SCREENSHOT_FAIL = Boolean.parseBoolean(PropertiesHelper.getValue("SCREENSHOT_FAIL")); + public static boolean SCREENSHOT_PASS = Boolean.parseBoolean(PropertiesHelper.getValue("SCREENSHOT_PASS")); + public static int TIMEOUT_STEP = Integer.parseInt(PropertiesHelper.getValue("TIMEOUT_STEP")); } diff --git a/src/main/java/com/anhtester/helpers/SystemHelper.java b/src/main/java/com/anhtester/helpers/SystemHelper.java index 802f3e4..52e6889 100644 --- a/src/main/java/com/anhtester/helpers/SystemHelper.java +++ b/src/main/java/com/anhtester/helpers/SystemHelper.java @@ -7,4 +7,10 @@ public static String getCurrentDir() { String current = System.getProperty("user.dir") + File.separator; return current; } + + public static String getOperatingSystem() { + String os = System.getProperty("os.name"); + // System.out.println("OS name: " + os); + return os; + } } diff --git a/src/main/java/com/anhtester/keywords/WebKeyword.java b/src/main/java/com/anhtester/keywords/WebKeyword.java index e3873e9..7997aac 100644 --- a/src/main/java/com/anhtester/keywords/WebKeyword.java +++ b/src/main/java/com/anhtester/keywords/WebKeyword.java @@ -1,19 +1,36 @@ package com.anhtester.keywords; +import com.anhtester.constants.AppConfig; +import com.anhtester.helpers.SystemHelper; import com.anhtester.managers.PageManager; import com.anhtester.reports.AllureManager; import com.anhtester.reports.ExtentTestManager; import com.anhtester.utils.LogUtils; import com.aventstack.extentreports.Status; import io.qameta.allure.Step; +import org.testng.asserts.SoftAssert; import java.awt.*; import java.awt.event.KeyEvent; public class WebKeyword { - private static int TIMEOUT = 10; - private static double STEP_TIME = 0; - private static int PAGE_LOAD_TIMEOUT = 20; + + private static SoftAssert softAssert; + + private static double STEP_TIME = AppConfig.TIMEOUT_STEP; + + public static SoftAssert getSoftAssert() { + if (softAssert == null) { + softAssert = new SoftAssert(); + } + return softAssert; + } + + public static void closeSoftAssert() { + if (softAssert != null) { + softAssert.assertAll(); + } + } public static void sleep(double second) { try { @@ -24,13 +41,16 @@ public static void sleep(double second) { } public static void maximizeBrowserOnWindow() { + System.out.println(SystemHelper.getOperatingSystem()); Robot rb = null; try { rb = new Robot(); - rb.keyPress(KeyEvent.VK_WINDOWS); - rb.keyPress(KeyEvent.VK_UP); - rb.keyRelease(KeyEvent.VK_UP); - rb.keyRelease(KeyEvent.VK_WINDOWS); + if (SystemHelper.getOperatingSystem().toLowerCase().contains("window")) { + rb.keyPress(KeyEvent.VK_WINDOWS); + rb.keyPress(KeyEvent.VK_UP); + rb.keyRelease(KeyEvent.VK_UP); + rb.keyRelease(KeyEvent.VK_WINDOWS); + } } catch (AWTException e) { e.printStackTrace(); } @@ -39,7 +59,7 @@ public static void maximizeBrowserOnWindow() { @Step("Navigate to URL: {0}") public static void navigate(String url) { PageManager.getPage().navigate(url); - sleep(STEP_TIME); + PageManager.getPage().waitForLoadState(); LogUtils.info("Navigate to URL: " + url); ExtentTestManager.logMessage(Status.INFO, "Navigate to URL: " + url); } @@ -60,6 +80,30 @@ public static void fill(String locator, String value) { ExtentTestManager.logMessage(Status.INFO, "Fill text " + value + " on element " + locator); } + @Step("Get attribute {1} of element {0}") + public static void getAttribute(String locator, String attributeName) { + sleep(STEP_TIME); + String text = PageManager.getPage().locator(locator).getAttribute(attributeName); + LogUtils.info("Get attribute " + attributeName + " of element " + locator); + ExtentTestManager.logMessage(Status.INFO, "Get attribute " + attributeName + " of element " + locator); + } + + @Step("Clear text in element {0}") + public static void clear(String locator) { + sleep(STEP_TIME); + PageManager.getPage().locator(locator).clear(); + LogUtils.info("Clear text in element " + locator); + ExtentTestManager.logMessage(Status.INFO, "Clear text in element " + locator); + } + + @Step("High light element {0}") + public static void highlight(String locator) { + sleep(STEP_TIME); + PageManager.getPage().locator(locator).highlight(); + LogUtils.info("High light element " + locator); + ExtentTestManager.logMessage(Status.INFO, "High light element " + locator); + } + @Step("Get text of element {0}") public static String textContent(String locator) { sleep(STEP_TIME); @@ -67,9 +111,19 @@ public static String textContent(String locator) { LogUtils.info("Get text of element " + locator + " ==> " + text); ExtentTestManager.logMessage(Status.INFO, "Get text of element " + locator); ExtentTestManager.logMessage(Status.INFO, "==> Text: " + text); - AllureManager.saveTextLog("==> " + text); + return text; + } + @Step("Get text of element {0}") + public static String innerText(String locator) { + sleep(STEP_TIME); + String text = PageManager.getPage().locator(locator).innerText(); + LogUtils.info("Get text of element " + locator + " ==> " + text); + ExtentTestManager.logMessage(Status.INFO, "Get text of element " + locator); + ExtentTestManager.logMessage(Status.INFO, "==> Text: " + text); + AllureManager.saveTextLog("==> " + text); return text; } + } diff --git a/src/main/java/com/anhtester/managers/BrowserFactory.java b/src/main/java/com/anhtester/managers/BrowserFactory.java index 1f3e99f..f116496 100644 --- a/src/main/java/com/anhtester/managers/BrowserFactory.java +++ b/src/main/java/com/anhtester/managers/BrowserFactory.java @@ -1,6 +1,7 @@ package com.anhtester.managers; import com.anhtester.constants.AppConfig; +import com.anhtester.keywords.WebKeyword; import com.microsoft.playwright.*; import java.awt.*; @@ -10,14 +11,9 @@ public class BrowserFactory { protected static Browser browser; protected static BrowserContext browserContext; protected static Page page; - - private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); - private static int screenWidth = ((int) screenSize.getWidth()); - private static int screenHeight = ((int) screenSize.getHeight()); - //https://playwright.dev/java/docs/browsers - public static Page createBrowser(String browserName) { + public static void createBrowser(String browserName) { playwright = Playwright.create(); PageManager.setPlaywright(playwright); @@ -48,21 +44,17 @@ public static Page createBrowser(String browserName) { break; } - System.out.println("Screen Browser Width: " + screenWidth); - System.out.println("Screen Browser Height: " + screenHeight); - PageManager.setBrowser(browser); - screenWidth = screenWidth - ((screenWidth * 5) / 100); - System.out.println("Screen Browser Width (customize): " + screenWidth); +// System.out.println("Screen Browser Width (customize): " + AppConfig.VIEWPORT_WIDTH); +// System.out.println("Screen Browser Height (customize): " + AppConfig.VIEWPORT_HEIGHT); - browserContext = PageManager.getBrowser().newContext(new Browser.NewContextOptions().setViewportSize(screenWidth, screenHeight).setScreenSize(screenWidth, screenHeight)); + browserContext = PageManager.getBrowser().newContext(new Browser.NewContextOptions().setViewportSize(AppConfig.VIEWPORT_WIDTH, AppConfig.VIEWPORT_HEIGHT).setDeviceScaleFactor(1)); PageManager.setBrowserContext(browserContext); page = PageManager.getBrowserContext().newPage(); PageManager.setPage(page); - //WebKeyword.maximizeBrowserOnWindow(); - return PageManager.getPage(); + WebKeyword.maximizeBrowserOnWindow(); } } diff --git a/src/main/resources/log4j2.properties b/src/main/resources/log4j2.properties index d7840ba..9266813 100644 --- a/src/main/resources/log4j2.properties +++ b/src/main/resources/log4j2.properties @@ -10,7 +10,7 @@ appender.console.layout.pattern=[%level] %d{dd-MM-yyyy HH:mm:ss} [%t] %c{1} - %m appender.file.type=File appender.file.name=LOGFILE appender.file.fileName=${filename} -appender.file.filePattern=logs/applog-%d{dd-MM-yyyy}.log +#appender.file.filePattern=logs/applog-%d{dd-MM-yyyy}.log appender.file.layout.type=PatternLayout appender.file.layout.pattern=[%level] %d{dd-MM-yyyy HH:mm:ss} [%t] %c{1} - %msg%n @@ -21,6 +21,6 @@ logger.file.appenderRefs=file logger.file.appenderRef.file.ref=LOGFILE rootLogger.level=info -rootLogger.appenderRefs=stdout, file +rootLogger.appenderRefs=console, file rootLogger.appenderRef.stdout.ref=STDOUT rootLogger.appenderRef.file.ref=LOGFILE \ No newline at end of file diff --git a/src/test/java/com/anhtester/common/BaseTest.java b/src/test/java/com/anhtester/common/BaseTest.java index 2998b2a..f0a70db 100644 --- a/src/test/java/com/anhtester/common/BaseTest.java +++ b/src/test/java/com/anhtester/common/BaseTest.java @@ -1,23 +1,33 @@ package com.anhtester.common; +import com.anhtester.constants.AppConfig; +import com.anhtester.helpers.PropertiesHelper; +import com.anhtester.keywords.WebKeyword; import com.anhtester.listeners.TestListener; import com.anhtester.managers.BrowserFactory; import com.anhtester.managers.PageManager; +import com.anhtester.pom.pages.CommonPage; import org.testng.annotations.*; @Listeners(TestListener.class) -public class BaseTest { +public class BaseTest extends CommonPage { @BeforeMethod @Parameters({"BROWSER"}) public void createBrowser(@Optional("chrome") String browserName) { - BrowserFactory.createBrowser(browserName); + if (AppConfig.BROWSER != null && !AppConfig.BROWSER.isEmpty()) { + BrowserFactory.createBrowser(AppConfig.BROWSER); + }else { + BrowserFactory.createBrowser(browserName); + } } @AfterMethod public void closePage() { PageManager.closePage(); PageManager.closeBrowser(); + + WebKeyword.closeSoftAssert(); } } diff --git a/src/test/java/com/anhtester/listeners/AllureListener.java b/src/test/java/com/anhtester/listeners/AllureListener.java index 52291c1..d5438b0 100644 --- a/src/test/java/com/anhtester/listeners/AllureListener.java +++ b/src/test/java/com/anhtester/listeners/AllureListener.java @@ -44,14 +44,14 @@ public void afterTestStart(TestResult result) { @Override public void beforeTestStop(TestResult result) { - SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss"); - - ScreenshotOptions screenshotOptions = new ScreenshotOptions(); - if (result.getStatus() == Status.FAILED || result.getStatus() == Status.SKIPPED || result.getStatus() == Status.BROKEN) { - if (PageManager.getPage() != null) { - Allure.addAttachment(result.getName() + "_Failed_Screenshot", new ByteArrayInputStream(PageManager.getPage().screenshot(screenshotOptions.setPath(Paths.get(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("SCREENSHOT_PATH") + File.separator + dateFormat.format(new Date()) + ".png"))))); - } - } +// SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss"); +// +// ScreenshotOptions screenshotOptions = new ScreenshotOptions(); +// if (result.getStatus() == Status.FAILED || result.getStatus() == Status.SKIPPED || result.getStatus() == Status.BROKEN) { +// if (PageManager.getPage() != null) { +// Allure.addAttachment(result.getName() + "_Failed_Screenshot", new ByteArrayInputStream(PageManager.getPage().screenshot(screenshotOptions.setPath(Paths.get(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("SCREENSHOT_PATH") + File.separator + dateFormat.format(new Date()) + ".png"))))); +// } +// } } @Override diff --git a/src/test/java/com/anhtester/listeners/TestListener.java b/src/test/java/com/anhtester/listeners/TestListener.java index 2320f41..8b5576e 100644 --- a/src/test/java/com/anhtester/listeners/TestListener.java +++ b/src/test/java/com/anhtester/listeners/TestListener.java @@ -2,16 +2,26 @@ import com.anhtester.helpers.CaptureHelper; import com.anhtester.helpers.PropertiesHelper; +import com.anhtester.helpers.SystemHelper; import com.anhtester.keywords.WebKeyword; +import com.anhtester.managers.PageManager; import com.anhtester.reports.AllureManager; import com.anhtester.reports.ExtentReportManager; import com.anhtester.reports.ExtentTestManager; import com.anhtester.utils.LogUtils; import com.aventstack.extentreports.Status; +import com.microsoft.playwright.Page; +import io.qameta.allure.Allure; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.nio.file.Paths; +import java.text.SimpleDateFormat; +import java.util.Date; + public class TestListener implements ITestListener { public String getTestName(ITestResult result) { @@ -34,7 +44,7 @@ public void onFinish(ITestContext result) { @Override public void onTestStart(ITestResult result) { - LogUtils.info("*********" + result.getName() + "*********"); + LogUtils.info("➡\uFE0F " + result.getName()); if (PropertiesHelper.getValue("VIDEO_RECORD").equals("true")) { CaptureHelper.startRecord(result.getName()); @@ -45,7 +55,7 @@ public void onTestStart(ITestResult result) { @Override public void onTestSuccess(ITestResult result) { - LogUtils.info("==> " + result.getName() + " is successfully."); + LogUtils.info("⭐\uFE0F " + result.getName() + " is successfully."); if (PropertiesHelper.getValue("SCREENSHOT_PASS").equals("true")) { CaptureHelper.takeScreenshot(result.getName()); @@ -61,7 +71,7 @@ public void onTestSuccess(ITestResult result) { @Override public void onTestFailure(ITestResult result) { - LogUtils.error("==> " + result.getName() + " is FAIL."); + LogUtils.error("❌ " + result.getName() + " is FAIL."); if (PropertiesHelper.getValue("SCREENSHOT_FAIL").equals("true")) { CaptureHelper.takeScreenshot(result.getName()); @@ -80,6 +90,14 @@ public void onTestFailure(ITestResult result) { //Allure Report //AllureManager.saveTextLog(result.getName() + " is failed."); //AllureManager.saveScreenshotPNG(); + + SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss"); + Page.ScreenshotOptions screenshotOptions = new Page.ScreenshotOptions(); + if (result.getStatus() == 2 || result.getStatus() == 3) { + if (PageManager.getPage() != null) { + Allure.addAttachment(result.getName() + "_Failed_Screenshot", new ByteArrayInputStream(PageManager.getPage().screenshot(screenshotOptions.setPath(Paths.get(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("SCREENSHOT_PATH") + File.separator + dateFormat.format(new Date()) + ".png"))))); + } + } } @Override diff --git a/src/test/java/com/anhtester/pom/pages/CategoryPage.java b/src/test/java/com/anhtester/pom/pages/CategoryPage.java deleted file mode 100644 index 84a35fd..0000000 --- a/src/test/java/com/anhtester/pom/pages/CategoryPage.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.anhtester.pom.pages; - -import com.microsoft.playwright.Page; -import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; - -public class CategoryPage extends CommonPage { - - private String buttonAddNewCategory = "//span[normalize-space()='Add New category']"; - private String inputCategoryName = "//input[@id='name']"; - private String dropdownParentCategory = "//label[normalize-space()='Parent Category']/following-sibling::div//button"; - private String inputParentCategory = "//div[@class='dropdown-menu show']//input[@aria-label='Search']"; - private String inputOrderNUmber = "//input[@id='order_level']"; - private String inputMetaTitle = "//input[@placeholder='Meta Title']"; - private String inputMetaDescription = "//textarea[@name='meta_description']"; - private String buttonSave = "//button[normalize-space()='Save']"; - private String inputSearchCategory = "//input[@id='search']"; - private String itemCategoryNameFirst = "//tbody/tr[1]/td[2]"; - - Page page; - - public CategoryPage(Page _page) { - super(_page); - page = _page; - } - - public void addNewCategory(String categoryName) { - page.click(buttonAddNewCategory); - page.fill(inputCategoryName, categoryName); - page.click(dropdownParentCategory); - page.waitForTimeout(1000); - page.fill(inputParentCategory, "Gaming pc"); - page.waitForTimeout(1000); - page.press(inputParentCategory, "Enter"); - page.fill(inputOrderNUmber, "1"); - page.fill(inputMetaTitle, categoryName); - page.fill(inputMetaDescription, categoryName); - page.click(buttonSave); - } - - public void checkCategory(String categoryName){ - page.waitForLoadState(); - page.fill(inputSearchCategory, categoryName); - page.press(inputSearchCategory, "Enter"); - page.waitForTimeout(1000); - assertThat(page.locator(itemCategoryNameFirst)).isVisible(); - assertThat(page.locator(itemCategoryNameFirst)).hasText(categoryName); - } -} diff --git a/src/test/java/com/anhtester/pom/pages/CommonPage.java b/src/test/java/com/anhtester/pom/pages/CommonPage.java index 0668a8b..3e3c624 100644 --- a/src/test/java/com/anhtester/pom/pages/CommonPage.java +++ b/src/test/java/com/anhtester/pom/pages/CommonPage.java @@ -1,21 +1,55 @@ package com.anhtester.pom.pages; -import com.microsoft.playwright.Page; +import com.anhtester.keywords.WebKeyword; +import com.anhtester.managers.PageManager; +import com.anhtester.pom.pages.admin.CategoryPage; +import com.anhtester.pom.pages.admin.DashboardPage; +import com.anhtester.pom.pages.admin.LoginPage; +import com.anhtester.pom.pages.user.LoginUserPage; public class CommonPage { - private String menuProducts = "//span[normalize-space()='Products']"; - private String menuCategory = "//span[normalize-space()='Category']"; + public String menuProducts = "//span[normalize-space()='Products']"; + public String menuCategory = "//span[normalize-space()='Category']"; + public String buttonClearCacheOnAdminPage = "//span[.='Clear Cache']"; - private Page page; - public CommonPage(Page _page){ - page = _page; + private LoginPage loginPage; + private DashboardPage dashboardPage; + private CategoryPage categoryPage; + private LoginUserPage loginUserPage; + + public LoginPage getLoginPage() { + if (loginPage == null) { + loginPage = new LoginPage(); + } + return loginPage; + } + + public DashboardPage getDashboardPage() { + if (dashboardPage == null) { + dashboardPage = new DashboardPage(); + } + return dashboardPage; + } + + public CategoryPage getCategoryPage() { + if (categoryPage == null) { + categoryPage = new CategoryPage(); + } + return categoryPage; + } + + public LoginUserPage getLoginUserPage() { + if (loginUserPage == null) { + loginUserPage = new LoginUserPage(); + } + return loginUserPage; } - public CategoryPage openCategoryPage(){ - page.waitForLoadState(); - page.click(menuProducts); - page.click(menuCategory); - return new CategoryPage(page); + public CategoryPage openCategoryPage() { + PageManager.getPage().waitForLoadState(); + WebKeyword.click(menuProducts); + WebKeyword.click(menuCategory); + return new CategoryPage(); } } diff --git a/src/test/java/com/anhtester/pom/pages/LoginPage.java b/src/test/java/com/anhtester/pom/pages/LoginPage.java deleted file mode 100644 index e2fbc05..0000000 --- a/src/test/java/com/anhtester/pom/pages/LoginPage.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.anhtester.pom.pages; - -import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; - -import com.anhtester.keywords.WebKeyword; -import com.anhtester.managers.PageManager; -import com.microsoft.playwright.Page; -import com.microsoft.playwright.options.WaitUntilState; -import org.testng.Assert; - -public class LoginPage { - - public String URL_ADMIN = "https://cms.anhtester.com/login"; - public String URL_USER = "https://cms.anhtester.com/users/login"; - - private String headerLoginPageAdmin = "//div[@class='card-body']//h1"; - private String headerLoginPageUser = "//div[@class='card']//h1"; - - private String inputEmail = "//input[@id='email']"; - private String inputPassword = "//input[@id='password']"; - private String buttonLogin = "//button[normalize-space()='Login']"; - private String buttonCopy = "//button[normalize-space()='Copy']"; - - private String buttonX = "//i[@class='la la-close fs-20']"; - private String buttonDefaultOnAdminPage = "//span[.='Clear Cache']"; - private String headerDashboardPageUser = "//h1[normalize-space()='Dashboard']"; - - public void loginAdminCMS(String email, String password){ - WebKeyword.navigate(URL_ADMIN); - PageManager.getPage().waitForLoadState(); - Assert.assertEquals(WebKeyword.textContent(headerLoginPageAdmin), "Welcome to Active eCommerce CMS"); - assertThat(PageManager.getPage().locator(headerLoginPageAdmin)).hasText("Welcome to Active eCommerce CMS"); - WebKeyword.fill(inputEmail, email); - WebKeyword.fill(inputPassword, password); - WebKeyword.click(buttonLogin); - PageManager.getPage().waitForLoadState(); - assertThat(PageManager.getPage().locator(buttonDefaultOnAdminPage)).isVisible(); - } - - public void loginUserCMS(String email, String password){ - WebKeyword.navigate(URL_USER); - PageManager.getPage().waitForLoadState(); - WebKeyword.click(buttonX); - assertThat(PageManager.getPage().locator(headerLoginPageUser)).hasText("Login to your account."); - WebKeyword.fill(inputEmail, email); - WebKeyword.fill(inputPassword, password); - WebKeyword.click(buttonLogin); - PageManager.getPage().waitForLoadState(); - assertThat(PageManager.getPage().locator(headerDashboardPageUser)).isVisible(); - } - -} diff --git a/src/test/java/com/anhtester/pom/pages/admin/CategoryPage.java b/src/test/java/com/anhtester/pom/pages/admin/CategoryPage.java new file mode 100644 index 0000000..44df8da --- /dev/null +++ b/src/test/java/com/anhtester/pom/pages/admin/CategoryPage.java @@ -0,0 +1,46 @@ +package com.anhtester.pom.pages.admin; + +import com.anhtester.keywords.WebKeyword; +import com.anhtester.managers.PageManager; +import com.anhtester.pom.pages.CommonPage; +import com.microsoft.playwright.Page; +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; + +public class CategoryPage extends CommonPage { + + private String buttonAddNewCategory = "//span[normalize-space()='Add New category']"; + private String inputCategoryName = "//input[@id='name']"; + private String dropdownParentCategory = "//label[normalize-space()='Parent Category']/following-sibling::div//button"; + private String inputParentCategory = "//div[@class='dropdown-menu show']//input[@aria-label='Search']"; + private String inputOrderNUmber = "//input[@id='order_level']"; + private String inputMetaTitle = "//input[@placeholder='Meta Title']"; + private String inputMetaDescription = "//textarea[@name='meta_description']"; + private String buttonSave = "//button[normalize-space()='Save']"; + private String inputSearchCategory = "//input[@id='search']"; + private String itemCategoryNameFirst = "//tbody/tr[1]/td[2]"; + + public CategoryPage addNewCategory(String categoryName) { + PageManager.getPage().waitForLoadState(); + PageManager.getPage().click(buttonAddNewCategory); + PageManager.getPage().fill(inputCategoryName, categoryName); + PageManager.getPage().click(dropdownParentCategory); + WebKeyword.sleep(1); + PageManager.getPage().fill(inputParentCategory, "Laptop"); + WebKeyword.sleep(1); + PageManager.getPage().press(inputParentCategory, "Enter"); + PageManager.getPage().fill(inputOrderNUmber, "1"); + PageManager.getPage().fill(inputMetaTitle, categoryName); + PageManager.getPage().fill(inputMetaDescription, categoryName); + PageManager.getPage().click(buttonSave); + return this; + } + + public void checkCategory(String categoryName){ + PageManager.getPage().waitForLoadState(); + PageManager.getPage().fill(inputSearchCategory, categoryName); + PageManager.getPage().press(inputSearchCategory, "Enter"); + WebKeyword.sleep(1); + assertThat(PageManager.getPage().locator(itemCategoryNameFirst)).isVisible(); + assertThat(PageManager.getPage().locator(itemCategoryNameFirst)).hasText(categoryName); + } +} diff --git a/src/test/java/com/anhtester/pom/pages/admin/DashboardPage.java b/src/test/java/com/anhtester/pom/pages/admin/DashboardPage.java new file mode 100644 index 0000000..2ffff83 --- /dev/null +++ b/src/test/java/com/anhtester/pom/pages/admin/DashboardPage.java @@ -0,0 +1,45 @@ +package com.anhtester.pom.pages.admin; + +import com.anhtester.keywords.WebKeyword; +import com.anhtester.pom.pages.CommonPage; + +public class DashboardPage extends CommonPage { + + private String totalCustomerLabel = "//div[normalize-space()='Total Customer']/following-sibling::div"; + private String totalOrderLabel = "//div[normalize-space()='Total Order']/following-sibling::div"; + private String totalProductCategoryLabel = "//div[normalize-space()='Total Product category']/following-sibling::div"; + private String totalProductBrandLabel = "//div[normalize-space()='Total Product brand']/following-sibling::div"; + private String buttonClearCacheOnAdminPage = "//span[.='Clear Cache']"; + + public void verifyTotalCustomer(String number) { + WebKeyword.getSoftAssert().assertEquals( + WebKeyword.textContent(totalCustomerLabel), + number, + "The total Customer not match." + ); + } + + public void verifyTotalOrder(String number) { + WebKeyword.getSoftAssert().assertEquals( + WebKeyword.textContent(totalOrderLabel), + number, + "The total Order not match." + ); + } + + public void verifyTotalProductCategory(String number) { + WebKeyword.getSoftAssert().assertEquals( + WebKeyword.textContent(totalProductCategoryLabel), + number, + "The total Product Category not match." + ); + } + + public void verifyTotalProductBrand(String number) { + WebKeyword.getSoftAssert().assertEquals( + WebKeyword.textContent(totalProductBrandLabel), + number, + "The total Product Brand not match." + ); + } +} diff --git a/src/test/java/com/anhtester/pom/pages/admin/LoginPage.java b/src/test/java/com/anhtester/pom/pages/admin/LoginPage.java new file mode 100644 index 0000000..9473155 --- /dev/null +++ b/src/test/java/com/anhtester/pom/pages/admin/LoginPage.java @@ -0,0 +1,47 @@ +package com.anhtester.pom.pages.admin; + +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; + +import com.anhtester.keywords.WebKeyword; +import com.anhtester.managers.PageManager; +import com.anhtester.pom.pages.CommonPage; +import org.testng.Assert; + +public class LoginPage extends CommonPage { + + private String URL_ADMIN = "https://cms.anhtester.com/login"; + private String headerLoginPageAdmin = "//div[@class='card-body']//h1"; + private String inputEmail = "//input[@id='email']"; + private String inputPassword = "//input[@id='password']"; + private String buttonLogin = "//button[normalize-space()='Login']"; + private String buttonCopy = "//button[normalize-space()='Copy']"; + + private String alertMessage = "//div[@role='alert']"; + + + public LoginPage loginAdminCMS(String email, String password){ + WebKeyword.navigate(URL_ADMIN); + PageManager.getPage().waitForLoadState(); + Assert.assertEquals(WebKeyword.textContent(headerLoginPageAdmin), "Welcome to Active eCommerce CMS"); + assertThat(PageManager.getPage().locator(headerLoginPageAdmin)).hasText("Welcome to Active eCommerce CMS"); + WebKeyword.fill(inputEmail, email); + WebKeyword.fill(inputPassword, password); + WebKeyword.click(buttonLogin); + PageManager.getPage().waitForLoadState(); + return this; + } + + public LoginPage verifyLoginSuccess(){ + PageManager.getPage().waitForLoadState(); + Assert.assertTrue(PageManager.getPage().isVisible(buttonClearCacheOnAdminPage), "FAIL. The button Clear Cache not visible."); + return this; + } + + public LoginPage verifyLoginFail(String message){ + PageManager.getPage().waitForLoadState(); + Assert.assertEquals(WebKeyword.textContent(alertMessage), message, "FAIL. The error message not match."); + Assert.assertFalse(PageManager.getPage().isVisible(buttonClearCacheOnAdminPage), "FAIL. The button Clear Cache visible."); + return this; + } + +} diff --git a/src/test/java/com/anhtester/pom/pages/user/LoginUserPage.java b/src/test/java/com/anhtester/pom/pages/user/LoginUserPage.java new file mode 100644 index 0000000..dc6c9b4 --- /dev/null +++ b/src/test/java/com/anhtester/pom/pages/user/LoginUserPage.java @@ -0,0 +1,45 @@ +package com.anhtester.pom.pages.user; + +import com.anhtester.keywords.WebKeyword; +import com.anhtester.managers.PageManager; +import com.anhtester.pom.pages.CommonPage; +import com.anhtester.pom.pages.admin.LoginPage; +import org.testng.Assert; + +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; + +public class LoginUserPage extends CommonPage { + private String URL_USER = "https://cms.anhtester.com/users/login"; + private String headerLoginPageUser = "//div[@class='card']//h1"; + private String buttonClosePopup = "//i[@class='la la-close fs-20']"; + private String inputEmail = "//input[@id='email']"; + private String inputPassword = "//input[@id='password']"; + private String buttonLogin = "//button[normalize-space()='Login']"; + private String headerDashboardPageUser = "//h1[normalize-space()='Dashboard']"; + private String alertMessage = "//div[@role='alert']"; + + public LoginUserPage loginUserCMS(String email, String password) { + WebKeyword.navigate(URL_USER); + PageManager.getPage().waitForLoadState(); + WebKeyword.click(buttonClosePopup); + assertThat(PageManager.getPage().locator(headerLoginPageUser)).hasText("Login to your account."); + WebKeyword.fill(inputEmail, email); + WebKeyword.fill(inputPassword, password); + WebKeyword.click(buttonLogin); + PageManager.getPage().waitForLoadState(); + return this; + } + + public LoginUserPage verifyLoginSuccess(){ + PageManager.getPage().waitForLoadState(); + Assert.assertTrue(PageManager.getPage().isVisible(headerDashboardPageUser), "FAIL. The button Clear Cache not visible."); + return this; + } + + public LoginUserPage verifyLoginFail(String message){ + PageManager.getPage().waitForLoadState(); + Assert.assertEquals(WebKeyword.textContent(alertMessage), message, "FAIL. The error message not match."); + Assert.assertFalse(PageManager.getPage().isVisible(headerDashboardPageUser), "FAIL. The button Clear Cache visible."); + return this; + } +} diff --git a/src/test/java/com/anhtester/pom/testcases/CategoryTest.java b/src/test/java/com/anhtester/pom/testcases/CategoryTest.java deleted file mode 100644 index e03283f..0000000 --- a/src/test/java/com/anhtester/pom/testcases/CategoryTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.anhtester.pom.testcases; - -import com.anhtester.common.BaseTest; -import com.anhtester.managers.PageManager; -import com.anhtester.pom.pages.CategoryPage; -import com.anhtester.pom.pages.CommonPage; -import com.anhtester.pom.pages.LoginPage; -import org.testng.annotations.Test; - -public class CategoryTest extends BaseTest { - - LoginPage loginPage; - CommonPage commonPage; - CategoryPage categoryPage; - - @Test - public void testAddNewCategory(){ - loginPage = new LoginPage(); - loginPage.loginAdminCMS("admin@example.com", "123456"); - commonPage = new CommonPage(PageManager.getPage()); - categoryPage = commonPage.openCategoryPage(); - categoryPage.addNewCategory("Acer Nitro 5 Tiger"); - categoryPage.checkCategory("Acer Nitro 5 Tiger"); - } -} diff --git a/src/test/java/com/anhtester/pom/testcases/LoginTest.java b/src/test/java/com/anhtester/pom/testcases/LoginTest.java deleted file mode 100644 index f41620f..0000000 --- a/src/test/java/com/anhtester/pom/testcases/LoginTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.anhtester.pom.testcases; - -import com.anhtester.common.BaseTest; -import com.anhtester.managers.PageManager; -import com.anhtester.pom.pages.LoginPage; -import org.testng.annotations.Test; - -public class LoginTest extends BaseTest { - LoginPage loginPage; - - @Test - public void loginTestAdminCMS(){ - loginPage = new LoginPage(); - loginPage.loginAdminCMS("admin@example.com", "123456"); - } - - @Test - public void loginTestUserCMS(){ - loginPage = new LoginPage(); - loginPage.loginUserCMS("customer123@example.com", "123456"); - } -} diff --git a/src/test/java/com/anhtester/pom/testcases/admin/CategoryTest.java b/src/test/java/com/anhtester/pom/testcases/admin/CategoryTest.java new file mode 100644 index 0000000..1e8848b --- /dev/null +++ b/src/test/java/com/anhtester/pom/testcases/admin/CategoryTest.java @@ -0,0 +1,18 @@ +package com.anhtester.pom.testcases.admin; + +import com.anhtester.common.BaseTest; +import com.anhtester.pom.pages.admin.CategoryPage; +import com.anhtester.pom.pages.CommonPage; +import com.anhtester.pom.pages.admin.LoginPage; +import org.testng.annotations.Test; + +public class CategoryTest extends BaseTest { + + @Test + public void testAddNewCategory() { + getLoginPage().loginAdminCMS("admin@example.com", "123456"); + getCategoryPage().openCategoryPage() + .addNewCategory("Acer Nitro 5 Tiger") + .checkCategory("Acer Nitro 5 Tiger"); + } +} diff --git a/src/test/java/com/anhtester/pom/testcases/admin/DashboardTest.java b/src/test/java/com/anhtester/pom/testcases/admin/DashboardTest.java new file mode 100644 index 0000000..c3c5cb8 --- /dev/null +++ b/src/test/java/com/anhtester/pom/testcases/admin/DashboardTest.java @@ -0,0 +1,19 @@ +package com.anhtester.pom.testcases.admin; + +import com.anhtester.common.BaseTest; +import com.anhtester.managers.PageManager; +import org.testng.annotations.Test; + +public class DashboardTest extends BaseTest { + + @Test + public void testTotalSummaryInDashboard() { + getLoginPage().loginAdminCMS("admin@example.com", "123456"); + PageManager.getPage().waitForLoadState(); + getDashboardPage().verifyTotalCustomer("124"); + getDashboardPage().verifyTotalOrder("734"); + getDashboardPage().verifyTotalProductCategory("439"); + getDashboardPage().verifyTotalProductBrand("84"); + } + +} diff --git a/src/test/java/com/anhtester/pom/testcases/admin/LoginTest.java b/src/test/java/com/anhtester/pom/testcases/admin/LoginTest.java new file mode 100644 index 0000000..0921301 --- /dev/null +++ b/src/test/java/com/anhtester/pom/testcases/admin/LoginTest.java @@ -0,0 +1,24 @@ +package com.anhtester.pom.testcases.admin; + +import com.anhtester.common.BaseTest; +import com.anhtester.pom.pages.admin.LoginPage; +import org.testng.annotations.Test; + +public class LoginTest extends BaseTest { + + LoginPage loginPage; + + @Test + public void testLoginSuccess() { + loginPage = new LoginPage(); + loginPage.loginAdminCMS("admin@example.com", "123456") + .verifyLoginSuccess(); + } + + @Test + public void testLoginFailWithEmailInvalid() { + loginPage = new LoginPage(); + loginPage.loginAdminCMS("admin123@example.com", "123456") + .verifyLoginFail("Invalid login credentials"); + } +} diff --git a/src/test/java/com/anhtester/pom/testcases/user/LoginUserTest.java b/src/test/java/com/anhtester/pom/testcases/user/LoginUserTest.java new file mode 100644 index 0000000..f07667d --- /dev/null +++ b/src/test/java/com/anhtester/pom/testcases/user/LoginUserTest.java @@ -0,0 +1,24 @@ +package com.anhtester.pom.testcases.user; + +import com.anhtester.common.BaseTest; +import com.anhtester.pom.pages.admin.LoginPage; +import com.anhtester.pom.pages.user.LoginUserPage; +import org.testng.annotations.Test; + +public class LoginUserTest extends BaseTest { + LoginUserPage loginUserPage; + + @Test + public void testLoginSuccess() { + loginUserPage = new LoginUserPage(); + loginUserPage.loginUserCMS("customer@example.com", "123456") + .verifyLoginSuccess(); + } + + @Test + public void testLoginFailWithEmailInvalid() { + loginUserPage = new LoginUserPage(); + loginUserPage.loginUserCMS("customer123@example.com", "123456") + .verifyLoginFail("Invalid login credentials"); + } +} diff --git a/src/test/resources/config/config.properties b/src/test/resources/config/config.properties index 9af7d08..b57afb1 100644 --- a/src/test/resources/config/config.properties +++ b/src/test/resources/config/config.properties @@ -1,11 +1,12 @@ BROWSER=chrome -EMAIL=admin@example.com -PASSWORD=123456 -TIMEOUT=20 +TIMEOUT_STEP=0 +TIMEOUT_PAGE_LOAD=40 URL=https://crm.anhtester.com/admin/authentication -SCREENSHOT_PATH=report/Screenshots/ -VIDEO_RECORD_PATH=report/VideoRecords/ -EXTENT_REPORT_PATH=report/ExtentReport/ExtentReport.html +VIEWPORT_WIDTH=1350 +VIEWPORT_HEIGHT=800 +SCREENSHOT_PATH=exports/Screenshots/ +VIDEO_RECORD_PATH=exports/VideoRecords/ +EXTENT_REPORT_PATH=exports/ExtentReport/ExtentReport.html HEADLESS=false VIDEO_RECORD=false SCREENSHOT_FAIL=true diff --git a/suites/CategoryTest.xml b/src/test/resources/suites/CategoryTest.xml similarity index 77% rename from suites/CategoryTest.xml rename to src/test/resources/suites/CategoryTest.xml index 64081ec..54633f8 100644 --- a/suites/CategoryTest.xml +++ b/src/test/resources/suites/CategoryTest.xml @@ -3,7 +3,7 @@ - + \ No newline at end of file diff --git a/src/test/resources/suites/DashboardTest.xml b/src/test/resources/suites/DashboardTest.xml new file mode 100644 index 0000000..4846420 --- /dev/null +++ b/src/test/resources/suites/DashboardTest.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/suites/LoginTest.xml b/src/test/resources/suites/LoginTest.xml similarity index 77% rename from suites/LoginTest.xml rename to src/test/resources/suites/LoginTest.xml index 1074356..20e3480 100644 --- a/suites/LoginTest.xml +++ b/src/test/resources/suites/LoginTest.xml @@ -3,7 +3,7 @@ - + \ No newline at end of file diff --git a/suites/SuiteAll.xml b/src/test/resources/suites/SuiteAll.xml similarity index 82% rename from suites/SuiteAll.xml rename to src/test/resources/suites/SuiteAll.xml index f701e8f..70549ae 100644 --- a/suites/SuiteAll.xml +++ b/src/test/resources/suites/SuiteAll.xml @@ -2,6 +2,7 @@ + \ No newline at end of file