Monday 17 April 2023

Playwright: evaluate: Run custom javascript code

Using ‘page.evaluate’ method, we can evaluate an expression.

 

Signature

Object evaluate(String expression)
Object evaluate(String expression, Object arg)
Object evaluate(String expression, Object arg, EvaluateOptions options)
Object evaluateAll(String expression)
Object evaluateAll(String expression, Object arg)
JSHandle evaluateHandle(String expression, Object arg)
JSHandle evaluateHandle(String expression)
JSHandle evaluateHandle(String expression, Object arg, EvaluateHandleOptions options)

 

Example

page.evaluate("someJavaScriptSnippet");

 

Find the below working application.

 

runJs.html
<!DOCTYPE html>
<html>

<head> </head>

<body>

	<p id="defaultTitle">Hello World</p>
	
</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();
		}

	}
}

 

RunJSFunction.java

package com.sample.app.miscellaneous;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.sample.app.util.FileUtil;

public class RunJSFunction {

	public static void main(String[] args) throws IOException, InterruptedException {
		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 + "runJs.html");

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

			String title = page.locator("#defaultTitle").textContent();
			System.out.println("title : " + title);

			TimeUnit.SECONDS.sleep(2);

			page.evaluate("document.getElementById('defaultTitle').innerHTML = 'Changed the title';");

			title = page.locator("#defaultTitle").textContent();
			System.out.println("title : " + title);

			TimeUnit.SECONDS.sleep(2);

		}
	}

}

 

Output

title : Hello World
title : Changed the title

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment