简体   繁体   English

如何使用cypress进行if..then测试流程

[英]how to use cypress for if..then test flow

I want to write some test in cypress to load page and check if some modal is opened (that should not take more than 5 seconds) then close it, otherwise if modal does not opened, just go to testcase in before block.我想在 cypress 中编写一些测试来加载页面并检查是否打开了某个模态(不应超过 5 秒)然后关闭它,否则如果模态没有打开,只需转到块之前的测试用例。 How can I do it?我该怎么做? so far I have following code, and I can only make sure if modal exist then it close.到目前为止,我有以下代码,我只能确定模态是否存在然后关闭。

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

It's a bit hard to guess what #check modal is open function represents, but if it's a function as the name implies then you can just call it and use the result directly in a simple if() statement.有点难以猜测#check modal is open function代表什么,但如果它是一个顾名思义的函数,那么您可以直接在简单的if()语句中调用它并使用结果。

Since that is obvious, and you have a cy.get() , I assume you are using a selector of some kind.由于这很明显,并且您有一个cy.get() ,因此我假设您正在使用某种选择器。

Using cy.get() in that scenario will fail the test whenever the modal is not actually open.每当模态实际上未打开时,在该场景中使用cy.get()都会使测试失败。

You can change the expression to use jquery (provided on the Cypress global as Cypress.$ , ref ), which allows you to test the selector without causing the test to fail, see jquery ref .您可以更改表达式以使用 jquery(在 Cypress 全局中提供为Cypress.$ , ref ),这允许您测试选择器而不会导致测试失败,请参阅jquery ref

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

I use this code:我使用这个代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM