https://cypress.io logo
Issue with Send me a push duo factor push notifica...
# i-need-help
b
I am trying to automate the DUO factor authorisation. Step 1: Visit the login url - enter username and password then click sign in. - Pass Step 2: After the successful login, it redirects to the duo factor push notification screen where the cross domain duosecurity iframe will load with two buttons - 'Send me a push' , 'Enter the code'. - I am stuck with this step - Not able to get the iframe and it's child elements. If I try to get the iframe in cypress run view the iframe shows only source url. it is not loading the iframe dom. Can someone please help me on this ?
h
Copy code
describe('Duo Factor Authorization', () => {
  it('should log in and complete Duo authorization', () => {
    // Visit the login page and enter username and password
    cy.visit('https://example.com/login')
    cy.get('#username').type('myusername')
    cy.get('#password').type('mypassword')

    // Click the Sign In button
    cy.get('button[type="submit"]').click()

    // Wait for the Duo iframe to load
    cy.get('iframe[name="duo_iframe"]')
      .should('be.visible')
      .its('0.contentDocument.body').should('not.be.empty')
      .then(cy.wrap)
      .find('#duo_iframe')
      .its('0.contentDocument.body').should('not.be.empty')

    // Switch to the Duo iframe and click the "Send Me a Push" button
    cy.get('iframe[name="duo_iframe"]')
      .its('0.contentDocument.body').should('not.be.empty')
      .then(cy.wrap)
      .find('#duo_iframe')
      .its('0.contentDocument.body').should('not.be.empty')
      .then((body) => {
        const duoIframe = body.find('#duo_iframe')
        return cy.wrap(duoIframe.contents().find('button[value="Send Me a Push"]')).click()
      })

    // Switch back to the parent window and confirm Duo authorization
    cy.get('body').then((body) => {
      return cy.wrap(body).find('.duo-iframe-container iframe').then((duoIframe) => {
        return cy.wrap(duoIframe.contents().find('.auth-status[data-reactid$="-status"]')).should('contain', 'Approve the request sent to your device')
      })
    })

    cy.get('body').then((body) => {
      return cy.wrap(body).find('.duo-iframe-container iframe').then((duoIframe) => {
        return cy.wrap(duoIframe.contents().find('.auth-button[data-reactid$="-accept"]')).click()
      })
    })

    // Complete the rest of the login process
    // ...
  })
})
In this example, we're first waiting for the Duo iframe to load using cy.get() and the appropriate selector. We're then using cy.wrap() to wrap the iframe contents so that we can interact with its child elements.
Next, we're using cy.find() and cy.contents() to navigate to the inner iframe inside the Duo iframe, and then clicking the "Send Me a Push" button using cy.click(). After that, we're switching back to the parent window and confirming the Duo authorization by checking the status and clicking the "Approve" button. Note that we're using should('not.be.empty') to make sure that the iframe contents have loaded before we try to interact with them. We're also using cy.wrap() to make sure that we can chain commands and assertions onto the iframe contents. This code should dynamically wait for the Duo iframe to load, even if it takes some time to appear.
b
Awesome. Thanks @hundreds-window-7679!
2 Views