Skip to content

Commit

Permalink
devtools package: fix switchToFrame function (#5350)
Browse files Browse the repository at this point in the history
  • Loading branch information
takeyaqa authored May 6, 2020
1 parent 43c7f4e commit 107c26a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
23 changes: 23 additions & 0 deletions e2e/protocol.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ describe('frames', () => {
await browser.switchToParentFrame()
expect(await browser.getTitle()).toBe('The Internet')
})

it('switchToFrame(number)', async () => {
const getDocumentText = () => browser.executeScript(
'return document.documentElement.outerText',
[]
)

expect(await getDocumentText())
.toContain('An iFrame containing the TinyMCE WYSIWYG Editor')
await browser.switchToFrame(0)

expect(await getDocumentText())
.toContain('Your content goes here.')

const body = await browser.findElement('css selector', 'body')
expect(await browser.getElementAttribute(body[ELEMENT_KEY], 'id')).toBe('tinymce')
await browser.elementClick(body[ELEMENT_KEY])
})

it('switchToFrame(null)', async () => {
await browser.switchToFrame(null)
expect(await browser.getTitle()).toBe('The Internet')
})
})

describe('executeScript', () => {
Expand Down
26 changes: 13 additions & 13 deletions packages/devtools/src/commands/switchToFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import { getStaleElementError } from '../utils'
export default async function switchToFrame ({ id }) {
const page = this.getPageHandle(true)

/**
* switch to parent frame
*/
if (id === null && typeof page.parentFrame === 'function') {
let parentFrame = await page.parentFrame()
while (parentFrame) {
parentFrame = await parentFrame.parentFrame()
}
this.currentFrame = parentFrame
return null
}

/**
* switch frame by element ID
*/
Expand Down Expand Up @@ -32,7 +44,7 @@ export default async function switchToFrame ({ id }) {
* `page` has `frames` method while `frame` has `childFrames` method
*/
let getFrames = page.frames || page.childFrames
const childFrames = await getFrames()
const childFrames = await getFrames.apply(page)
const childFrame = childFrames[id]

if (!childFrame) {
Expand All @@ -43,17 +55,5 @@ export default async function switchToFrame ({ id }) {
return null
}

/**
* switch to parent frame
*/
if (id === null && typeof page.parentFrame === 'function') {
let parentFrame = await page.parentFrame()
while (parentFrame) {
parentFrame = await page.parentFrame()
}
this.currentFrame = parentFrame
return null
}

throw new Error(`Could not switch frame, unknwon id: ${id}`)
}

0 comments on commit 107c26a

Please sign in to comment.