Skip to content

Commit

Permalink
[JS]add support to switch frame by name (SeleniumHQ#10670)
Browse files Browse the repository at this point in the history
  • Loading branch information
gravityvi authored and elgatov committed Jun 27, 2022
1 parent 589471e commit 050be81
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
16 changes: 9 additions & 7 deletions javascript/node/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2167,16 +2167,18 @@ class TargetLocator {
* when the driver has changed focus to the specified frame.
*/
frame(id) {
let frameReference = id;

if (typeof id === "string") {
frameReference = this.driver_.findElement({
id: id
});
let frameReference = id
if (typeof id === 'string') {
frameReference = this.driver_
.findElement({ id })
.catch((_) => this.driver_.findElement({ name: id }))
}

return this.driver_.execute(
new command.Command(command.Name.SWITCH_TO_FRAME).setParameter('id', frameReference)
new command.Command(command.Name.SWITCH_TO_FRAME).setParameter(
'id',
frameReference
)
)
}

Expand Down
14 changes: 14 additions & 0 deletions javascript/node/selenium-webdriver/test/frame_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,18 @@ test.suite(function (env) {
'This page has iframes'
)
})

it('can switch to a frame by name', async function () {
await driver.get(test.Pages.iframePage)
await driver.switchTo().frame('iframe1-name')
assert.strictEqual(
await driver.executeScript('return document.title'),
'We Leave From Here'
)
await driver.switchTo().parentFrame()
assert.strictEqual(
await driver.executeScript('return document.title'),
'This page has iframes'
)
})
})

0 comments on commit 050be81

Please sign in to comment.