How to handle multiple tabs using Playwright Java & POM and interact with elements inside the new tab ?
I am using PageBase class and here is the basic content of it
`public class PageBase {
Page page;
BrowserContext context;
public PageBase(Page page) {
this.page = page;
this.context = page.context();
}
public void clickOnElement(Locator elementLocator) {
elementLocator.click();
}
}`
And Also TestBase Class:
`public class TestBase {
public Playwright playwright = Playwright.create();
public Browser browser;
public BrowserContext context;
public Page page;
@BeforeTest
public void startBrowser() {
browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
context = browser.newContext();
// Start tracing before creating / navigating a page.
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true)
.setSnapshots(true)
.setSources(true));
page = context.newPage();
page.navigate("Website URL");
}`
there is a button I click on and it opens a new tab so I want to create a generic method to allow me to move between tabs by giving it the tab index only like I do in selenium for example
public void switchToTab(int tabIndex) { ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles()); if (tabIndex < tabs.size()) { driver.switchTo().window(tabs.get(tabIndex)); } else { throw new IndexOutOfBoundsException("Invalid tab index: " + tabIndex); } }
can anyone tell me how to implement the same function but in playwright ?
I tried to create a generic function to allow me to move between browser tabs but i couldn’t interact with the elements inside the new tab
Yahia Hisham is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.