Skip to content

Commit

Permalink
[JS] Add support to switch frame by id
Browse files Browse the repository at this point in the history
  • Loading branch information
harsha509 committed May 16, 2022
1 parent 6083cc4 commit 97aeedb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions javascript/node/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2162,13 +2162,21 @@ class TargetLocator {
* If the specified frame can not be found, the returned promise will be
* rejected with a {@linkplain error.NoSuchFrameError}.
*
* @param {(number|WebElement|null)} id The frame locator.
* @param {(number|string|WebElement|null)} id The frame locator.
* @return {!Promise<void>} A promise that will be resolved
* 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
});
}

return this.driver_.execute(
new command.Command(command.Name.SWITCH_TO_FRAME).setParameter('id', id)
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 @@ -50,4 +50,18 @@ test.suite(function (env) {
'This page has iframes'
)
})

it('can switch to a frame by id', async function () {
await driver.get(test.Pages.iframePage)
await driver.switchTo().frame("iframe1")
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 97aeedb

Please sign in to comment.