Skip to content

Commit f9e60aa

Browse files
committed
Update Playwright 1.52.0 and Parallel Execution stable
1 parent f5ac40d commit f9e60aa

File tree

17 files changed

+239
-104
lines changed

17 files changed

+239
-104
lines changed

pom.xml

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

77
<groupId>com.anhtester</groupId>
88
<artifactId>PlaywrightJava</artifactId>
9-
<version>1.48.0</version>
9+
<version>1.52.0</version>
1010

1111
<properties>
1212
<maven.compiler.source>17</maven.compiler.source>
1313
<maven.compiler.target>17</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515

16-
<log4j.version>2.24.1</log4j.version>
17-
<slf4j.version>2.0.16</slf4j.version>
16+
<log4j.version>2.24.3</log4j.version>
17+
<slf4j.version>2.0.17</slf4j.version>
1818
</properties>
1919

2020
<dependencies>
2121
<!-- https://mvnrepository.com/artifact/com.microsoft.playwright/playwright -->
2222
<dependency>
2323
<groupId>com.microsoft.playwright</groupId>
2424
<artifactId>playwright</artifactId>
25-
<version>1.48.0</version>
25+
<version>1.52.0</version>
2626
</dependency>
2727

2828
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
2929
<dependency>
3030
<groupId>org.testng</groupId>
3131
<artifactId>testng</artifactId>
32-
<version>7.10.2</version>
32+
<version>7.11.0</version>
3333
</dependency>
3434

3535
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
@@ -50,21 +50,21 @@
5050
<dependency>
5151
<groupId>org.apache.poi</groupId>
5252
<artifactId>poi</artifactId>
53-
<version>5.3.0</version>
53+
<version>5.4.1</version>
5454
</dependency>
5555

5656
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
5757
<dependency>
5858
<groupId>org.apache.poi</groupId>
5959
<artifactId>poi-ooxml</artifactId>
60-
<version>5.3.0</version>
60+
<version>5.4.1</version>
6161
</dependency>
6262

6363
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
6464
<dependency>
6565
<groupId>commons-io</groupId>
6666
<artifactId>commons-io</artifactId>
67-
<version>2.17.0</version>
67+
<version>2.19.0</version>
6868
</dependency>
6969

7070
<!-- https://mvnrepository.com/artifact/com.github.stephenc.monte/monte-screen-recorder -->
@@ -99,36 +99,36 @@
9999
<dependency>
100100
<groupId>io.qameta.allure</groupId>
101101
<artifactId>allure-testng</artifactId>
102-
<version>2.29.0</version>
102+
<version>2.29.1</version>
103103
</dependency>
104104

105105
<!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-attachments -->
106106
<dependency>
107107
<groupId>io.qameta.allure</groupId>
108108
<artifactId>allure-attachments</artifactId>
109-
<version>2.29.0</version>
109+
<version>2.29.1</version>
110110
</dependency>
111111

112112
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
113113
<dependency>
114114
<groupId>org.aspectj</groupId>
115115
<artifactId>aspectjweaver</artifactId>
116-
<version>1.9.22.1</version>
116+
<version>1.9.24</version>
117117
</dependency>
118118

119119
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
120120
<dependency>
121121
<groupId>org.projectlombok</groupId>
122122
<artifactId>lombok</artifactId>
123-
<version>1.18.34</version>
123+
<version>1.18.38</version>
124124
<scope>provided</scope>
125125
</dependency>
126126

127127
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
128128
<dependency>
129129
<groupId>com.google.guava</groupId>
130130
<artifactId>guava</artifactId>
131-
<version>33.3.1-jre</version>
131+
<version>33.4.8-jre</version>
132132
</dependency>
133133

134134
</dependencies>
@@ -138,20 +138,20 @@
138138
<plugin>
139139
<groupId>org.apache.maven.plugins</groupId>
140140
<artifactId>maven-compiler-plugin</artifactId>
141-
<version>3.13.0</version>
141+
<version>3.14.0</version>
142142
</plugin>
143143

144144
<plugin>
145145
<groupId>org.apache.maven.plugins</groupId>
146146
<artifactId>maven-surefire-plugin</artifactId>
147-
<version>3.2.5</version>
147+
<version>3.5.3</version>
148148
<configuration>
149149
<suiteXmlFiles>
150-
<suiteXmlFile>src/test/resources/suites/SuiteAll.xml</suiteXmlFile>
150+
<suiteXmlFile>src/test/resources/suites/LoginTest.xml</suiteXmlFile>
151151
</suiteXmlFiles>
152152

