Cypress test: is .contains() equivalent to should('contain')?

前端 未结 2 1998
夕颜
夕颜 2021-02-13 12:44

Is this: cy.get(\'[name=planSelect]\').contains(dummyPlan)

equivalent to this: cy.get(\'[name=planSelect]\').should(\'contain\', dummyPlan)

2条回答
  •  攒了一身酷
    2021-02-13 13:29

    Answer

    • .contains(selector, content) is the best selector; it retries element selection AND allows text matching (not just .class #id [attributes])

    • .should() is just an assertion and only the assertion is retried (not the element selection)

    .should('exist') is implied unless you specify your own -- this is how they allowed .should('not.exist')


    Tangent

    Browsers support XPath 1.0 which is a pretty cool but obscure way to make complex queries based on DOM tree traversal. There's a contains predicate function:

    //*[         contains(normalize-space(.), 'The quick brown fox jumped over the lazy dog.') ]
       [not(.//*[contains(normalize-space(.), 'The quick brown fox jumped over the lazy dog.') ])]
    

    This searches from root of the document for any node that contains the text and doesn't contain a descendant node which contains the text. You can test it in the console:

    $x("//*[contains(normalize-space(.), 'The quick brown fox jumped over the lazy dog.')][not(.//*[contains(normalize-space(.), 'The quick brown fox jumped over the lazy dog.') ])]")
    

提交回复
热议问题