How to check if element exists using Cypress.io

后端 未结 4 522
不知归路
不知归路 2020-12-01 16:00

How to check if element is present or not, so that certain steps can be performed if element is present. Else certain different steps can be performed if element is not pres

4条回答
  •  不思量自难忘°
    2020-12-01 16:57

    cypress all steps are async ,, so that you should make common function in commands file or page object file,,..

        export function checkIfEleExists(ele){
        return new Promise((resolve,reject)=>{
            /// here if  ele exists or not
            cy.get('body').find( ele ).its('length').then(res=>{
                if(res > 0){
                    //// do task that you want to perform
                    cy.get(ele).select('100').wait(2000);
                    resolve();
                }else{
                    reject();
                }
            });
        })
    }
    
    
    // here check if select[aria-label="rows per page"] exists
    cy.checkIfEleExists('select[aria-label="rows per page"]')
    .then(e=>{
            //// now do what if that element is in ,,..
            })
    .catch(e=>{
        ////// if not exists...
        })
    

提交回复
热议问题