Cypress - run test in iframe

后端 未结 3 1915
日久生厌
日久生厌 2021-01-22 03:49

I\'m trying to find elements in iframe but it doesn\'t work.

Is there anyone who have some system to run tests with Cypress in iframe? Some way to get in iframe and work

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-22 04:38

    This works for me locally and via CI. Credit: Gleb Bahmutov iframes blog post

     export const getIframeBody = (locator) => {
      // get the iframe > document > body
      // and retry until the body element is not empty
      return cy
        .get(locator)
        .its('0.contentDocument.body').should('not.be.empty')
        // wraps "body" DOM element to allow
        // chaining more Cypress commands, like ".find(...)"
        // https://on.cypress.io/wrap
        .then(cy.wrap)
    }
    

    spec file:

        let iframeStripe = 'iframe[name="stripe_checkout_app"]'
      getIframeBody(iframeStripe).find('button[type="submit"] .Button-content > span').should('have.text', `Buy me`)
    

提交回复
热议问题