How Does Cypress Locate Element In Iframe?

2 minutes read

Cypress uses the cy.iframe() command to locate elements within an iframe. This command allows Cypress to switch its focus to the desired iframe, then use regular Cypress commands like cy.get() to locate elements within the iframe. Cypress does not treat elements within iframes any differently than elements outside of iframes; once the iframe is selected, Cypress can interact with elements inside it just like it would with any other element on the page.


How to work with iframes in Cypress tests using custom commands?

To work with iframes in Cypress tests using custom commands, you can create a custom command to interact with the iframes. Here's an example of how you can do this:

  1. Create a new Cypress command in your support/commands.js file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Cypress.Commands.add('iframe', { prevSubject: 'element' }, ($iframe) => {
  return new Cypress.Promise(resolve => {
    const onIframeReady = () => {
      const contentWindow = $iframe.prop('contentWindow')
      resolve(contentWindow)
    }
    $iframe.on('load', onIframeReady)
    if ($iframe.prop('complete')) {
      onIframeReady()
    }
  })
})


  1. In your test file, use the custom command to interact with the iframe. For example, you can switch to the iframe, perform some actions inside the iframe, and then switch back to the main window:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
describe('iframe test', () => {
  it('should interact with iframe', () => {
    cy.visit('https://example.com')
    
    cy.get('iframe').iframe().then(iframe => {
      const body = iframe.document.querySelector('body')
      cy.wrap(body).find('input').type('Hello, World!')
    })
    
    // switch back to the main window
    cy.get('body').type('{esc}')
  })
})


With this custom command, you can easily interact with iframes in your Cypress tests without having to manually handle switching to and from the iframes each time.


How to switch back to the main window after interacting with an iframe using Cypress?

You can switch back to the main window after interacting with an iframe in Cypress using the cy.switchTo() command. Here's an example:

1
2
3
4
5
6
7
8
// interacting with the iframe
cy.get('iframe').then(($iframe) => {
  const $body = $iframe.contents().find('body');
  cy.wrap($body).find('.element-in-iframe').click();
});

// switching back to the main window
cy.switchTo('window');


In this example, we first interact with the elements inside the iframe using the standard Cypress commands. Then, we use the cy.switchTo('window') command to switch back to the main window. This command is specifically designed to switch between frames and windows in Cypress.


What is the purpose of using iframes on a webpage?

The purpose of using iframes on a webpage is to embed or display content from another source within the primary webpage. This allows for the integration of external content, such as videos, maps, or social media feeds, without redirecting the user to a different website. iframes are commonly used to streamline the user experience and enhance the functionality of a webpage.

Facebook Twitter LinkedIn Telegram Whatsapp

Related Posts:

To pass an array to an iframe element in Vue.js, you can set the source of the iframe using a computed property that converts the array into a query string. This can be achieved by using the v-bind directive to bind the src attribute of the iframe to the compu...
When a browser blocks an iframe, it usually means that the content within the iframe has been deemed insecure or harmful by the browser's security settings. There are a few ways to detect when this happens. One way is to listen for errors in the console lo...
The allow attribute is used in the <iframe> element to specify a set of restrictions that should be applied to the embedded content within the iframe. This attribute allows developers to control certain features of the embedded content such as allowing o...
You can change the opacity of an element with hover by applying a CSS rule using the :hover pseudo-class selector. By targeting the element with the hover selector, you can adjust the opacity property to your desired level when the element is hovered over by...
In Elixir, a double loop can be implemented using nested Enum.each functions or using recursion.Here is an example of implementing a double loop using nested Enum.each functions: list1 = [1, 2, 3] list2 = [:a, :b, :c] Enum.each(list1, fn x -> Enum.each(l...