cyp*_*ull 5 javascript cypress
我想存储一个值,然后执行一个操作并断言该值没有改变。我确实有一个有效的代码,但如果有更优雅的解决方案,我希望得到输入。
基本思想是:
describe('Store and compare a value', () => {
it('store and compare', () => {
cy.login()
cy.visit('url2')
cy.get('.total-count-results').invoke('text')
.then((text) => {
const counts = text
cy.get('.medium.col100 > .filterwrapper > input').type('Test Dummy',{force: true})
cy.get('.medium.col100 > .filterwrapper > input').type('{enter}')
cy.get('.total-count-results').invoke('text')
.then((text) => {
const new_counts = text
expect(new_counts).to.eq(counts)
})
})
})
})
Run Code Online (Sandbox Code Playgroud)
这是我能想到的最好的处理异步性的方法。
我认为不需要别名。这是我在本地测试的解决方案。我将大部分代码保留在 Alapan Das 的答案中,这样更容易比较。对我来说,没有别名看起来更简洁、更容易阅读。
describe('Store and compare a value', () => {
it('store and compare', () => {
cy.login()
cy.visit('url2')
cy.get('.total-count-results').invoke('text')
.then((previousText) => {
cy.get('.medium.col100 > .filterwrapper > input').type('Test Dummy',{force: true})
cy.get('.medium.col100 > .filterwrapper > input').type('{enter}')
cy.get('.total-count-results').invoke('text').should("eq", previousText)
})
})
})
Run Code Online (Sandbox Code Playgroud)
您可以为此使用别名并执行以下操作:
describe('Store and compare a value', () => {
it('store and compare', () => {
cy.login()
cy.visit('url2')
cy.get('.total-count-results').invoke('text').as('counts')
cy.get('.medium.col100 > .filterwrapper > input').type('Test Dummy', {
force: true,
})
cy.get('.medium.col100 > .filterwrapper > input').type('{enter}')
cy.get('.total-count-results').invoke('text').as('new_counts')
cy.get('@counts').then((counts) => {
cy.get('@new_counts').then((new_counts) => {
expect(new_counts).to.eq(counts)
})
})
})
})
Run Code Online (Sandbox Code Playgroud)