首页 > 解决方案 > Cypress 测试依赖于隐藏域

问题描述

我正在尝试仅在不是可选的情况下测试电子邮件地址。我原以为这会起作用,但事实并非如此。

在我的 html 中,我有<input type="hidden" id="Customer_AllowEmailToBeOptional" value="false" >

cy.get('#Customer_AllowEmailToBeOptional').should('have.value', 'false') // passes the test

cy.get('#Customer_AllowEmailToBeOptional').then(($allowEmail) => {
    console.log('text=' + JSON.stringify( $allowEmail)); // text={"0":{},"length":1}               
    if ($allowEmail.text() === 'false') {
        // test email is valid
    }
})

我原以为当 .then() 执行时,我可以访问 .text() 值。我哪里错了?

标签: cypress

解决方案


您将需要使用$allowEmail.val(). 该元素中没有文本内容。


推荐阅读