Skip to content

Commit 1986add

Browse files
Refactored Screenshot Strategy.
1 parent 4874534 commit 1986add

10 files changed

Lines changed: 40 additions & 40 deletions

File tree

src/main/java/io/swaglabs/portal/qa/browsermanager/ChromeBrowser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
import com.microsoft.playwright.Playwright;
77
import io.swaglabs.portal.qa.constants.WebPortalConstants;
88

9+
import java.util.List;
10+
911
public class ChromeBrowser implements IBrowser {
1012

1113
@Override
1214
public BrowserContext createSession(Playwright playwright, boolean isHeadless) {
13-
BrowserType.LaunchOptions chromeLaunchOptions = WebPortalConstants.BROWSER_LAUNCH_OPTIONS.setHeadless(isHeadless);
15+
BrowserType.LaunchOptions chromeLaunchOptions = WebPortalConstants.BROWSER_LAUNCH_OPTIONS.setHeadless(isHeadless)
16+
.setArgs(List.of("--window-position=0,0"));
1417
return playwright.chromium().launch(chromeLaunchOptions).newContext(new Browser.NewContextOptions()
1518
.setViewportSize(WebPortalConstants.SCREEN_WIDTH, WebPortalConstants.SCREEN_HEIGHT)
1619
.setTimezoneId(WebPortalConstants.TIME_ZONE));

src/main/java/io/swaglabs/portal/qa/browsermanager/MsEdgeBrowser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import com.microsoft.playwright.Playwright;
77
import io.swaglabs.portal.qa.constants.WebPortalConstants;
88

9+
import java.util.List;
10+
911
public class MsEdgeBrowser implements IBrowser {
1012
@Override
1113
public BrowserContext createSession(Playwright playwright, boolean isHeadless) {
1214
BrowserType.LaunchOptions msedgeLaunchOptions = WebPortalConstants.BROWSER_LAUNCH_OPTIONS
1315
.setHeadless(isHeadless)
14-
.setChannel(BrowserName.MS_EDGE.getBrowserType());
16+
.setChannel(BrowserName.MS_EDGE.getBrowserType())
17+
.setArgs(List.of("--window-position=0,0"));
1518
return playwright.chromium().launch(msedgeLaunchOptions).newContext(new Browser.NewContextOptions()
1619
.setViewportSize(WebPortalConstants.SCREEN_WIDTH, WebPortalConstants.SCREEN_HEIGHT)
1720
.setTimezoneId(WebPortalConstants.TIME_ZONE));

src/main/java/io/swaglabs/portal/qa/constants/WebPortalConstants.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package io.swaglabs.portal.qa.constants;
22

33
import com.microsoft.playwright.BrowserType;
4-
import io.swaglabs.portal.qa.screenshotsmanager.ScreenshotContext;
5-
import io.swaglabs.portal.qa.screenshotsmanager.ScreenshotsUtils;
4+
import io.swaglabs.portal.qa.utils.ScreenshotsUtils;
65
import io.swaglabs.portal.qa.utils.WebConfigLoader;
76
import lombok.AccessLevel;
87
import lombok.NoArgsConstructor;
@@ -31,10 +30,8 @@ public final class WebPortalConstants {
3130
public static final String IMAGE_FORMAT = ".png";
3231

3332
// Browser Type Launch Options Config
34-
public static final BrowserType.LaunchOptions BROWSER_LAUNCH_OPTIONS = new BrowserType.LaunchOptions()
35-
.setArgs(List.of("--window-position=0,0"));
33+
public static final BrowserType.LaunchOptions BROWSER_LAUNCH_OPTIONS = new BrowserType.LaunchOptions();
3634

3735
// SCREENSHOT STRATEGIES
3836
public static final ScreenshotsUtils SCREENSHOTS_UTILS = ScreenshotsUtils.getInstance();
39-
public static final ScreenshotContext FULL_PAGE_SCREENSHOT = SCREENSHOTS_UTILS.getFullPageScreenshotContext();
4037
}

src/main/java/io/swaglabs/portal/qa/pages/SwagLabsProductPage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public String getProductNameText() {
1818
public String getProductPriceText() {
1919
Locator productPrice = locators.getPageLocator(".inventory_details_price");
2020
String productPriceText = getTextContent(productPrice);
21-
WebPortalConstants.SCREENSHOTS_UTILS.takeElementScreenshot(basePage, productPrice, productPriceText);
21+
WebPortalConstants.SCREENSHOTS_UTILS.takeElementScreenshot(productPrice, productPriceText);
2222
return productPriceText;
2323
}
2424

@@ -33,7 +33,7 @@ public boolean isProductAddedToCart() {
3333

3434
public boolean isShoppingCartClicked() {
3535
Locator shoppingCart = locators.getPageLocator(".shopping_cart_link");
36-
WebPortalConstants.SCREENSHOTS_UTILS.takeElementScreenshot(basePage, shoppingCart, "shoppingCart");
36+
WebPortalConstants.SCREENSHOTS_UTILS.takeElementScreenshot(shoppingCart, "shoppingCart");
3737
clickElement(shoppingCart);
3838
return true;
3939
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package io.swaglabs.portal.qa.screenshotsmanager;
22

33
import com.microsoft.playwright.Locator;
4-
import com.microsoft.playwright.Page;
5-
import lombok.AllArgsConstructor;
4+
import lombok.RequiredArgsConstructor;
65

76
import java.nio.file.Paths;
87

9-
@AllArgsConstructor
8+
@RequiredArgsConstructor
109
public class ElementScreenshotStrategy implements ScreenshotStrategy {
1110

1211
private final Locator locator;
1312

1413
@Override
15-
public void capture(Page page, String filePath) {
14+
public void capture(String filePath) {
1615
locator.screenshot(new Locator.ScreenshotOptions().setPath(Paths.get(filePath)));
1716
}
1817
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package io.swaglabs.portal.qa.screenshotsmanager;
22

33
import com.microsoft.playwright.Page;
4+
import lombok.RequiredArgsConstructor;
45

56
import java.nio.file.Paths;
67

8+
@RequiredArgsConstructor
79
public class FullPageScreenshotStrategy implements ScreenshotStrategy {
810

11+
private final Page PAGE;
12+
913
@Override
10-
public void capture(Page page, String filePath) {
11-
page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(filePath)).setFullPage(true));
14+
public void capture(String filePath) {
15+
PAGE.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(filePath)).setFullPage(true));
1216
}
1317
}
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package io.swaglabs.portal.qa.screenshotsmanager;
22

3-
import com.microsoft.playwright.Page;
4-
import lombok.AllArgsConstructor;
3+
import lombok.RequiredArgsConstructor;
54

6-
import java.util.Objects;
7-
8-
@AllArgsConstructor
5+
@RequiredArgsConstructor
96
public class ScreenshotContext {
107

11-
private ScreenshotStrategy screenshotStrategy;
8+
private final ScreenshotStrategy screenshotStrategy;
129

13-
public void captureScreenshot(Page page, String filePath) {
14-
Objects.requireNonNull(screenshotStrategy, "Screenshot strategy is not set.");
15-
screenshotStrategy.capture(page, filePath);
10+
public void captureScreenshot(String filePath) {
11+
screenshotStrategy.capture(filePath);
1612
}
1713
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package io.swaglabs.portal.qa.screenshotsmanager;
22

3-
import com.microsoft.playwright.Page;
4-
53
public interface ScreenshotStrategy {
64

7-
void capture(Page page, String filePath);
5+
void capture(String filePath);
86
}

src/main/java/io/swaglabs/portal/qa/screenshotsmanager/ScreenshotsUtils.java renamed to src/main/java/io/swaglabs/portal/qa/utils/ScreenshotsUtils.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
package io.swaglabs.portal.qa.screenshotsmanager;
1+
package io.swaglabs.portal.qa.utils;
22

33
import com.microsoft.playwright.Locator;
44
import com.microsoft.playwright.Page;
55
import io.swaglabs.portal.qa.constants.WebPortalConstants;
6+
import io.swaglabs.portal.qa.screenshotsmanager.ElementScreenshotStrategy;
7+
import io.swaglabs.portal.qa.screenshotsmanager.FullPageScreenshotStrategy;
8+
import io.swaglabs.portal.qa.screenshotsmanager.ScreenshotContext;
69
import lombok.AccessLevel;
710
import lombok.NoArgsConstructor;
811

@@ -11,9 +14,9 @@
1114
@NoArgsConstructor(access = AccessLevel.PRIVATE)
1215
public class ScreenshotsUtils {
1316

17+
private static ScreenshotsUtils instance;
1418
private static final String ELEMENT_SCREENSHOT_FILE_LOCATION = WebPortalConstants.SCREENSHOT_FILE_LOCATION
1519
+ "/elements/" + WebPortalConstants.BROWSER + "_" + WebPortalConstants.RUN_MODE + "_Element_";
16-
private static ScreenshotsUtils instance;
1720

1821
public static ScreenshotsUtils getInstance() {
1922
if (instance == null) {
@@ -26,17 +29,14 @@ public static ScreenshotsUtils getInstance() {
2629
return instance;
2730
}
2831

29-
public ScreenshotContext getFullPageScreenshotContext() {
30-
return new ScreenshotContext(new FullPageScreenshotStrategy());
31-
}
32-
33-
public ScreenshotContext getElementScreenshotContext(Locator locator) {
34-
Objects.requireNonNull(locator, "Locator cannot be null for taking element screenshot");
35-
return new ScreenshotContext(new ElementScreenshotStrategy(locator));
32+
public ScreenshotContext getFullPageScreenshotContext(Page page) {
33+
return new ScreenshotContext(new FullPageScreenshotStrategy(page));
3634
}
3735

38-
public void takeElementScreenshot(Page page, Locator locator, String fileName) {
39-
getElementScreenshotContext(locator).captureScreenshot(page,
40-
ELEMENT_SCREENSHOT_FILE_LOCATION + fileName + WebPortalConstants.IMAGE_FORMAT);
36+
public void takeElementScreenshot(Locator locator, String fileName) {
37+
Objects.requireNonNull(locator, "Locator cannot be null.");
38+
Objects.requireNonNull(fileName, "File name cannot be null.");
39+
String filePath = ELEMENT_SCREENSHOT_FILE_LOCATION + fileName + WebPortalConstants.IMAGE_FORMAT;
40+
new ScreenshotContext(new ElementScreenshotStrategy(locator)).captureScreenshot(filePath);
4141
}
4242
}

src/test/java/io/swaglabs/portal/qa/listeners/WebTestListeners.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void takeScreenshot(ITestResult testResult) {
7171
String filePath = Paths.get(dirPath, fileName).toString();
7272
try {
7373
Files.createDirectories(Paths.get(dirPath));
74-
WebPortalConstants.FULL_PAGE_SCREENSHOT.captureScreenshot(currentPage, filePath);
74+
WebPortalConstants.SCREENSHOTS_UTILS.getFullPageScreenshotContext(currentPage).captureScreenshot(filePath);
7575
} catch (IOException e) {
7676
log.error("Screenshot failed for {}: {}", testName, e.getMessage());
7777
}

0 commit comments

Comments
 (0)