【问题标题】:how to use cypress for if..then test flow如何使用 cypress 进行 if..then 测试流程
【发布时间】:2020-01-27 23:10:35
【问题描述】:

我想在 cypress 中编写一些测试来加载页面并检查是否打开了某些模态(不应该超过 5 秒)然后关闭它,否则如果模态没有打开,只需转到 before 块中的测试用例。我该怎么做?到目前为止,我有以下代码,我只能确定模态是否存在然后关闭。

function checkModalThenProceed() {
  cy.get(#check modal is open function).then(($modal) => {
    if ($modal) {
      closedModal();
    }
  })
}
describe('test if-else flow', () => {
  before(()=>{
    checkModalThenProceed();
  })
  it('testflow', () => {
    expect(1).to.eq(1);
  });
})
``

【问题讨论】:

  • 如果你想基于元素或 DOM 有条件地运行测试,你需要使用 jQuery 来选择这些元素并弄清楚你想要什么。如果您使用cy.get(),它实际上是在运行测试。不返回任何内容的cy.get() 算作测试失败。

标签: typescript mocha.js cypress


【解决方案1】:

有点难以猜测#check modal is open function代表什么,但如果它是一个顾名思义的函数,那么你可以直接调用它并在一个简单的@987654323中直接使用结果@ 陈述。

因为这很明显,并且您有一个cy.get(),我假设您正在使用某种选择器。

在这种情况下使用cy.get() 将导致测试失败,只要模态框实际上没有打开。

您可以更改表达式以使用 jquery(在 Cypress 全局上提供为 Cypress.$ref),它允许您测试选择器而不会导致测试失败,请参阅 jquery ref

function checkModalThenProceed() {
  if (Cypress.$('my-modal-selector').length) {  // zero length means not found
    closedModal();
  }
}

【讨论】:

    【解决方案2】:

    我使用这个代码:

    if ($body.find("modal_selector").length > 0) {   
            //evaluates as true if selector exists at all
            //do something
            .....
          }else{
               //if selector does not exist
               }
    

    【讨论】:

      猜你喜欢
      • 2022-08-19
      • 1970-01-01
      • 2022-12-16
      • 2017-02-28
      • 2021-10-07
      • 1970-01-01
      • 2021-03-11
      • 2020-04-13
      • 1970-01-01
      相关资源
      最近更新 更多