153153
<argLine>
154-
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.22.1/aspectjweaver-1.9.22.1.jar"
154+
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.9.24/aspectjweaver-1.9.24.jar"
155155
</argLine>
156156
<testFailureIgnore>true</testFailureIgnore>
157157
<skipTests>false</skipTests>

src/main/java/com/anhtester/constants/AppConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public class AppConfig {
1212
public static boolean SCREENSHOT_FAIL = Boolean.parseBoolean(PropertiesHelper.getValue("SCREENSHOT_FAIL"));
1313
public static boolean SCREENSHOT_PASS = Boolean.parseBoolean(PropertiesHelper.getValue("SCREENSHOT_PASS"));
1414
public static int TIMEOUT_STEP = Integer.parseInt(PropertiesHelper.getValue("TIMEOUT_STEP"));
15+
public static int TIMEOUT_PAGE_LOAD = Integer.parseInt(PropertiesHelper.getValue("TIMEOUT_PAGE_LOAD"));
1516
public static boolean TRACE_VIEWER = Boolean.parseBoolean(PropertiesHelper.getValue("TRACE_VIEWER"));
17+
public static String TRACING_PATH = PropertiesHelper.getValue("TRACING_PATH");
18+
public static String SCREENSHOT_PATH = PropertiesHelper.getValue("SCREENSHOT_PATH");
19+
public static String VIDEO_RECORD_PATH = PropertiesHelper.getValue("VIDEO_RECORD_PATH");
20+
public static String EXTENT_REPORT_PATH = PropertiesHelper.getValue("EXTENT_REPORT_PATH");
1621

1722
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.anhtester.helpers;
22

3+
import com.anhtester.constants.AppConfig;
34
import com.anhtester.managers.PageManager;
45

56
import org.monte.media.Format;
@@ -52,7 +53,7 @@ protected File createMovieFile(Format fileFormat) throws IOException {
5253
// Start record video
5354
public static void startRecord(String methodName) {
5455
//Tạo thư mục để lưu file video vào
55-
File file = new File(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("VIDEO_RECORD_PATH"));
56+
File file = new File(SystemHelper.getCurrentDir() + AppConfig.VIDEO_RECORD_PATH);
5657
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
5758
int width = screenSize.width;
5859
int height = screenSize.height;
@@ -89,12 +90,12 @@ public static void takeScreenshot(String screenshotName) {
8990
// screenshots
9091
ScreenshotOptions screenshotOptions = new ScreenshotOptions();
9192

92-
File theDir = new File(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("SCREENSHOT_PATH"));
93+
File theDir = new File(SystemHelper.getCurrentDir() + AppConfig.SCREENSHOT_PATH);
9394
if (!theDir.exists()) {
9495
theDir.mkdirs();
9596
}
9697

97-
PageManager.getPage().screenshot(screenshotOptions.setPath(Paths.get(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("SCREENSHOT_PATH") + File.separator + screenshotName + "_" + dateFormat.format(new Date()) + ".png")));
98+
PageManager.getPage().screenshot(screenshotOptions.setPath(Paths.get(SystemHelper.getCurrentDir() + AppConfig.SCREENSHOT_PATH + File.separator + screenshotName + "_" + dateFormat.format(new Date()) + ".png")));
9899
System.out.println("Screenshot taken: " + screenshotName);
99100
System.out.println("Screenshot taken current URL: " + PageManager.getPage().url());
100101
} catch (Exception e) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.anhtester.reports.ExtentTestManager;
88
import com.anhtester.utils.LogUtils;
99
import com.aventstack.extentreports.Status;
10+
import com.microsoft.playwright.Page;
11+
import com.microsoft.playwright.options.LoadState;
1012
import io.qameta.allure.Step;
1113
import org.testng.asserts.SoftAssert;
1214

@@ -126,4 +128,10 @@ public static String innerText(String locator) {
126128
return text;
127129
}
128130

131+
@Step("Wait for page loaded")
132+
public static void waitForPageLoaded() {
133+
PageManager.getPage().waitForLoadState(LoadState.LOAD);
134+
LogUtils.info("Wait for page loaded");
135+
}
136+
129137
}
Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
package com.anhtester.managers;
22

33
import com.anhtester.constants.AppConfig;
4-
import com.anhtester.keywords.WebKeyword;
54
import com.microsoft.playwright.*;
5+
import com.microsoft.playwright.options.LoadState;
66

77
import java.awt.*;
88
import java.nio.file.Paths;
9+
import java.util.Arrays;
910

1011
public class BrowserFactory {
11-
protected static Playwright playwright;
12-
protected static Browser browser;
13-
protected static BrowserContext browserContext;
14-
protected static Page page;
12+
protected Playwright playwright;
13+
protected Browser browser;
14+
protected BrowserContext browserContext;
15+
protected Page page;
1516
//https://playwright.dev/java/docs/browsers
1617

17-
public static void createBrowser(String browserName) {
18+
public void createBrowser(String browserName) {
1819
playwright = Playwright.create();
1920
PageManager.setPlaywright(playwright);
2021

2122
switch (browserName.toLowerCase().trim()) {
2223
case "chromium":
2324
System.out.println("Create Chromium browser...");
24-
browser = (PageManager.getPlaywright().chromium().launch(new BrowserType.LaunchOptions().setHeadless(AppConfig.HEADLESS)));
25+
browser = (PageManager.getPlaywright().chromium().launch(new BrowserType.LaunchOptions().setHeadless(AppConfig.HEADLESS).setArgs(Arrays.asList("--start-maximized"))));
2526
break;
2627
case "chrome":
2728
System.out.println("Create Chrome browser...");
28-
browser = (PageManager.getPlaywright().chromium().launch(new BrowserType.LaunchOptions().setChannel("chrome").setHeadless(AppConfig.HEADLESS)));
29+
browser = (PageManager.getPlaywright().chromium().launch(new BrowserType.LaunchOptions().setChannel("chrome").setHeadless(AppConfig.HEADLESS).setArgs(Arrays.asList("--start-maximized"))));
2930
break;
3031
case "edge":
3132
System.out.println("Create Edge browser...");
32-
browser = (PageManager.getPlaywright().chromium().launch(new BrowserType.LaunchOptions().setChannel("msedge").setHeadless(AppConfig.HEADLESS)));
33+
browser = (PageManager.getPlaywright().chromium().launch(new BrowserType.LaunchOptions().setChannel("msedge").setHeadless(AppConfig.HEADLESS).setArgs(Arrays.asList("--start-maximized"))));
3334
break;
3435
case "firefox":
3536
System.out.println("Create Firefox browser...");
@@ -41,32 +42,43 @@ public static void createBrowser(String browserName) {
4142
break;
4243
default:
4344
System.out.println("Set default Chromium browser...");
44-
browser = (PageManager.getPlaywright().chromium().launch(new BrowserType.LaunchOptions().setHeadless(AppConfig.HEADLESS)));
45+
browser = (PageManager.getPlaywright().chromium().launch(new BrowserType.LaunchOptions().setHeadless(AppConfig.HEADLESS).setArgs(Arrays.asList("--start-maximized"))));
4546
break;
4647
}
4748

4849
PageManager.setBrowser(browser);
4950

51+
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
52+
int width = (int) screenSize.getWidth();
53+
int height = (int) screenSize.getHeight();
54+
5055
// System.out.println("Screen Browser Width (customize): " + AppConfig.VIEWPORT_WIDTH);
5156
// System.out.println("Screen Browser Height (customize): " + AppConfig.VIEWPORT_HEIGHT);
57+
System.out.println("Screen Browser Width (default): " + width);
58+
System.out.println("Screen Browser Height (default): " + height);
5259

53-
browserContext = PageManager.getBrowser().newContext(new Browser.NewContextOptions()
54-
.setRecordVideoDir(Paths.get("exports/videos/"))
55-
.setViewportSize(AppConfig.VIEWPORT_WIDTH, AppConfig.VIEWPORT_HEIGHT)
56-
.setDeviceScaleFactor(1));
57-
58-
// Start tracing before creating / navigating a page.
59-
if (AppConfig.TRACE_VIEWER == true) {
60-
browserContext.tracing().start(new Tracing.StartOptions()
61-
.setScreenshots(true)
62-
.setSnapshots(true)
63-
.setSources(true));
60+
if(AppConfig.HEADLESS) {
61+
browserContext = PageManager.getBrowser().newContext(new Browser.NewContextOptions()
62+
.setViewportSize(AppConfig.VIEWPORT_WIDTH, AppConfig.VIEWPORT_HEIGHT)
63+
.setRecordVideoDir(Paths.get(AppConfig.VIDEO_RECORD_PATH))
64+
.setRecordVideoSize(AppConfig.VIEWPORT_WIDTH, AppConfig.VIEWPORT_HEIGHT)
65+
//.setDeviceScaleFactor(1)
66+
);
67+
}else {
68+
browserContext = PageManager.getBrowser().newContext(new Browser.NewContextOptions()
69+
.setViewportSize(null)
70+
.setRecordVideoDir(Paths.get(AppConfig.VIDEO_RECORD_PATH))
71+
.setRecordVideoSize(width, height)
72+
//.setDeviceScaleFactor(1)
73+
);
6474
}
6575

6676
PageManager.setBrowserContext(browserContext);
6777
page = PageManager.getBrowserContext().newPage();
78+
page.waitForLoadState(LoadState.LOAD, new Page.WaitForLoadStateOptions().setTimeout(AppConfig.TIMEOUT_PAGE_LOAD));
6879

6980
PageManager.setPage(page);
70-
//WebKeyword.maximizeBrowserOnWindow();
81+
7182
}
83+
7284
}

src/main/java/com/anhtester/managers/PageManager.java

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.anhtester.managers;
22

3+
import com.anhtester.constants.AppConfig;
34
import com.anhtester.utils.LogUtils;
45
import com.microsoft.playwright.*;
56

@@ -52,13 +53,6 @@ public static void closePage() {
5253
}
5354
}
5455

55-
public static void closeTracing(String path) {
56-
// Stop tracing and export it into a zip archive.
57-
browserContextThreadLocal.get().tracing().stop(new Tracing.StopOptions()
58-
.setPath(Paths.get(path)));
59-
LogUtils.info("Closed Tracing.");
60-
}
61-
6256
public static void closeBrowser() {
6357
if (browserThreadLocal.get() != null) {
6458
browserThreadLocal.get().close();
@@ -67,4 +61,55 @@ public static void closeBrowser() {
6761
}
6862
}
6963

64+
public static void closeBrowserContext() {
65+
if (browserContextThreadLocal.get() != null) {
66+
browserContextThreadLocal.get().close();
67+
browserContextThreadLocal.remove();
68+
LogUtils.info("Closed browser context.");
69+
}
70+
}
71+
72+
public static void closePlaywright() {
73+
if (playwrightThreadLocal.get() != null) {
74+
playwrightThreadLocal.get().close();
75+
playwrightThreadLocal.remove();
76+
LogUtils.info("Closed Playwright.");
77+
}
78+
}
79+
80+
public static void closeAll() {
81+
//closePage();
82+
closeBrowserContext();
83+
closeBrowser();
84+
closePlaywright();
85+
}
86+
87+
public static void startTracing() {
88+
// Start tracing before performing actions.
89+
if (AppConfig.TRACE_VIEWER) {
90+
browserContextThreadLocal.get().tracing().start(new Tracing.StartOptions()
91+
.setScreenshots(true)
92+
.setSnapshots(true)
93+
.setSources(true));
94+
LogUtils.info("Started tracing.");
95+
}
96+
}
97+
98+
public static void closeTracing() {
99+
// Stop tracing and export it into a zip archive.
100+
if (AppConfig.TRACE_VIEWER) {
101+
browserContextThreadLocal.get().tracing().stop();
102+
LogUtils.info("Closed tracing.");
103+
}
104+
}
105+
106+
public static void closeTracing(String path) {
107+
// Stop tracing and export it into a zip archive.
108+
if (AppConfig.TRACE_VIEWER) {
109+
browserContextThreadLocal.get().tracing().stop(new Tracing.StopOptions()
110+
.setPath(Paths.get(path)));
111+
LogUtils.info("Closed tracing.");
112+
}
113+
}
114+
70115
}

src/main/java/com/anhtester/reports/AllureManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.anhtester.reports;
22

3+
import com.anhtester.constants.AppConfig;
34
import com.anhtester.helpers.FileHelper;
45
import com.anhtester.helpers.PropertiesHelper;
56
import com.anhtester.helpers.SystemHelper;
@@ -30,14 +31,14 @@ public static byte[] saveScreenshotPNG() {
3031
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH-mm-ss");
3132

3233
ScreenshotOptions screenshotOptions = new ScreenshotOptions();
33-
return PageManager.getPage().screenshot(screenshotOptions.setPath(Paths.get(SystemHelper.getCurrentDir() + PropertiesHelper.getValue("SCREENSHOT_PATH") + File.separator + dateFormat.format(new Date()) + ".png")));
34+
return PageManager.getPage().screenshot(screenshotOptions.setPath(Paths.get(SystemHelper.getCurrentDir() + AppConfig.SCREENSHOT_PATH + File.separator + dateFormat.format(new Date()) + ".png")));
3435
}
3536

3637
public static void addAttachmentVideoAVI() {
37-
if (PropertiesHelper.getValue("VIDEO_RECORD").equals("true")) {
38+
if (AppConfig.VIDEO_RECORD) {
3839
try {
3940
//Get file Last Modified in folder
40-
File video = FileHelper.getFileLastModified(PropertiesHelper.getValue("VIDEO_RECORD_PATH"));
41+
File video = FileHelper.getFileLastModified(AppConfig.VIDEO_RECORD_PATH);
4142
if (video != null) {
4243
Allure.addAttachment("Failed test Video record AVI", "video/avi", Files.asByteSource(video).openStream(), ".avi");
4344
} else {

0 commit comments

Comments
 (0)