Skip to content

feat: support locating element inside an iframe - #2

Merged
sheremet-va merged 2 commits into
vitest-dev:masterfrom
acaldas:master
Jun 5, 2025
Merged

sheremet-va merged 2 commits into
vitest-dev:masterfrom
acaldas:master

Conversation

@acaldas

@acaldas acaldas commented May 31, 2025

Copy link
Copy Markdown
Contributor

Related to vitest-dev/vitest#6966 and vitest-dev/vitest#8016.

Retrieves the iframe's contentDocument when querying locator.
contentDocument is only available on same-origin urls.

I noticed that on the vitest test, the iframe doesn't load Immediately.
expect.element works. Not sure if there is a way to prevent this?

test('locates an iframe', async () => {
  const iframe = document.createElement('iframe')
  iframe.setAttribute('data-testid', 'iframe')
  iframe.srcdoc = `<div onclick="console.log">Hello World!</div>`
  document.body.append(iframe)

  const frame = page.frameLocator(
    page.getByTestId('iframe'),
  )

  await expect.element(frame.getByText('Hello World')).toBeTruthy()
})

@sheremet-va sheremet-va left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but can you add a test?

@sheremet-va

Copy link
Copy Markdown
Member

I noticed that on the vitest test, the iframe doesn't load Immediately.
expect.element works. Not sure if there is a way to prevent this?

It's not a vitest thing, iframes are loaded asynchronously, you can use iframe.onload = resolve to capture the load event, or just use expect.element

@acaldas

acaldas commented Jun 4, 2025

Copy link
Copy Markdown
Contributor Author

Looks good to me, but can you add a test?

Added a test with the iframe.onload = resolve await.

Comment thread test/iframe.test.ts Outdated
@sheremet-va
sheremet-va merged commit 56a31c9 into vitest-dev:master Jun 5, 2025
@sheremet-va

Copy link
Copy Markdown
Member

Released in 1.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants