Skip to content

Commit 4157860

Browse files
Charankumar HCharankumar H
authored andcommitted
Implemented method to switch to a new tab and executed test cases with capturing screenshots.
1 parent a40d1f1 commit 4157860

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/main/java/io/swaglabs/portal/qa/commons/WebBasePage.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import com.microsoft.playwright.Page;
55
import io.swaglabs.portal.qa.constants.WebPortalConstants;
66
import io.swaglabs.portal.qa.exceptions.WebPageException;
7+
import io.swaglabs.portal.qa.listeners.WebTestListeners;
78
import io.swaglabs.portal.qa.locators.Locators;
89
import io.swaglabs.portal.qa.screenshotsmanager.ElementScreenshotStrategy;
910
import io.swaglabs.portal.qa.screenshotsmanager.ScreenshotContext;
11+
import org.testng.ITestResult;
12+
import org.testng.Reporter;
1013

1114
import java.util.Objects;
15+
import java.util.function.Consumer;
1216

1317
public abstract class WebBasePage {
1418

@@ -58,4 +62,36 @@ protected void takeElementScreenshot(Locator locator, String fileName) {
5862
ScreenshotContext screenshotContext = new ScreenshotContext(new ElementScreenshotStrategy(locator));
5963
screenshotContext.captureScreenshot(basePage, ELEMENT_SCREENSHOT_FILE_LOCATION + fileName + WebPortalConstants.IMAGE_FORMAT);
6064
}
65+
66+
protected void switchToNewTabAndRun(Page newTabPage, Consumer<WebBasePage> actionsInNewTab) {
67+
Page originalPage = this.basePage;
68+
Locators originalLocators = this.locators;
69+
try {
70+
// Update to new tab context
71+
this.basePage = newTabPage;
72+
this.locators = new Locators(newTabPage);
73+
WebTestListeners.setPage(newTabPage);
74+
actionsInNewTab.accept(this); // Perform operations in new tab
75+
} catch (Exception testException) {
76+
// Capture screenshot of the new tab before any cleanup
77+
if (newTabPage != null && !newTabPage.isClosed()) {
78+
ITestResult currentTestResult = Reporter.getCurrentTestResult();
79+
if (currentTestResult != null) {
80+
currentTestResult.setStatus(ITestResult.FAILURE);
81+
currentTestResult.setThrowable(testException);
82+
WebTestListeners listeners = new WebTestListeners();
83+
listeners.onTestFailure(currentTestResult);
84+
}
85+
}
86+
throw testException; // Re-throw to maintain original behavior
87+
} finally {
88+
if (newTabPage != null && !newTabPage.isClosed()) {
89+
newTabPage.close(); // Always close the new tab
90+
}
91+
WebTestListeners.setPage(originalPage);
92+
// Revert to the original page and locator
93+
this.basePage = originalPage;
94+
this.locators = originalLocators;
95+
}
96+
}
6197
}

src/main/java/io/swaglabs/portal/qa/utils/PerformanceUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static void evaluateDomContentLoadTime(Page page, String methodName) {
4646
log.info("DOM Content Loaded Time for the method: {} is: {} ms.", methodName, domContentLoadTime);
4747
}
4848

49+
@SuppressWarnings("unchecked")
4950
private static Map<String, Object> getPerformanceMetrics(Page page) {
5051
Objects.requireNonNull(page, "Page cannot be null!");
5152
return (Map<String, Object>) page.evaluate(PERFORMANCE_JS_SCRIPT);

0 commit comments

Comments
 (0)