-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Dynamic alias for cy.intercept() #16321
Comments
in that example: let alias = '';
cy.intercept('POST', '/graphql', req => {
const randomString = generateRandom();
alias = `${req.operationName}${randomString}`
req.alias = alias;
});
cy.wait(`@${alias}`); |
@jennifer-shehane I checked that issue, but it is not related to my problem because I have to use dynamic alias out of @Renato66 It was typo so I updated my description. Also, it doesn't work for your suggestion too. Anyway thanks. |
still missing body: |
@Renato66 Thanks. I fixed typo |
@baeharam this is a JS problem, not a Cy problem. In JS, only objects are passed by reference, primitives like strings are passed by value. So in your example, you're actually doing This will work because putting the .wait in the .then uses the updated value of let alias = '';
cy.intercept('POST', '/graphql', req => {
const randomString = generateRandom();
alias = `${req.body.operationName}${randomString}`
req.alias = alias;
})
.then(() => {
cy.wait(`@${alias}`);
}) |
@flotwig Thanks for your suggestion, but I already tried your code. It didn't work and result was |
@baeharam the problem here is that by the time it('creates random alias', () => {
let alias = ''
cy.intercept('GET', '/todos', (req) => {
alias = 'get-todos-' + Cypress._.random(1e6)
req.alias = alias
})
cy.visit('/?delay=2400')
// first, wait for the alias string to become define
cy.wrap('the alias string')
.should(() => {
expect(alias, 'alias string').to.not.be.empty
})
.then(() => {
cy.wait(`@${alias}`)
})
}) First, we retry using alias.mp4 |
@bahmutov Your solution nearly works! Thanks! My solution from your suggestion. it('creates random alias', () => {
const aliasList = [];
cy.intercept('GET', '/todos', (req) => {
const alias = 'get-todos-' + Cypress._.random(1e6)
req.alias = alias
aliasList.push(alias);
})
cy.visit('/?delay=2400')
// first, wait for the alias string to become define
cy.wrap('the alias string')
.should(() => {
expect(alias, 'alias string').have.length(??)
})
.then(() => {
aliasList.forEach(alias => cy.wait(`@${alias}`));
})
}) But, I have this conditions in my situation. (1) There are multiple graphql request |
So at this point I would ask myself - do you know what to expect? Maybe you want to check if the number of requests is > 10 and write that as an assertion? Maybe use something like network idle by looking at the timestamp of the last intercept? I have such example at https://glebbahmutov.com/blog/cypress-tips-and-tricks/#wait-for-network-idle |
@bahmutov Great solution! Thanks! |
For someone to solve this problem: see #16321 (comment) |
What would you like?
By #3083, I have to distinguish each graphql request because, cypress groups requests which has same endpoint very often. To handle independently each request, we can use
operationName
for alias like below.However, if same request is sent multiple time with different variables, it is grouped again. Therefore, I cannot distinguish these queries. To do this, I thought random string will work as a suffix to operationName like below.
From above, each request differentiated even if it is same request. But, how can I wait? how can I store that dynamic alias? I mean this
${req.operationName}${randomString}
alias. When I store this alias to outer variable, I cannot use that variable withwait
syntax.Is there other way?
Below solution is not working
Why is this needed?
I have to wait all graphql requests when I load or visit specific page.
Thanks for your reading.
The text was updated successfully, but these errors were encountered: