|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +class BrowsingContext { |
| 19 | + constructor(driver) { |
| 20 | + this._driver = driver |
| 21 | + } |
| 22 | + |
| 23 | + async init({ browsingContextId, type, referenceContext }) { |
| 24 | + if (!(await this._driver.getCapabilities()).get('webSocketUrl')) { |
| 25 | + throw Error('WebDriver instance must support BiDi protocol') |
| 26 | + } |
| 27 | + |
| 28 | + if (type != undefined && !['window', 'tab'].includes(type)) { |
| 29 | + throw Error(`Valid types are 'window' & 'tab'. Received: ${type}`) |
| 30 | + } |
| 31 | + |
| 32 | + this.bidi = await this._driver.getBidi() |
| 33 | + this._id = |
| 34 | + browsingContextId == undefined |
| 35 | + ? (await this.create(type, referenceContext))['result']['context'] |
| 36 | + : browsingContextId |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates a browsing context for the given type and referenceContext |
| 41 | + */ |
| 42 | + async create(type, referenceContext) { |
| 43 | + const params = { |
| 44 | + method: 'browsingContext.create', |
| 45 | + params: { |
| 46 | + type: type, |
| 47 | + referenceContext: referenceContext, |
| 48 | + }, |
| 49 | + } |
| 50 | + return await this.bidi.send(params) |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @returns id |
| 55 | + */ |
| 56 | + get id() { |
| 57 | + return this._id |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param url the url to navigate to |
| 62 | + * @param readinessState type of readiness state: "none" / "interactive" / "complete" |
| 63 | + * @returns NavigateResult object |
| 64 | + */ |
| 65 | + async navigate(url, readinessState = undefined) { |
| 66 | + if ( |
| 67 | + readinessState != undefined && |
| 68 | + !['none', 'interactive', 'complete'].includes(readinessState) |
| 69 | + ) { |
| 70 | + throw Error( |
| 71 | + `Valid readiness states are 'none', 'interactive' & 'complete'. Received: ${readinessState}` |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + const params = { |
| 76 | + method: 'browsingContext.navigate', |
| 77 | + params: { |
| 78 | + context: this._id, |
| 79 | + url: url, |
| 80 | + wait: readinessState, |
| 81 | + }, |
| 82 | + } |
| 83 | + const navigateResult = (await this.bidi.send(params))['result'] |
| 84 | + |
| 85 | + return new NavigateResult( |
| 86 | + navigateResult['url'], |
| 87 | + navigateResult['navigation'] |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param maxDepth the max depth of the descendents of browsing context tree |
| 93 | + * @returns BrowsingContextInfo object |
| 94 | + */ |
| 95 | + async getTree(maxDepth = undefined) { |
| 96 | + const params = { |
| 97 | + method: 'browsingContext.getTree', |
| 98 | + params: { |
| 99 | + root: this._id, |
| 100 | + maxDepth: maxDepth, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + let result = await this.bidi.send(params) |
| 105 | + if ('error' in result) { |
| 106 | + throw Error(result['error']) |
| 107 | + } |
| 108 | + |
| 109 | + result = result['result']['contexts'][0] |
| 110 | + return new BrowsingContextInfo( |
| 111 | + result['context'], |
| 112 | + result['url'], |
| 113 | + result['children'], |
| 114 | + result['parent'] |
| 115 | + ) |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Closes the browing context |
| 120 | + * @returns {Promise<void>} |
| 121 | + */ |
| 122 | + async close() { |
| 123 | + const params = { |
| 124 | + method: 'browsingContext.close', |
| 125 | + params: { |
| 126 | + context: this._id, |
| 127 | + }, |
| 128 | + } |
| 129 | + await this.bidi.send(params) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +class NavigateResult { |
| 134 | + constructor(url, navigationId) { |
| 135 | + this._url = url |
| 136 | + this._navigationId = navigationId |
| 137 | + } |
| 138 | + |
| 139 | + get url() { |
| 140 | + return this._url |
| 141 | + } |
| 142 | + |
| 143 | + get navigationId() { |
| 144 | + return this._navigationId |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +class BrowsingContextInfo { |
| 149 | + constructor(id, url, children, parentBrowsingContext) { |
| 150 | + this._id = id |
| 151 | + this._url = url |
| 152 | + this._children = children |
| 153 | + this._parentBrowsingContext = parentBrowsingContext |
| 154 | + } |
| 155 | + |
| 156 | + get id() { |
| 157 | + return this._id |
| 158 | + } |
| 159 | + |
| 160 | + get url() { |
| 161 | + return this._url |
| 162 | + } |
| 163 | + |
| 164 | + get children() { |
| 165 | + return this._children |
| 166 | + } |
| 167 | + |
| 168 | + get parentBrowsingContext() { |
| 169 | + return this._parentBrowsingContext |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * initiate browsing context instance and return |
| 175 | + * @param driver |
| 176 | + * @param browsingContextId The browsing context of current window/tab |
| 177 | + * @param type "window" or "tab" |
| 178 | + * @param referenceContext To get a browsing context for this reference if passed |
| 179 | + * @returns {Promise<BrowsingContext>} |
| 180 | + */ |
| 181 | +async function getBrowsingContextInstance( |
| 182 | + driver, |
| 183 | + { browsingContextId, type, referenceContext } |
| 184 | +) { |
| 185 | + let instance = new BrowsingContext(driver) |
| 186 | + await instance.init({ browsingContextId, type, referenceContext }) |
| 187 | + return instance |
| 188 | +} |
| 189 | + |
| 190 | +/** |
| 191 | + * API |
| 192 | + * @type {function(*, {*,*,*}): Promise<BrowsingContext>} |
| 193 | + */ |
| 194 | +module.exports = getBrowsingContextInstance |
0 commit comments