Path: blob/trunk/javascript/selenium-webdriver/bidi/browser.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617const { WindowState, ClientWindowInfo } = require('./clientWindowInfo')1819/**20* Represents the commands and events under Browser Module.21* Described in https://w3c.github.io/webdriver-bidi/#module-browser22*/23class Browser {24constructor(driver) {25this._driver = driver26}2728async init() {29if (!(await this._driver.getCapabilities()).get('webSocketUrl')) {30throw Error('WebDriver instance must support BiDi protocol')31}3233this.bidi = await this._driver.getBidi()34}3536/**37* Creates a new user context.38* @returns {Promise<string>} A promise that resolves to the user context id.39*/40async createUserContext() {41const command = {42method: 'browser.createUserContext',43params: {},44}4546let response = await this.bidi.send(command)4748return response.result.userContext49}5051/**52* Gets the list of all user contexts.53* @returns {Promise<string[]>} A promise that resolves to an array of user context ids.54*/55async getUserContexts() {56const command = {57method: 'browser.getUserContexts',58params: {},59}6061let response = await this.bidi.send(command)6263let userContexts = []6465let userContextsArray = response.result.userContexts6667for (let userContextJson of userContextsArray) {68userContexts.push(userContextJson.userContext)69}7071return userContexts72}7374/**75* Removes a user context.76* @param {string} userContext The user context id to be removed.77* @returns {Promise<void>}78*/79async removeUserContext(userContext) {80const command = {81method: 'browser.removeUserContext',82params: { userContext: userContext },83}8485await this.bidi.send(command)86}8788/**89* Gets information about all client windows.90* @returns {Promise<ClientWindowInfo[]>} Array of client window information91*/92async getClientWindows() {93const command = {94method: 'browser.getClientWindows',95params: {},96}9798const response = await this.bidi.send(command)99return response.result.clientWindows.map((window) => ClientWindowInfo.fromJson(window))100}101}102103async function getBrowserInstance(driver) {104let instance = new Browser(driver)105await instance.init()106return instance107}108109module.exports = getBrowserInstance110module.exports.WindowState = WindowState111112113