Monday 24 July 2023

Playwright: switch between tabs

page.bringToFront() method is used to activate a tab (or) brings the page to front.

Example

// Get all the opened pages in the context
List<Page> pages = page.context().pages();
for (Page tempPage : pages) {
	System.out.println("Bringing the page '" + tempPage.title() + "' to the front");
	tempPage.bringToFront();
	tempPage.waitForTimeout(2000);
}

 

Find the below working application.

 

openInNewTab.html
<!DOCTYPE html>
<html>

<head>
	<title>Hello World</title>
</head>

<body>

	<form onsubmit="return false;">

		<table>
			<tr>
				<td>
					<p>Java</p>
				</td>
				<td> <a href="https://self-learning-java-tutorial.blogspot.com/" id="java">Java tutorial</a> </td>
			</tr>

			<tr>
				<td>
					<p>Playwright</p>
				</td>
				<td> <a href="https://playwright.dev/" id="playwright">Playwright tutorial</a> </td>
			</tr>


		</table>

	</form>


</body>

</html>

 

FileUtil.java

package com.sample.app.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class FileUtil {
	
	public static String resourceAsString(String resourceName) throws IOException {
		ClassLoader classLoader = FileUtil.class.getClassLoader();
		URL url = classLoader.getResource(resourceName);
		if (url == null) {
			return null;
		}

		URLConnection urlConnection = url.openConnection();

		urlConnection.setUseCaches(false);

		try (InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream())) {
			char[] buffer = new char[1048];
			StringBuilder builder = new StringBuilder();

			int count = -1;
			while ((count = inputStreamReader.read(buffer, 0, buffer.length)) != -1) {
				builder.append(buffer, 0, count);
			}

			return builder.toString();
		}

	}
}

SwitchBetweenTabs.java

package com.sample.app.miscellaneous;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Locator.ClickOptions;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.KeyboardModifier;
import com.sample.app.util.FileUtil;

public class SwitchBetweenTabs {
	public static void main(String[] args) throws IOException {
		try (Playwright playwright = Playwright.create()) {
			Browser browser = playwright.chromium()
					.launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
			final String content = FileUtil.resourceAsString("miscellaneous" + File.separator + "openInNewTab.html");

			Page page = browser.newPage();
			page.setContent(content);

			ClickOptions clickOptions = new ClickOptions();

			List<KeyboardModifier> modifiers = Arrays.asList(KeyboardModifier.META);
			clickOptions.setModifiers(modifiers);
			page.locator("a#java").click(clickOptions);
			page.locator("a#playwright").click(clickOptions);

			/* wait for sometime, so all the pages will be loaded */
			page.waitForTimeout(8000);

			// Get all the opened pages in the context
			List<Page> pages = page.context().pages();
			for (Page tempPage : pages) {
				System.out.println("Bringing the page '" + tempPage.title() + "' to the front");
				tempPage.bringToFront();
				tempPage.waitForTimeout(2000);
			}

		}
	}
}


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment