简体   繁体   中英

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. 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.

Since that is obvious, and you have a cy.get() , I assume you are using a selector of some kind.

Using cy.get() in that scenario will fail the test whenever the modal is not actually open.

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 .

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
           }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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