-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question : How can we wait using this service until all/particular network request is completed ? #149
Comments
this would be tricky.If you compare with other js tool you can achieve this very easily by waiting for the http call implicitly.
PS: This is how i have achieved it.if you have any better solution ,please share |
Hi @splurgeop Thanks for getting back to me but the biggest challenge which I see at the moment when we use browser.requests(); method it itself don't capture the intended Api request sometimes for e.g. I am trying to intercept a request /v1/getCustomer/ but when we use this method browser.requests() sometimes it capture this request and sometimes not |
that's what the problem is.You have to wait explicitly.since you mentioned that sometimes it captures and sometimes not so you have to wait explicitly for the browser.getrequests till you get the api that you are looking for. |
See #111 for a previous request for this functionality. |
@krarpitgupta @splurgeop You should be able to write a simple utility function in your test library that makes use of the functionality in #186 , which is available beginning with version 4.2.0. async function doWhenSettled(callbackFn) {
while(await browser.hasPendingRequests()) {
await browser.pause(50);
}
// execute the callback
return callbackFn();
}
// example
doWhenSettled(() => console.log('network activity has subsided for the moment!'); In a future release this library may provide a direct option to e.g. |
Hi @tehhowch Thanks for getting back to me, to tackle this problem I tried below code but seems like
Any better way to tackle it or Am I missing something here ? P.S. I also tried by adding following statement |
Idk, maybe this will help someone: export const waitForRequest = async (browser, match) =>
new Promise((resolve) => {
let timeout = 0;
const step = 500;
const interval = setInterval(async () => {
if (timeout <= 20000) {
const requests = await browser.getRequests({ includePending: true });
const hasRequest = requests.find((r) => r.url.includes(match));
if (hasRequest) {
clearInterval(interval);
resolve(true);
} else {
timeout += step;
}
} else {
resolve(false);
clearInterval(interval);
}
}, step);
}); And usage is: const requestFound = await waitForRequest(browser, 'api.com/user'); // true or false This will wait for particular request or until it reaches timeout. You can modify it if you want to wait for multiple requests or maybe use regex instead of strings. |
Scenario:
Any help would be really appreciated, TIA .
The text was updated successfully, but these errors were encountered: