Skip to content

Playwright: grabTextFrom ignores the frame of switchTo #5810

Open

Description

@danielrentz

What are you trying to achieve?

After I.switchTo('iframe'), I.grabTextFrom('#text') should return the text of the element in that frame, like I.see() and I.grabTextFromAll() do.

The Playwright helper reads the text with this.page.textContent(locator), so it always searches the main frame. grabValueFrom, grabAttributeFrom and grabHTMLFrom delegate to their ...All variants, which locate the elements in the current frame, and the Puppeteer helper's grabTextFrom does the same. If the main frame has an element that matches the locator, the step returns its text. Otherwise it fails with text "#text" was not found by text|CSS|XPath.

Would you accept a fix that locates the element in the current frame and still returns its textContent? Role locators would get the same change, since handleRoleLocator is called with this.page as well. Happy to send a PR.

What do you get instead?

see() and grabTextFromAll() find the text in the frame, grabTextFrom() returns the text from the main frame:

  'Outside the frame' == 'Inside the frame'
      + expected - actual
      -Outside the frame
      +Inside the frame
import assert from 'node:assert'

const html = `
<p id="text">Outside the frame</p>
<iframe srcdoc="<p id='text'>Inside the frame</p>"></iframe>`

Feature('grabTextFrom')

Scenario('grabs the text in the frame of switchTo', async ({ I }) => {
  I.amOnPage('data:text/html,' + encodeURIComponent(html))
  I.switchTo('iframe')
  I.see('Inside the frame', '#text')
  const all = await I.grabTextFromAll('#text')
  const text = await I.grabTextFrom('#text')
  assert.deepEqual(all, ['Inside the frame'])
  assert.equal(text, 'Inside the frame')
})

Details

  • CodeceptJS version: 4.2.4 (grabTextFrom is unchanged on 4.x)
  • NodeJS Version: 24.21.0
  • Operating System: Windows 11
  • playwright version: 1.64.1
  • Configuration file:
export const config = {
  tests: './*_test.js',
  output: './output',
  helpers: {
    Playwright: { url: 'http://localhost', browser: 'chromium', show: false, waitForTimeout: 3000, timeout: 3000 },
  },
}

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions