我对 AngularJS 和 Cypress 还很陌生,我一直在寻找解决这个问题的方法,但没有成功。
我有一个带有 ng-if 的部分
<section ng-if="!hasKey">
我想获取这个 hasKey 值来确定是否应该在 Cypress 测试中执行一个额外的步骤来执行以下操作:
if(hasKey) {
cy.someFunc();
}
谢谢!
最佳答案
从元素的存在中推断出 hasKey
值可能就足够了,就像这样
cy.get('section').then(_ => {
cy.someFunc()
})
但如果你想更明确地测试,请参阅此博客 Control an AngularJS Application From E2E Tests to get access to properties on the angularjs scope获取 AngularJs 应用程序的内部属性。
const getAngular = () =>
cy.window()
.its('angular')
const getElementScope = (selector) =>
cy.get(selector)
.then($el =>
getAngular()
.then(ng => ng.element($el).scope())
)
it('has hasKey property in scope', () => {
getElementScope('section')
.its('hasKey').then(hasKey => {
if(hasKey) {
cy.someFunc();
}
})
})
关于angularjs - 从 Cypress 访问 ng-if 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52823597/