Back

Technologies:

javascriptjavascript
avatar
Tolerim
a month ago

Is there a CasperJS equivalent for Playwright's waitForResource method?

Rewritten paragraph: I am wondering if there is a Playwright equivalent to the waitForResource function in CasperJS. Could you guide me on how to convert the following code to Playwright syntax while implementing the same functionality?
casper.waitForResource(function test(resource) {
    return resource.url.indexOf("submit") > -1;
}
Answers(2)
avatar
Vogi
a month ago
Verified Answer
Yes, there is an equivalent function in Playwright called page.waitForRequest(urlOrPredicate[, options]) which waits for a request matching the given URL or predicate function to be issued by the page. The function resolves when the request is issued, or rejects if the request fails to be issued before the timeout. Here's how you can convert your code to Playwright syntax while implementing the same functionality:
await page.waitForRequest(request => request.url().includes('submit'));
I hope this helps! Let me know if you have any other questions.

Sources

Is there any Playwright equivalent for waitForResource in CasperJS?javascript - Casperjs waitForResource - Stack OverflowCasperjs ajax calls - waitForResource and parse errorsCasperJS waitForResource: how to get the resource i've waited for[Question] Best way to write helper functions #10358 - Github
avatar
Tolerim
a month ago
Yes! In Playwright, you can use the method page.waitForRequest(urlOrPredicate[, options]) to wait for a request that matches the given URL substring or predicate function. Here's an example of how you can write the given code in Playwright:
await page.waitForRequest(request => request.url().includes('submit'));
In this example, we have used the page.waitForRequest() method to wait for a request that includes the substring "submit" in its URL. The request object in the callback function represents the intercepted request, and we have used its url() method to get the request URL. Note that you will need to use await before calling page.waitForRequest(), as this method returns a promise.
;