Skip to content

Commit

Permalink
[bidi][js] Add 'fetchError' command
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Feb 14, 2024
1 parent 0b22bc8 commit b6b2096
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
17 changes: 15 additions & 2 deletions javascript/node/selenium-webdriver/bidi/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

const { BeforeRequestSent, ResponseStarted } = require('./networkTypes')
const { BeforeRequestSent, ResponseStarted, FetchError} = require('./networkTypes')
const {AddInterceptParameters} = require("./addInterceptParameters");

class Network {
Expand Down Expand Up @@ -44,6 +44,10 @@ class Network {
await this.subscribeAndHandleEvent('network.authRequired', callback)
}

async fetchError(callback) {
await this.subscribeAndHandleEvent('network.fetchError', callback)
}

async subscribeAndHandleEvent(eventType, callback) {
if (this._browsingContextIds != null) {
await this.bidi.subscribe(eventType, this._browsingContextIds)
Expand Down Expand Up @@ -77,9 +81,18 @@ class Network {
params.timestamp,
params.response,
)
} else if ('errorText' in params) {
response = new FetchError(
params.context,
params.navigation,
params.redirectCount,
params.request,
params.timestamp,
params.errorText,
)
}
callback(response)
}
}
})
}

Expand Down
13 changes: 12 additions & 1 deletion javascript/node/selenium-webdriver/bidi/networkTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,17 @@ class BeforeRequestSent extends BaseParameters {
}
}

class FetchError extends BaseParameters {
constructor(id, navigation, redirectCount, request, timestamp, errorText) {
super(id, navigation, redirectCount, request, timestamp)
this._errorText = errorText
}

get errorText() {
return this._errorText
}
}

class ResponseData {
constructor(
url,
Expand Down Expand Up @@ -442,4 +453,4 @@ class ResponseStarted extends BaseParameters {
}
}

module.exports = { BeforeRequestSent, ResponseStarted }
module.exports = { BeforeRequestSent, ResponseStarted, FetchError }
27 changes: 24 additions & 3 deletions javascript/node/selenium-webdriver/test/bidi/network_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

const assert = require('assert')
const firefox = require('../../firefox')
const { Browser } = require('../../')
const { Pages, suite } = require('../../lib/test')
const {Browser} = require('../../')
const {Pages, suite} = require('../../lib/test')
const Network = require('../../bidi/network')
const until = require('../../lib/until')

Expand Down Expand Up @@ -174,6 +174,27 @@ suite(
assert.equal(authRequiredEvent.response.url.includes('basicAuth'), true)
})

xit('can listen to fetch error event', async function () {
let fetchErrorEvent = null
const network = await Network(driver)
await network.fetchError(function (event) {
fetchErrorEvent = event
})

try {
await driver.get('https://not_a_valid_url.test/')
} catch (e) {
// ignore
}

const url = fetchErrorEvent.request.url
assert.equal(fetchErrorEvent.id, await driver.getWindowHandle())
assert.equal(fetchErrorEvent.request.method, 'GET')
assert.equal(url.includes('valid_url'), true)
assert.equal(fetchErrorEvent.request.headers.length > 1, true)
assert.equal(fetchErrorEvent.errorText, 'NS_ERROR_UNKNOWN_HOST')
})

it('test response completed mime type', async function () {
let onResponseCompleted = []
const network = await Network(driver)
Expand All @@ -196,5 +217,5 @@ suite(
})
})
},
{ browsers: [Browser.FIREFOX] },
{browsers: [Browser.FIREFOX]},
)

0 comments on commit b6b2096

Please sign in to comment.