Using
'page.waitForResponse()' method, we can wait until we receive a matching
response.
Example
page.waitForResponse(response -> {
String url = response.url();
System.out.println("processed url : " + url);
boolean isMatched = url.contains("google.com");
if (isMatched) {
System.out.println("Received matching response");
}
return isMatched;
}, () -> {
page.navigate("https://self-learning-java-tutorial.blogspot.com/");
});
Above snippet wait until we process a response url that contains the string "google.com" in it.
Find the below working application.
WaitForResponse.java
package com.sample.app.miscellaneous;
import java.io.IOException;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
public class WaitForResponse {
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));
Page page = browser.newPage();
page.waitForResponse(response -> {
String url = response.url();
System.out.println("processed url : " + url);
boolean isMatched = url.contains("google.com");
if (isMatched) {
System.out.println("Received matching response");
}
return isMatched;
}, () -> {
page.navigate("https://self-learning-java-tutorial.blogspot.com/");
});
System.out.println("title : " + page.title());
}
}
}
Output
processed url : https://self-learning-java-tutorial.blogspot.com/ processed url : https://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html processed url : https://www.blogger.com/static/v1/widgets/2975350028-css_bundle_v2.css processed url : https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js processed url : https://apis.google.com/js/platform.js Received matching response title : Programming for beginners
Previous Next Home
No comments:
Post a Comment