In this example on the website, they say there is a simple way to use data you create in before() or beforeEach() in a testcase it(). But when I tried it it said my this.variable was undefined. After a little more searching I found that all aliases are cleared before each test, making the example on the website not possible.
Example on the website under Sharing context:
describe('parent', () => {
beforeEach(() => {
cy.wrap('one').as('a')
})
context('child', () => {
beforeEach(() => {
cy.wrap('two').as('b')
})
describe('grandchild', () => {
beforeEach(() => {
cy.wrap('three').as('c')
})
it('can access all aliases as properties', function () {
expect(this.a).to.eq('one') // true
expect(this.b).to.eq('two') // true
expect(this.c).to.eq('three') // true
})
})
})
})
Briefly what I’m trying to do (I thought the way I get my data irrelevant to the question):
describe('myTests', () => {
beforeEach(() => {
// producing data resulting in a jquery list element
cy.wrap($element).as('element')
})
it('firstTest', function () {
this.element.click()
})
}
This results on a TypeError (void 0) is undefined
with an error to the beginning of this
edit:
So what I’m doing is getting a list element from a website. The second visible element in the main menu.
describe('Click test', () => {
beforeEach(() => {
let counter = 0
cy.visit('https://www.qalybr.nl')
cy.get('ul[class~=menu] li').each(($el) => {
if($el.is(':visible')) {
if(counter === 1) {
cy.wrap($el).as('werkenBij')
}
counter++;
}
})
})
it('Werken', () => {
this.werkenBij.click()
})
})
It wraps and aliases the element correctly, the cypress runner shows this, but then isn’t able to find this.
2
Answers
It does actually depend on the way you are obtaining
$element
. If you follow the pattern in the documents you quoted, there is no problem.This passes
and of course this passes
Cypress tells you (on the linked page) that you should not use the return value from a query, because it may go stale.
So don’t do this
Always chain the alias instead, even if you are working with
$element
inside athen()
The full example in the edit section is different to the example given above it.
To use the
this.werkenBij
form of alias you must have afunction()
based callback, not a() => {}
arrow-function based callback.