diff --git a/javascript/node/selenium-webdriver/bidi/continueRequestParameters.js b/javascript/node/selenium-webdriver/bidi/continueRequestParameters.js new file mode 100644 index 0000000000000..ef305b8e1a66d --- /dev/null +++ b/javascript/node/selenium-webdriver/bidi/continueRequestParameters.js @@ -0,0 +1,83 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +const { BytesValue, Header } = require('./networkTypes') + +class ContinueRequestParameters { + #map = new Map() + + constructor(request) { + this.#map.set('request', request) + } + + body(value) { + if (!(value instanceof BytesValue)) { + throw new Error(`Value must be an instance of BytesValue. Received: '${value})'`) + } + this.#map.set('body', Object.fromEntries(value.asMap())) + return this + } + + cookies(cookieHeaders) { + const cookies = [] + cookieHeaders.forEach((header) => { + if (!(header instanceof Header)) { + throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`) + } + cookies.push(Object.fromEntries(header.asMap())) + }) + + this.#map.set('cookies', cookies) + return this + } + + headers(headers) { + const headerList = [] + headers.forEach((header) => { + if (!(header instanceof Header)) { + throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`) + } + headerList.push(Object.fromEntries(header.asMap())) + }) + + this.#map.set('headers', headerList) + return this + } + + method(method) { + if (typeof method !== 'string') { + throw new Error(`Http method must be a string. Received: '${method})'`) + } + this.#map.set('method', method) + return this + } + + url(url) { + if (typeof url !== 'string') { + throw new Error(`Url must be a string. Received:'${url}'`) + } + + this.#map.set('url', url) + return this + } + + asMap() { + return this.#map + } +} + +module.exports = { ContinueRequestParameters } diff --git a/javascript/node/selenium-webdriver/bidi/continueResponseParameters.js b/javascript/node/selenium-webdriver/bidi/continueResponseParameters.js new file mode 100644 index 0000000000000..04db033f923ae --- /dev/null +++ b/javascript/node/selenium-webdriver/bidi/continueResponseParameters.js @@ -0,0 +1,89 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +const { BytesValue, Header } = require('./networkTypes') + +class ContinueResponseParameters { + #map = new Map() + + constructor(request) { + this.#map.set('request', request) + } + + cookies(cookieHeaders) { + const cookies = [] + cookieHeaders.forEach((header) => { + if (!(header instanceof Header)) { + throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`) + } + cookies.push(Object.fromEntries(header.asMap())) + }) + + this.#map.set('cookies', cookies) + return this + } + + credentials(username, password) { + if (typeof username !== 'string') { + throw new Error(`Username must be a string. Received:'${username}'`) + } + + if (typeof password !== 'string') { + throw new Error(`Password must be a string. Received:'${password}'`) + } + + this.#map.set('credentials', { type: 'password', username: username, password: password }) + + return this + } + + headers(headers) { + const headerList = [] + headers.forEach((header) => { + if (!(header instanceof Header)) { + throw new Error(`Header must be an instance of Header. Received:'${header}'`) + } + headerList.push(Object.fromEntries(header.asMap())) + }) + + this.#map.set('headers', headerList) + return this + } + + reasonPhrase(reasonPhrase) { + if (typeof reasonPhrase !== 'string') { + throw new Error(`Reason phrase must be a string. Received: '${reasonPhrase})'`) + } + this.#map.set('reasonPhrase', reasonPhrase) + return this + } + + statusCode(statusCode) { + if (!Number.isInteger(statusCode)) { + throw new Error(`Status must be an integer. Received:'${statusCode}'`) + } + + this.#map.set('statusCode', statusCode) + return this + } + + asMap() { + return this.#map + } +} + +module.exports = { ContinueResponseParameters } diff --git a/javascript/node/selenium-webdriver/bidi/network.js b/javascript/node/selenium-webdriver/bidi/network.js index 27e9883ba74c7..9d7b3842b0f3f 100644 --- a/javascript/node/selenium-webdriver/bidi/network.js +++ b/javascript/node/selenium-webdriver/bidi/network.js @@ -17,6 +17,8 @@ const { BeforeRequestSent, ResponseStarted, FetchError } = require('./networkTypes') const { AddInterceptParameters } = require('./addInterceptParameters') +const { ContinueResponseParameters } = require('./continueResponseParameters') +const { ContinueRequestParameters } = require('./continueRequestParameters') class Network { constructor(driver, browsingContextIds) { @@ -98,7 +100,7 @@ class Network { async addIntercept(params) { if (!(params instanceof AddInterceptParameters)) { - throw new Error(`Params must be an instance of AddInterceptParamenters. Received:'${params}'`) + throw new Error(`Params must be an instance of AddInterceptParameters. Received:'${params}'`) } const command = { @@ -168,6 +170,32 @@ class Network { await this.bidi.send(command) } + async continueRequest(params) { + if (!(params instanceof ContinueRequestParameters)) { + throw new Error(`Params must be an instance of ContinueRequestParameters. Received:'${params}'`) + } + + const command = { + method: 'network.continueRequest', + params: Object.fromEntries(params.asMap()), + } + + let response = await this.bidi.send(command) + } + + async continueResponse(params) { + if (!(params instanceof ContinueResponseParameters)) { + throw new Error(`Params must be an instance of ContinueResponseParameters. Received:'${params}'`) + } + + const command = { + method: 'network.continueResponse', + params: Object.fromEntries(params.asMap()), + } + + await this.bidi.send(command) + } + async close() { await this.bidi.unsubscribe( 'network.beforeRequestSent', diff --git a/javascript/node/selenium-webdriver/bidi/networkTypes.js b/javascript/node/selenium-webdriver/bidi/networkTypes.js index 26322477a1456..3fb6f180f96af 100644 --- a/javascript/node/selenium-webdriver/bidi/networkTypes.js +++ b/javascript/node/selenium-webdriver/bidi/networkTypes.js @@ -59,10 +59,12 @@ class BytesValue { } class Header { - constructor(name, value, binaryValue) { + constructor(name, value) { this._name = name + if (!(value instanceof BytesValue)) { + throw new Error(`Value must be an instance of BytesValue. Received:'${value}'`) + } this._value = value - this._binaryValue = binaryValue } get name() { @@ -72,10 +74,6 @@ class Header { get value() { return this._value } - - get binaryValue() { - return this._binaryValue - } } class Cookie { @@ -222,9 +220,8 @@ class RequestData { headers.forEach((header) => { let name = header.name let value = 'value' in header ? header.value : null - let binaryValue = 'binaryValue' in header ? header.binaryValue : null - this._headers.push(new Header(name, value, binaryValue)) + this._headers.push(new Header(name, new BytesValue(value.type, value.value))) }) this._cookies = [] @@ -488,4 +485,4 @@ class ResponseStarted extends BaseParameters { } } -module.exports = { BytesValue, Cookie, SameSite, BeforeRequestSent, ResponseStarted, FetchError } +module.exports = { Header, BytesValue, Cookie, SameSite, BeforeRequestSent, ResponseStarted, FetchError } diff --git a/javascript/node/selenium-webdriver/test/bidi/network_commands_test.js b/javascript/node/selenium-webdriver/test/bidi/network_commands_test.js index 5efbb94adcecb..0e0c5d46cd9b7 100644 --- a/javascript/node/selenium-webdriver/test/bidi/network_commands_test.js +++ b/javascript/node/selenium-webdriver/test/bidi/network_commands_test.js @@ -25,6 +25,8 @@ const Network = require('../../bidi/network') const { AddInterceptParameters } = require('../../bidi/addInterceptParameters') const { InterceptPhase } = require('../../bidi/interceptPhase') const { until } = require('../../index') +const { ContinueRequestParameters } = require('../../bidi/continueRequestParameters') +const { ContinueResponseParameters } = require('../../bidi/continueResponseParameters') suite( function (env) { @@ -115,6 +117,36 @@ suite( assert.strictEqual(e.name, 'TimeoutError') } }) + + xit('can continue request', async function () { + await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT)) + + let counter = 0 + + await network.beforeRequestSent(async (event) => { + await network.continueRequest(new ContinueRequestParameters(event.request.request)) + counter = counter + 1 + }) + + await driver.get(Pages.logEntryAdded) + + assert.strictEqual(counter, 1) + }) + + xit('can continue response', async function () { + await network.addIntercept(new AddInterceptParameters(InterceptPhase.RESPONSE_STARTED)) + + let counter = 0 + + await network.responseStarted(async (event) => { + await network.continueResponse(new ContinueResponseParameters(event.request.request)) + counter = counter + 1 + }) + + await driver.get(Pages.logEntryAdded) + + assert.strictEqual(counter, 1) + }) }) }, { browsers: [Browser.FIREFOX] },