Mastering Advanced Cypress.io Test Automation [Part 1/4]: Dealing with Iframes
Tags: testing, javascript, cypress, e2e
Published: 2023-05-12 01:13:06

Welcome to the first instalment of our series on mastering advanced Cypress.io test automation! In this series, we’ll be diving deep into some of the most complex concepts and techniques for writing efficient and effective automated tests with Cypress.io.

In this series of articles, we will delve into complex concepts such as handling [_iframes_](https://dev.to/ahsanbilal/mastering-advanced-cypressio-test-automation-part-14-dealing-with-iframes-422m), [_network stubbing_](https://dev.to/ahsanbilal/mastering-advanced-cypressio-test-automation-part-24-network-stubbing-4mgg), and [_resolving promises_](https://dev.to/ahsanbilal/mastering-advanced-cypressio-test-automation-part-34-promise-resolving-4p3b) with Cypress.io. Additionally, we will provide you with valuable insights on how to create [_custom functions_](https://dev.to/ahsanbilal/mastering-advanced-cypressio-test-automation-part-44-writing-custom-functions-5bkp) to address the unique testing challenges that Cypress.io does not cover directly. So, fasten your seatbelts and get ready to elevate your Cypress.io proficiency!

In this first part, we’ll be focusing on one of the most challenging aspects of test automation: dealing with iframes. As you may already know, iframes can present unique challenges for automated testing, since they essentially embed one website inside another. We’ll explore some strategies and techniques for handling iframes in your Cypress.io tests, including how to locate and interact with elements inside iframes, how to assert on iframe content, and how to handle cross-domain iframes.

By the end of this part, you’ll have a solid understanding of how to tackle iframes in your Cypress.io test suite, and you’ll be well on your way to mastering advanced Cypress.io test automation. So, let’s get started!

## Dealing with Content within Iframes
When testing web applications, it’s not uncommon to come across iframes, which are HTML elements used to embed another document within the current HTML document. Testing content within iframes can be a bit more complicated than testing regular HTML elements, but Cypress.io provides us with the tools we need to tackle this challenge.

## Real-World Example
Suppose you are testing a web page that has a chatbot integrated into an iframe. To test the chatbot functionality, you need to interact with the iframe’s elements. However, because the iframe’s elements are technically part of a separate document, you cannot access them directly from the parent document using the Cypress commands. Instead, you need to switch the context of your test to the iframe document in order to interact with its elements. You can do this by using the `cy.get()` command to select the iframe element based on its attributes, using the `.then()` method to access the contents of the iframe using the `$iframe.contents()` method, and finally, using the `cy.wrap()` command to wrap the form element and save it to an alias `@form` that you can reference later in your test.

## Understanding the Challenge
One of the challenges of testing content within iframes is that the elements within the iframe are technically part of a separate document. This means that we cannot access them directly from the parent document using the Cypress commands. Instead, we need to switch the context of our test to the iframe document in order to interact with its elements.

## The Code Example
To illustrate how to deal with content within iframes in Cypress.io, let’s use the example of a web page that contains an iframe with a form element. Our goal is to fill out the form and submit it.

First, we need to identify the iframe element using the `cy.get()` command and save it to a variable. We'll use the `iframe()` command to get the content of the iframe:

```
cy.get('iframe[name="my-iframe"]').then(($iframe) => {
  const iframeContents = $iframe.contents()
  cy.wrap(iframeContents.find('form')).as('form')
})
```

Here, we’re using the `cy.get()` command to select the iframe element based on its name attribute. We then use the `.then()` method to access the contents of the iframe using the `$iframe.contents()` method, which returns the document object of the iframe.

Next, we use the `cy.wrap()` command to wrap the form element and save it to an alias `@form` that we can reference later in our test.

Now that we have access to the iframe document and the form element, we can interact with the form fields and submit the form as we would with any other form element. For example, let’s fill out the form and submit it:

```
cy.get('@form').find('[name="name"]').type('FirstName LastName')
cy.get('@form').find('[name="email"]').type('firstname.lastname@example.com')
cy.get('@form').find('button[type="submit"]').click()
```

Here, we’re using the `cy.get('@form')` command to reference the form element that we saved to the `@form` alias earlier. We then use the `.find()` method to select the form fields by their name attributes and fill them out using the `.type()` command. Finally, we use the `.click()` command to submit the form.

---
Enough for now.

In subsequent parts of this article, I’ll talk about [Network Interception](https://dev.to/ahsanbilal/mastering-advanced-cypressio-test-automation-part-24-network-stubbing-4mgg), [Promise Resolving](https://dev.to/ahsanbilal/mastering-advanced-cypressio-test-automation-part-34-promise-resolving-4p3b), and [Writing Custom Functions](https://dev.to/ahsanbilal/mastering-advanced-cypressio-test-automation-part-44-writing-custom-functions-5bkp).

Up Next: [Part 2](https://dev.to/ahsanbilal/mastering-advanced-cypressio-test-automation-part-24-network-stubbing-4mgg), [Part 3](https://dev.to/ahsanbilal/mastering-advanced-cypressio-test-automation-part-34-promise-resolving-4p3b), [Part 4](https://dev.to/ahsanbilal/mastering-advanced-cypressio-test-automation-part-44-writing-custom-functions-5bkp)

_If you liked this, click ❤ so other people will also notice here._