Warn when defining alias in before hook - as they are not reusable in following tests.
See original GitHub issueSince this is my first Issue here, I want to use this opportunity to thank you guys for your hard work. I love and enjoy working with cypress
so far and I’m really looking forward to upcoming releases.
Current behaviour:
It seems like, that a defined alias can only be used once under certain circumstances. The test code will be self explanatory about my approach.
Desired behaviour:
The defined alias should be reusable as expected. Since we are doing a lot of end to end testing, we are trying hard to achieve the best possible performance. As far as I know, requery-ing the dom with cy.get()
is in general a bit expensive, so we try to avoid that.
Making aliases reusable as much as possible, would also result in prettier, slimmer and easier to manage test code.
Test code:
describe('reusing alias', () => {
before(() => {
cy.visit('/my-page.html');
});
// ...
describe('number input', () => {
before(() => {
cy.get('#my-input').as('input');
});
it('should be of type number', () => {
cy.get('@input').should('have.attr', 'type', 'number');
});
it('should allow a minimum value of 1', () => {
cy.get('@input').should('have.attr', 'min', '1');
});
it('should allow a maximum value of 3', () => {
cy.get('@input').should('have.attr', 'max', '3');
});
});
});
Additional Info (images, stack traces, etc)
The given Example will lead to the following behaviour:
- The first
it()
will succeed - The second and third
it()
will fail - Changing the
before
in describe number imput into abeforeEach
will solve the problem. But not really, I don’t want to re-query the dom, I just want to re-use the defined alias.
Maybe I am getting something wrong here. If so, feel free to correct my way of thinking here.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:29
- Comments:37 (7 by maintainers)
Clearing aliases between each test is surprisingly inflexible for no good reason. I do much data setup in before all hook and save ids of generated objects in aliases. Doing the same setup for every tests will add 10s of seconds to each test. If your remedy is to add all related tests into one single test then you end up with ugly, long test that do too many things and hard to maintain.
Even after all hook forget all the previously set aliases. Then why have before all/after all hook at all? This is just asking for developers to circumvent the alias issue by using regular variable instead.
Ach, I would love to prepare setup for my tests once in
before
hook. Would you consider to enable aliases? I think many users would appreciate it 😦