Skip to content

Commit c8064e3

Browse files
johnybDavertMik
authored andcommitted
fix(Puppeteer): simplify switchTo iframe by selector (#1974)
switchTo did not work as expected in some situations, because the lookup code did not consider every possibility. However, given a ElementHandle, the corresponding frame can be fetched directly using Puppeteer API. This allows to remove a lot of code and should still work as expected. Fixes: #1439
1 parent d95f312 commit c8064e3

1 file changed

Lines changed: 3 additions & 29 deletions

File tree

‎lib/helper/Puppeteer.js‎

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,17 +1915,10 @@ class Puppeteer extends Helper {
19151915
// iframe by selector
19161916
const els = await this._locate(locator);
19171917
assertElementExists(els, locator);
1918+
const contentFrame = await els[0].contentFrame();
19181919

1919-
const iframeName = await els[0].getProperty('name').then(el => el.jsonValue());
1920-
const iframeId = await els[0].getProperty('id').then(el => el.jsonValue());
1921-
const iframeUrl = await els[0].getProperty('src').then(el => el.jsonValue());
1922-
1923-
const searchName = iframeName || iframeId; // Name takes precedence over id, because of puppeteer's Frame.name() function
1924-
const currentContext = await this._getContext();
1925-
const resFrame = await findFrame.call(this, currentContext, searchName, iframeUrl);
1926-
1927-
if (resFrame) {
1928-
this.context = resFrame;
1920+
if (contentFrame) {
1921+
this.context = contentFrame;
19291922
} else {
19301923
this.context = els[0];
19311924
}
@@ -2028,25 +2021,6 @@ class Puppeteer extends Helper {
20282021

20292022
module.exports = Puppeteer;
20302023

2031-
async function findFrame(context, name, url) {
2032-
if (!context) {
2033-
return;
2034-
}
2035-
let frames = [];
2036-
if (typeof context.childFrames === 'function') {
2037-
frames = context.childFrames();
2038-
} else {
2039-
frames = this.page.frames();
2040-
}
2041-
2042-
return frames.find((frame) => {
2043-
if (name || !url) {
2044-
return frame.name() === name;
2045-
}
2046-
return frame.url() === url;
2047-
});
2048-
}
2049-
20502024
async function findElements(matcher, locator) {
20512025
if (locator.react) return findReact(matcher, locator);
20522026
locator = new Locator(locator, 'css');

0 commit comments

Comments
 (0)