javascript - Cypress 最佳实践 - 存储和比较两个值

我想存储一个值,然后执行一个操作并断言该值没有改变。我确实有一个有效的代码,但如果有更优雅的解决方案,我想提供意见。

基本思路是:

  • 获取显示的数字或结果('counts'),将其存储在 .then() 函数中
  • 改变用途
  • 获取显示的结果数('new_counts'),将其存储在新的 .then 函数中
  • 在第二个 .then() 函数中比较计数和 new_counts
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)
                    })
            })
    })
})

这是我能想到的处理异步性的最佳方法。

最佳答案

我认为不需要别名。这是我在本地测试的解决方案。我将大部分代码保留在 Alapan Das 的 answer 中所以更容易比较。对我来说,没有别名似乎更简洁、更容易阅读。

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)
            })
    })
})

关于javascript - Cypress 最佳实践 - 存储和比较两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69359211/

相关文章:

python - 如何旋转图像以获得非空像素?

loops - 如何在 Kotlin 循环内更改 for 循环的计数器变量值?

reactjs - 如何实现react-dnd useDragLayer?

android - 如何从 Webview 中访问摄像头?

c++ - 使用 std::memcpy 复制包含 boost::any 数据成员的对象

ios - 更改 iOS 15 中导航栏的当前颜色

reactjs - 如何去除 Material-UI 对话框中的阴影?

c - 尝试将二维字符串数组传递给函数并打印它

sql - 按可互换使用的值对分组

c - 打印 int_fast_32_t 等数据类型的可移植和正确方法是什么