diff --git a/javascript/node/selenium-webdriver/bidi/browsingContext.js b/javascript/node/selenium-webdriver/bidi/browsingContext.js index eb9bb88160146..ec22aff2e296a 100644 --- a/javascript/node/selenium-webdriver/bidi/browsingContext.js +++ b/javascript/node/selenium-webdriver/bidi/browsingContext.js @@ -154,6 +154,32 @@ class BrowsingContext { return new BrowsingContextInfo(result['context'], result['url'], result['children'], result['parent']) } + /** + * @returns {Promise>} A Promise that resolves to an array of BrowsingContextInfo objects representing the top-level browsing contexts. + */ + async getTopLevelContexts() { + const params = { + method: 'browsingContext.getTree', + params: {}, + } + + let result = await this.bidi.send(params) + if ('error' in result) { + throw Error(result['error']) + } + + const contexts = result['result']['contexts']; + const browsingContexts = contexts.map(context => { + return new BrowsingContextInfo( + context['id'], + context['url'], + context['children'], + context['parent'] + ); + }); + return browsingContexts; + } + /** * Closes the browsing context * @returns {Promise} diff --git a/javascript/node/selenium-webdriver/test/bidi/browsingcontext_test.js b/javascript/node/selenium-webdriver/test/bidi/browsingcontext_test.js index 1c7094accc2cb..52488babb02df 100644 --- a/javascript/node/selenium-webdriver/test/bidi/browsingcontext_test.js +++ b/javascript/node/selenium-webdriver/test/bidi/browsingcontext_test.js @@ -447,7 +447,18 @@ suite( const devicePixelRatio = await driver.executeScript('return window.devicePixelRatio;') assert.equal(devicePixelRatio, 5) - }) + }); + + it('Get All Top level browsing contexts', async ()=> { + const id = await driver.getWindowHandle() + const window1 = await BrowsingContext(driver, { + browsingContextId: id, + }); + const window2 = await BrowsingContext(driver, {type: 'window'}); + + const res = await window1.getTopLevelContexts(); + assert.equal(res.length, 2); + }); }) }, { browsers: [Browser.FIREFOX] },