Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.swaglabs.portal.qa.browsermanager;

import io.swaglabs.portal.qa.constants.WebPortalConstants;

public class BrowserFactory {

public IBrowser createBrowser(String browserName) {
return switch (BrowserName.fromString(browserName)) {
public IBrowser createBrowser() {
return switch (BrowserName.fromString(WebPortalConstants.BROWSER)) {
case FIREFOX -> new FirefoxBrowser();
case WEBKIT -> new WebkitBrowser();
case MS_EDGE -> new MsEdgeBrowser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ public class BrowserManager implements IBrowserManager<Page> {

@Override
public Page getBrowserPage(Playwright playwright) {
String browserName = WebPortalConstants.BROWSER;
String runMode = WebPortalConstants.RUN_MODE;
boolean isHeadless = runMode.equals("headless");
BrowserContext browserContext = new BrowserFactory().createBrowser(browserName).createSession(playwright, isHeadless);
boolean isHeadless = "headless".equals(WebPortalConstants.RUN_MODE);
BrowserContext browserContext = new BrowserFactory().createBrowser().createSession(playwright, isHeadless);
Objects.requireNonNull(browserContext, "Playwright Browser Context is null!");
return browserContext.newPage();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.swaglabs.portal.qa.browsermanager;

import io.swaglabs.portal.qa.exceptions.UtilsException;
import io.swaglabs.portal.qa.exceptions.WebUtilsException;
import lombok.AllArgsConstructor;
import lombok.Getter;

Expand All @@ -20,6 +20,6 @@ public enum BrowserName {
public static BrowserName fromString(String browserName) {
return Arrays.stream(BrowserName.values())
.filter(browserType -> browserType.getBrowserType().equalsIgnoreCase(browserName))
.findFirst().orElseThrow(() -> new UtilsException("Unknown browser: " + browserName));
.findFirst().orElseThrow(() -> new WebUtilsException("Unknown browser: " + browserName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
import com.microsoft.playwright.Playwright;
import io.swaglabs.portal.qa.constants.WebPortalConstants;

import java.util.List;

public class ChromeBrowser implements IBrowser {

@Override
public BrowserContext createSession(Playwright playwright, boolean isHeadless) {
return playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(isHeadless))
.newContext(new Browser.NewContextOptions()
.setViewportSize(WebPortalConstants.SCREEN_WIDTH, WebPortalConstants.SCREEN_HEIGHT)
.setTimezoneId(WebPortalConstants.TIME_ZONE));
BrowserType.LaunchOptions chromeLaunchOptions = WebPortalConstants.BROWSER_LAUNCH_OPTIONS.setHeadless(isHeadless)
.setArgs(List.of(WebPortalConstants.WINDOW_POSITION));
return playwright.chromium().launch(chromeLaunchOptions).newContext(new Browser.NewContextOptions()
.setViewportSize(WebPortalConstants.SCREEN_WIDTH, WebPortalConstants.SCREEN_HEIGHT)
.setTimezoneId(WebPortalConstants.TIME_ZONE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
public class FirefoxBrowser implements IBrowser {
@Override
public BrowserContext createSession(Playwright playwright, boolean isHeadless) {
return playwright.firefox().launch(new BrowserType.LaunchOptions()
.setHeadless(isHeadless))
.newContext(new Browser.NewContextOptions()
.setViewportSize(WebPortalConstants.SCREEN_WIDTH, WebPortalConstants.SCREEN_HEIGHT)
.setTimezoneId(WebPortalConstants.TIME_ZONE));
BrowserType.LaunchOptions firefoxLaunchOptions = WebPortalConstants.BROWSER_LAUNCH_OPTIONS.setHeadless(isHeadless);
return playwright.firefox().launch(firefoxLaunchOptions).newContext(new Browser.NewContextOptions()
.setViewportSize(WebPortalConstants.SCREEN_WIDTH, WebPortalConstants.SCREEN_HEIGHT)
.setTimezoneId(WebPortalConstants.TIME_ZONE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import com.microsoft.playwright.Playwright;
import io.swaglabs.portal.qa.constants.WebPortalConstants;

import java.util.List;

public class MsEdgeBrowser implements IBrowser {
@Override
public BrowserContext createSession(Playwright playwright, boolean isHeadless) {
return playwright.chromium().launch(new BrowserType.LaunchOptions()
.setHeadless(isHeadless).setChannel(BrowserName.MS_EDGE.getBrowserType()))
.newContext(new Browser.NewContextOptions()
.setViewportSize(WebPortalConstants.SCREEN_WIDTH, WebPortalConstants.SCREEN_HEIGHT)
.setTimezoneId(WebPortalConstants.TIME_ZONE));
BrowserType.LaunchOptions msedgeLaunchOptions = WebPortalConstants.BROWSER_LAUNCH_OPTIONS
.setHeadless(isHeadless)
.setChannel(BrowserName.MS_EDGE.getBrowserType())
.setArgs(List.of(WebPortalConstants.WINDOW_POSITION));
return playwright.chromium().launch(msedgeLaunchOptions).newContext(new Browser.NewContextOptions()
.setViewportSize(WebPortalConstants.SCREEN_WIDTH, WebPortalConstants.SCREEN_HEIGHT)
.setTimezoneId(WebPortalConstants.TIME_ZONE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
public class WebkitBrowser implements IBrowser {
@Override
public BrowserContext createSession(Playwright playwright, boolean isHeadless) {
return playwright.webkit().launch(new BrowserType.LaunchOptions()
.setHeadless(isHeadless))
.newContext(new Browser.NewContextOptions()
.setViewportSize(WebPortalConstants.SCREEN_WIDTH, WebPortalConstants.SCREEN_HEIGHT)
.setTimezoneId(WebPortalConstants.TIME_ZONE));
BrowserType.LaunchOptions webkitLaunchOptions = new BrowserType.LaunchOptions().setHeadless(isHeadless);
return playwright.webkit().launch(webkitLaunchOptions).newContext(new Browser.NewContextOptions()
.setViewportSize(WebPortalConstants.SCREEN_WIDTH, WebPortalConstants.SCREEN_HEIGHT)
.setTimezoneId(WebPortalConstants.TIME_ZONE));
}
}
10 changes: 0 additions & 10 deletions src/main/java/io/swaglabs/portal/qa/commons/WebBasePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import io.swaglabs.portal.qa.constants.WebPortalConstants;
import io.swaglabs.portal.qa.exceptions.WebPageException;
import io.swaglabs.portal.qa.locators.Locators;
import io.swaglabs.portal.qa.screenshotsmanager.ElementScreenshotStrategy;
import io.swaglabs.portal.qa.screenshotsmanager.ScreenshotContext;

import java.util.Objects;

public abstract class WebBasePage {

private static final String ELEMENT_SCREENSHOT_FILE_LOCATION = WebPortalConstants.SCREENSHOT_FILE_LOCATION + "/elements/"
+ WebPortalConstants.BROWSER + "_" + WebPortalConstants.RUN_MODE + "_Element_";
protected Page basePage;
protected Locators locators;

Expand Down Expand Up @@ -53,9 +48,4 @@ private String extractText(Locator locator, String errorMessage) {
Objects.requireNonNull(locator, errorMessage);
return locator.textContent().trim();
}

protected void takeElementScreenshot(Locator locator, String fileName) {
ScreenshotContext screenshotContext = new ScreenshotContext(new ElementScreenshotStrategy(locator));
screenshotContext.captureScreenshot(basePage, ELEMENT_SCREENSHOT_FILE_LOCATION + fileName + WebPortalConstants.IMAGE_FORMAT);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.swaglabs.portal.qa.constants;

import com.microsoft.playwright.BrowserType;
import io.swaglabs.portal.qa.utils.ScreenshotsUtils;
import io.swaglabs.portal.qa.utils.WebConfigLoader;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -18,9 +20,17 @@ public final class WebPortalConstants {
public static final String BROWSER = System.getProperty("browser");
public static final String TIME_ZONE = "Asia/Kolkata";

// Web Browser resolution
public static final int SCREEN_WIDTH = 1920;
public static final int SCREEN_HEIGHT = 1080;
public static final String WINDOW_POSITION = "--window-position=0,0";

public static final String SCREENSHOT_FILE_LOCATION = "./target/screenshots/";
public static final String IMAGE_FORMAT = ".png";

// Browser Type Launch Options Config
public static final BrowserType.LaunchOptions BROWSER_LAUNCH_OPTIONS = new BrowserType.LaunchOptions();

// SCREENSHOT STRATEGIES
public static final ScreenshotsUtils SCREENSHOTS_UTILS = ScreenshotsUtils.getInstance();
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.swaglabs.portal.qa.exceptions;

public class WebUtilsException extends RuntimeException {

public WebUtilsException(String errorMessage) {
super(errorMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.options.AriaRole;
import io.swaglabs.portal.qa.constants.WebPortalConstants;

public final class SwagLabsProductPage extends SwagLabsBasePage {

Expand All @@ -17,7 +18,7 @@ public String getProductNameText() {
public String getProductPriceText() {
Locator productPrice = locators.getPageLocator(".inventory_details_price");
String productPriceText = getTextContent(productPrice);
takeElementScreenshot(productPrice, productPriceText);
WebPortalConstants.SCREENSHOTS_UTILS.takeElementScreenshot(productPrice, productPriceText);
return productPriceText;
}

Expand All @@ -32,7 +33,7 @@ public boolean isProductAddedToCart() {

public boolean isShoppingCartClicked() {
Locator shoppingCart = locators.getPageLocator(".shopping_cart_link");
takeElementScreenshot(shoppingCart, "shoppingCart");
WebPortalConstants.SCREENSHOTS_UTILS.takeElementScreenshot(shoppingCart, "shoppingCart");
clickElement(shoppingCart);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package io.swaglabs.portal.qa.screenshotsmanager;

import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;

import java.nio.file.Paths;

@AllArgsConstructor
@RequiredArgsConstructor
public class ElementScreenshotStrategy implements ScreenshotStrategy {

private final Locator locator;
private final Locator LOCATOR;

@Override
public void capture(Page page, String filePath) {
locator.screenshot(new Locator.ScreenshotOptions().setPath(Paths.get(filePath)));
public void capture(String filePath) {
LOCATOR.screenshot(new Locator.ScreenshotOptions().setPath(Paths.get(filePath)));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.swaglabs.portal.qa.screenshotsmanager;

import com.microsoft.playwright.Page;
import lombok.RequiredArgsConstructor;

import java.nio.file.Paths;

@RequiredArgsConstructor
public class FullPageScreenshotStrategy implements ScreenshotStrategy {

private final Page PAGE;

@Override
public void capture(Page page, String filePath) {
page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(filePath)).setFullPage(true));
public void capture(String filePath) {
PAGE.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(filePath)).setFullPage(true));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package io.swaglabs.portal.qa.screenshotsmanager;

import com.microsoft.playwright.Page;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;

import java.util.Objects;

@AllArgsConstructor
@RequiredArgsConstructor
public class ScreenshotContext {

private ScreenshotStrategy screenshotStrategy;
private final ScreenshotStrategy screenshotStrategy;

public void captureScreenshot(Page page, String filePath) {
Objects.requireNonNull(screenshotStrategy, "Screenshot strategy is not set.");
screenshotStrategy.capture(page, filePath);
public void captureScreenshot(String filePath) {
screenshotStrategy.capture(filePath);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.swaglabs.portal.qa.screenshotsmanager;

import com.microsoft.playwright.Page;

public interface ScreenshotStrategy {

void capture(Page page, String filePath);
void capture(String filePath);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.swaglabs.portal.qa.utils;

import io.swaglabs.portal.qa.exceptions.UtilsException;
import io.swaglabs.portal.qa.exceptions.WebUtilsException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -23,7 +23,7 @@ public static Properties loadProperties(String fileName) {
properties = new Properties();
properties.load(fileInputStream);
} catch (IOException e) {
throw new UtilsException("Properties File failed loading..." + e.getMessage());
throw new WebUtilsException("Properties File failed loading..." + e.getMessage());
}
return properties;
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/io/swaglabs/portal/qa/utils/ScreenshotsUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.swaglabs.portal.qa.utils;

import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import io.swaglabs.portal.qa.constants.WebPortalConstants;
import io.swaglabs.portal.qa.screenshotsmanager.ElementScreenshotStrategy;
import io.swaglabs.portal.qa.screenshotsmanager.FullPageScreenshotStrategy;
import io.swaglabs.portal.qa.screenshotsmanager.ScreenshotContext;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Objects;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ScreenshotsUtils {

private static ScreenshotsUtils instance;
private static final String ELEMENT_SCREENSHOT_FILE_LOCATION = WebPortalConstants.SCREENSHOT_FILE_LOCATION
+ "/elements/" + WebPortalConstants.BROWSER + "_" + WebPortalConstants.RUN_MODE + "_Element_";

public static ScreenshotsUtils getInstance() {
if (instance == null) {
synchronized (ScreenshotsUtils.class) {
if (instance == null) {
instance = new ScreenshotsUtils();
}
}
}
return instance;
}

public ScreenshotContext getFullPageScreenshotContext(Page page) {
return new ScreenshotContext(new FullPageScreenshotStrategy(page));
}

public void takeElementScreenshot(Locator locator, String fileName) {
Objects.requireNonNull(locator, "Locator cannot be null.");
Objects.requireNonNull(fileName, "File name cannot be null.");
String filePath = ELEMENT_SCREENSHOT_FILE_LOCATION + fileName + WebPortalConstants.IMAGE_FORMAT;
new ScreenshotContext(new ElementScreenshotStrategy(locator)).captureScreenshot(filePath);
}
}
Loading