Skip to content

Commit

Permalink
Update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vothaian committed Jul 19, 2024
1 parent 997c6e1 commit 12d2fcb
Show file tree
Hide file tree
Showing 28 changed files with 480 additions and 220 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
target/
out/
bin/
.vs/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
Expand All @@ -11,6 +14,6 @@ buildNumber.properties
.mvn/wrapper/maven-wrapper.jar
*.zip
.idea/
report/
logs/
*.iml
exports/
*.iml
27 changes: 14 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.anhtester</groupId>
<artifactId>PlaywrightJava</artifactId>
<version>1.44.0</version>
<version>1.45.0</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand All @@ -19,7 +19,7 @@
<dependency>
<groupId>com.microsoft.playwright</groupId>
<artifactId>playwright</artifactId>
<version>1.44.0</version>
<version>1.45.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
Expand All @@ -40,21 +40,21 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.11</version>
<version>2.0.13</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.5</version>
<version>5.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.5</version>
<version>5.3.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
Expand Down Expand Up @@ -89,21 +89,21 @@
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>5.1.1</version>
<version>5.1.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.27.0</version>
<version>2.28.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-attachments -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-attachments</artifactId>
<version>2.27.0</version>
<version>2.28.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
Expand All @@ -117,15 +117,15 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.2.0-jre</version>
<version>33.2.1-jre</version>
</dependency>

</dependencies>
Expand All @@ -135,7 +135,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<version>3.13.0</version>
</plugin>

<plugin>
Expand All @@ -144,13 +144,14 @@
<version>3.2.5</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>suites/SuiteAll.xml</suiteXmlFile>
<suiteXmlFile>src/test/resources/suites/SuiteAll.xml</suiteXmlFile>
</suiteXmlFiles>

<argLine>
-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"
</argLine>
<testFailureIgnore>true</testFailureIgnore>
<skipTests>false</skipTests>
<systemPropertyVariables>
<allure.results.directory>target/allure-results</allure.results.directory>
</systemPropertyVariables>
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/anhtester/constants/AppConfig.java
Original file line number Diff line number Diff line change
@@ -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"));
}
6 changes: 6 additions & 0 deletions src/main/java/com/anhtester/helpers/SystemHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
72 changes: 63 additions & 9 deletions src/main/java/com/anhtester/keywords/WebKeyword.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
}
Expand All @@ -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);
}
Expand All @@ -60,16 +80,50 @@ 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);
String text = PageManager.getPage().locator(locator).textContent();
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;
}

}
20 changes: 6 additions & 14 deletions src/main/java/com/anhtester/managers/BrowserFactory.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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);

Expand Down Expand Up @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/main/resources/log4j2.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
14 changes: 12 additions & 2 deletions src/test/java/com/anhtester/common/BaseTest.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
Loading

0 comments on commit 12d2fcb

Please sign in to comment.