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
36 changes: 0 additions & 36 deletions src/main/java/io/swaglabs/portal/qa/commons/WebBasePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import com.microsoft.playwright.Page;
import io.swaglabs.portal.qa.constants.WebPortalConstants;
import io.swaglabs.portal.qa.exceptions.WebPageException;
import io.swaglabs.portal.qa.listeners.WebTestListeners;
import io.swaglabs.portal.qa.locators.Locators;
import io.swaglabs.portal.qa.screenshotsmanager.ElementScreenshotStrategy;
import io.swaglabs.portal.qa.screenshotsmanager.ScreenshotContext;
import org.testng.ITestResult;
import org.testng.Reporter;

import java.util.Objects;
import java.util.function.Consumer;

public abstract class WebBasePage {

Expand Down Expand Up @@ -62,36 +58,4 @@ 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);
}

protected void switchToNewTabAndRun(Page newTabPage, Consumer<WebBasePage> actionsInNewTab) {
Page originalPage = this.basePage;
Locators originalLocators = this.locators;
try {
// Update to new tab context
this.basePage = newTabPage;
this.locators = new Locators(newTabPage);
WebTestListeners.setPage(newTabPage);
actionsInNewTab.accept(this); // Perform operations in new tab
} catch (Exception testException) {
// Capture screenshot of the new tab before any cleanup
if (newTabPage != null && !newTabPage.isClosed()) {
ITestResult currentTestResult = Reporter.getCurrentTestResult();
if (currentTestResult != null) {
currentTestResult.setStatus(ITestResult.FAILURE);
currentTestResult.setThrowable(testException);
WebTestListeners listeners = new WebTestListeners();
listeners.onTestFailure(currentTestResult);
}
}
throw testException; // Re-throw to maintain original behavior
} finally {
if (newTabPage != null && !newTabPage.isClosed()) {
newTabPage.close(); // Always close the new tab
}
WebTestListeners.setPage(originalPage);
// Revert to the original page and locator
this.basePage = originalPage;
this.locators = originalLocators;
}
}
}
Loading