从赛普拉斯访问 ng-if 值

Access ng-if value from Cypress

我是 AngularJS 和 Cypress 的新手,我一直在寻找解决这个问题的方法,但没有成功。

我有一个带有 ng-if 的部分

<section ng-if="!hasKey">

我想获得这个 hasKey 值以确定我是否应该在赛普拉斯测试中执行一个额外的步骤来做这样的事情:

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