【问题标题】:Access ng-if value from Cypress从赛普拉斯访问 ng-if 值
【发布时间】:2018-10-15 19:34:08
【问题描述】:

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

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

<section ng-if="!hasKey">

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

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

谢谢!

【问题讨论】:

  • section 元素的存在不会给你这个价值吗?
  • 如果可以的话,我建议你切换到 Angular(非 js)。另外,如果您正在设置测试,我想您会知道该密钥是否存在。我并不是要刻薄,但您的测试用例可能是该部分在那里,请对其进行测试。我正在尝试了解您的测试用例如何在运行之间发生变化,有时关键在那里,有时关键不在。
  • 感谢您的建议@Maccurt,我们计划很快开始切换到 Angular。这里的问题是我想做更多的事情是该部分不存在,因此我不能只做那个部分,因为测试会失败(如果我错了,请纠正我)这就是理查德的回答有效的原因对我来说。

标签: angularjs cypress


【解决方案1】:

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

【讨论】:

  • 谢谢!你的例子和参考帮助我实现了我正在寻找的东西
猜你喜欢
  • 2020-08-15
  • 2019-04-26
  • 1970-01-01
  • 2020-12-23
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多