从赛普拉斯访问ng-if值

时间:2018-10-15 19:34:09

标签: angularjs cypress

我对AngularJS和Cypress还是陌生的,我一直在寻找解决问题的方法,但没有成功。

我有一个包含ng-if

的部分
<section ng-if="!hasKey">

并且我想获取此hasKey值来确定是否应在赛普拉斯测试中执行一个额外的步骤来执行以下操作:

if(hasKey) {        
    cy.someFunc();
}

谢谢!

1 个答案:

答案 0 :(得分:0)

从元素的存在推断出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();
      }
    })
})
相关问题