I have a method for our website's search field.When I type a word into search field, server gives me a response.These responses sometimes can be a log so I have to wait for all XHRs to be loaded not a specific response.Tried waitForLoadState seems not working.Tried below code but it only returned one response not other requests?
async searchTextWithWait(value: string) {
await(await this.page.waitForSelector("//span[contains(#class,'search-input')]/input")).type(value,{delay:80})
await this.page.waitForResponse(response => {
return response.status() == 200
});
}
Maybe this solution can help you:
const selector = "//span[contains(#class,'search-input')]/input";
await this.page.locator(selector).waitFor();
await Promise.all([
this.page.waitForLoadState('networkidle'), // wait until there are no network connections for at least 500 ms.
this.page.locator(selector).type(value,{delay:80}),
]);
Related
andI'm trying to work on a caching solution for inflight requests to Koa,
Let's say that i have 100 seperate users hitting the same endpoint concurrently, but the endpoint takes ~4-5 seconds to return a response.
For example:
GET http://mykoa.application.com/getresults
In my router middleware is it possible to cache all of the concurrent inbound requests and then once the response has been generated return the same result to all of them? Potentially something similar to the example below?
const inflight = {};
router.use(async function(ctx, next) {
// Create a cache 'key'
const hash = `${ctx.request.url}-${ctx.state.user?.data?.id}-${JSON.stringify(ctx.request.body)}`;
// Check if there is already a request inflight
if (inflight[hash]) {
// If there is then wait for the promise resolution
return await inflight[hash];
}
// Cache the request resolution for any other identical requests
inflight[hash] = next();
await inflight[hash];
// Clean it up so that the next request will be fresh
inflight[hash].then(function(res) {
delete inflight[hash];
}, function(err) {
delete inflight[hash];
})
})
In my head this should work, and the expectation would be that all 100 concurrent requests would resolve at the same time (after the first one has resolved) however in my tests each request is still being run separately and taking 4-5 seconds each in sequence.
I requesting the server(written in Node.js) using "fetch-api" function of javascript. I want to make multiple request to the server When the user clicks a button.
Is there such a way in JavaScript ?
When the server responds. Then the second request Send.
And when second response come.
Third request send and so on...
Please help me . Thanks in advance!
the fetch function returns a promise.
you need to await it like so:
await fetch('http://test.com/1');
await fetch('http://test.com/2');
await fetch('http://test.com/3');
you can also access the responses like so:
const res1 = await fetch('http://test.com/1');
const data1 = await res1.json();
if (data1.foo == '123') {
await fetch('http://test.com/2');
await fetch('http://test.com/3');
}
I'm trying to make assertions on XHR, but can't find a right way on how to grab the correct request.
The task is simple: click a button, then wait for network request and make assertions on it's response and request bodies.
The problem is that before I call changePageSize() function in my tests, there are already multiple requests in my network with the exact same URLs and methods. The only difference between them is request and response body, so it happens that my code just grabs the first request that matches url I provided. Is there an any way on how to specify the exact network request that I want to use in my tests?
Here is the function:
static async changePageSize(selector: string): Promise<any> {
const [resp]: any = await Promise.all([
page.waitForResponse(`**${paths.graph}`),
this.setPagination(selector),
]);
return [resp]
}
And then I'm using it in my tests:
const [response] = await myPage.changePageSize(selector);
expect(await response.text()).toContain(`${size}`);
expect(response.request().postData()).toContain(`${size}`);
I have a utility method in my React app for making HTTP requests.
The method implements fetch timeout using AbortController.
What I'd like to get as a return value is a fully resolved Response. Here's my definition:
calling any method on this resolved response object should not result in network packets being sent or received. Plus, the body of this response should not be consumed.
Here's what I'm doing right now (not sure about this):
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
const response = await fetch(url, { signal: controller.signal })
const clone = response.clone();
// a call to initiate body transfer via network
const text = await clone.text();
clearTimeout(timeoutId);
return response;
When I call response.json() afterward, what exactly will happen? Will there be any network communication?
I'm trying to create a chatbot in DialogFlow that checks the status of your insurance claim.
I have set up a call to an external API (mock), and I use a promise to wait for the response and then return it. However, I consistently get [empty response] from DF, despite getting the correct data from the mock API. Is it just taking too long?
Below is the relevant code:
var callClaimsApi = new Promise((resolve, reject)=>{
try{
https.get('https://MOCKAPIURL.COM', (res) => {
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
resolve(JSON.parse(rawData));
});
});} catch(e){reject(e.message);}
});
function checkClaims(agent){
callClaimsApi
.then(function(fulfillment){
console.log("fulfillment name: " + fulfillment.name);
agent.add("It looks like you want to find a claim for " + fulfillment.name);
})
.catch(function(error){console.log(error);});
}
intentMap.set('checkClaims', checkClaims);
here is the output from the logs:
The issue is that, although you're doing all your processing through a Promise, you are not returning that Promise in your Handler. The library needs the Promise so it knows there is an asynchronous operation going on and that it should wait till that operation is completed before sending a reply.
Fortunately, in your case, you may be able to do this by just adding the return statement before callClaimsApi.
You may also wish to look into using a library such as axios to do the http call, since it has promise support built-in.
According to documentation, Dialogflow's wait time is 5 seconds. If you can optimize your code that would be awesome. There are some tricks to make DF wait for longer using Follow-Up events or use one intent to request -> respond to the user with some confirmation (ex. Can you wait for 3 seconds? Yes/No) -> By this time the request will be available so you can send it in the next message.
You can check his post for me info