Skip to main content

How to Work with Frames

The Problem

In this tip, you'll learn how to work with frames and write tests against them.

On occasion, we may encounter relics of the front-end world, such as frames. When writing tests against them, it's easy to make a mistake if we're not paying attention.

A Solution

Rather than getting frustrated when authoring your tests, you can work with the elements in a frame switching to that frame first. Then the rest of your test should be more straightforward.

Let's explore further with some examples.

Examples

Expected Behavior

Example 1

  • Open the browser
  • Visit the page
  • Switch to the nested frame
  • Grab the text from the frame and assert that Selenium is in the correct place
  • Close the browser

Example 2

  • Open the browser
  • Visit the page
  • Switch to the frame that contains the TinyMCE editor
  • Grab and clear the text in the editor
  • Input and grab new text in the editor
  • Assert that the original and new text entries don't match
  • Switch to the top level of the page
  • Grab the text from the top of the page and assert that it's not empty
  • Close the browser

Outro

Now you're ready to handily defeat frames when they cross your path.

Happy Testing!

Special Thanks

Mike Millgate
Contributed the Python code for this tip.
Isaul Vargas
Python code review
Peter Bittner
Python code review
Jonathan Taylor
Contributed the initial C# code for this tip.
Roman Isko
Contributed the initial Java code for this tip.
Jan Molak
Contributed the Serenity/JS code for this tip.
Andrei Solntsev
Contributed the Selenide code for this tip.

Authors

Dave Piacente
Original creator of Elemental Selenium
Diego Molina
Current maintainer of Elemental Selenium