Skip to content

Commit

Permalink
[bidi][js] Fix the event unsubscribe method. Update modules to have c…
Browse files Browse the repository at this point in the history
…lose methods.
  • Loading branch information
pujagani committed Jun 26, 2024
1 parent 84cc67e commit 8290096
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ class BrowsingContextInspector {
}
})
}

async close() {
if (
this._browsingContextIds !== null &&
this._browsingContextIds !== undefined &&
this._browsingContextIds.length > 0
) {
await this.bidi.unsubscribe(
'browsingContext.contextCreated',
'browsingContext.contextDestroyed',
'browsingContext.fragmentNavigated',
'browsingContext.userPromptClosed',
this._browsingContextIds,
)
} else {
await this.bidi.unsubscribe(
'browsingContext.contextCreated',
'browsingContext.contextDestroyed',
'browsingContext.fragmentNavigated',
'browsingContext.userPromptClosed',
)
}
}
}

async function getBrowsingContextInstance(driver, browsingContextIds = null) {
Expand Down
20 changes: 14 additions & 6 deletions javascript/node/selenium-webdriver/bidi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ class Index extends EventEmitter {
params.params.contexts = contextsArray
}

this.events.push(...eventsArray)

await this.send(params)
}

Expand All @@ -167,22 +169,28 @@ class Index extends EventEmitter {
* @returns {Promise<void>}
*/
async unsubscribe(events, browsingContexts) {
if (typeof events === 'string') {
this.events = this.events.filter((event) => event !== events)
} else if (Array.isArray(events)) {
this.events = this.events.filter((event) => !events.includes(event))
}
const eventsToRemove = typeof events === 'string' ? [events] : events

// Check if the eventsToRemove are in the subscribed events array
// Filter out events that are not in this.events before filtering
const existingEvents = eventsToRemove.filter((event) => this.events.includes(event))

// Remove the events from the subscribed events array
this.events = this.events.filter((event) => !existingEvents.includes(event))

if (typeof browsingContexts === 'string') {
this.browsingContexts.pop()
} else if (Array.isArray(browsingContexts)) {
this.browsingContexts = this.browsingContexts.filter((id) => !browsingContexts.includes(id))
}

if (existingEvents.length === 0) {
return
}
const params = {
method: 'session.unsubscribe',
params: {
events: this.events,
events: existingEvents,
},
}

Expand Down
10 changes: 9 additions & 1 deletion javascript/node/selenium-webdriver/bidi/logInspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,15 @@ class LogInspector {
* @returns {Promise<void>}
*/
async close() {
await this.bidi.unsubscribe('log.entryAdded', this._browsingContextIds)
if (
this._browsingContextIds !== null &&
this._browsingContextIds !== undefined &&
this._browsingContextIds.length > 0
) {
await this.bidi.unsubscribe('log.entryAdded', this._browsingContextIds)
} else {
await this.bidi.unsubscribe('log.entryAdded')
}
}
}

Expand Down
26 changes: 20 additions & 6 deletions javascript/node/selenium-webdriver/bidi/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,26 @@ class Network {
* @returns {Promise<void>} A promise that resolves when the network connection is closed.
*/
async close() {
await this.bidi.unsubscribe(
'network.beforeRequestSent',
'network.responseStarted',
'network.responseCompleted',
'network.authRequired',
)
if (
this._browsingContextIds !== null &&
this._browsingContextIds !== undefined &&
this._browsingContextIds.length > 0
) {
await this.bidi.unsubscribe(
'network.beforeRequestSent',
'network.responseStarted',
'network.responseCompleted',
'network.authRequired',
this._browsingContextIds,
)
} else {
await this.bidi.unsubscribe(
'network.beforeRequestSent',
'network.responseStarted',
'network.responseCompleted',
'network.authRequired',
)
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions javascript/node/selenium-webdriver/bidi/scriptManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,23 @@ class ScriptManager {
}
})
}

async close() {
if (
this._browsingContextIds !== null &&
this._browsingContextIds !== undefined &&
this._browsingContextIds.length > 0
) {
await this.bidi.unsubscribe(
'script.message',
'script.realmCreated',
'script.realmDestroyed',
this._browsingContextIds,
)
} else {
await this.bidi.unsubscribe('script.message', 'script.realmCreated', 'script.realmDestroyed')
}
}
}

async function getScriptManagerInstance(browsingContextId, driver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,32 @@ const { UrlPattern } = require('../../bidi/urlPattern')
suite(
function (env) {
let driver
let network

beforeEach(async function () {
driver = await env.builder().build()
network = await Network(driver)
})

afterEach(async function () {
await network.close()
await driver.quit()
})

describe('Add Intercept parameters test', function () {
it('can add intercept phase', async function () {
const network = await Network(driver)
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
assert.notEqual(intercept, null)
})

it('can add intercept phases', async function () {
const network = await Network(driver)
const intercept = await network.addIntercept(
new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED, InterceptPhase.BEFORE_REQUEST_SENT),
)
assert.notEqual(intercept, null)
})

it('can add string url pattern', async function () {
const network = await Network(driver)
const intercept = await network.addIntercept(
new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT).urlStringPattern(
'http://localhost:4444/basicAuth',
Expand All @@ -63,7 +63,6 @@ suite(
})

it('can add string url patterns', async function () {
const network = await Network(driver)
const intercept = await network.addIntercept(
new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT).urlStringPatterns([
'http://localhost:4444/basicAuth',
Expand All @@ -74,7 +73,6 @@ suite(
})

it('can add url pattern', async function () {
const network = await Network(driver)
const urlPattern = new UrlPattern().protocol('http').hostname('localhost').port(4444).pathname('basicAuth')
const intercept = await network.addIntercept(
new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT).urlPattern(urlPattern),
Expand All @@ -83,7 +81,6 @@ suite(
})

it('can add url patterns', async function () {
const network = await Network(driver)
const urlPattern1 = new UrlPattern()
.protocol('http')
.hostname('localhost')
Expand Down
6 changes: 4 additions & 2 deletions javascript/node/selenium-webdriver/test/bidi/bidi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,22 @@ const until = require('../../lib/until')
suite(
function (env) {
let driver
let inspector

beforeEach(async function () {
driver = await env.builder().build()
inspector = await logInspector(driver)
})

afterEach(async function () {
await inspector.close()
await driver.quit()
})

describe('Integration Tests', function () {
it('can navigate and listen to errors', async function () {
let logEntry = null
const inspector = await logInspector(driver)

await inspector.onJavascriptException(function (log) {
logEntry = log
})
Expand All @@ -61,7 +64,6 @@ suite(
assert.equal(logEntry.type, 'javascript')
assert.equal(logEntry.level, 'error')

await inspector.close()
await browsingContext.close()
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@ const until = require('../../lib/until')
suite(
function (env) {
let driver
let browsingcontextInspector

beforeEach(async function () {
driver = await env.builder().build()
})

afterEach(async function () {
await browsingcontextInspector.close()
await driver.quit()
})

describe('Browsing Context Inspector', function () {
it('can listen to window browsing context created event', async function () {
let contextInfo = null
const browsingcontextInspector = await BrowsingContextInspector(driver)
browsingcontextInspector = await BrowsingContextInspector(driver)
await browsingcontextInspector.onBrowsingContextCreated((entry) => {
contextInfo = entry
})
Expand All @@ -54,7 +56,7 @@ suite(

it('can listen to browsing context destroyed event', async function () {
let contextInfo = null
const browsingcontextInspector = await BrowsingContextInspector(driver)
browsingcontextInspector = await BrowsingContextInspector(driver)
await browsingcontextInspector.onBrowsingContextDestroyed((entry) => {
contextInfo = entry
})
Expand All @@ -72,7 +74,7 @@ suite(

it('can listen to tab browsing context created event', async function () {
let contextInfo = null
const browsingcontextInspector = await BrowsingContextInspector(driver)
browsingcontextInspector = await BrowsingContextInspector(driver)
await browsingcontextInspector.onBrowsingContextCreated((entry) => {
contextInfo = entry
})
Expand All @@ -87,7 +89,7 @@ suite(
})

it('can listen to dom content loaded event', async function () {
const browsingcontextInspector = await BrowsingContextInspector(driver)
browsingcontextInspector = await BrowsingContextInspector(driver)
let navigationInfo = null
await browsingcontextInspector.onDomContentLoaded((entry) => {
navigationInfo = entry
Expand All @@ -104,7 +106,7 @@ suite(

it('can listen to browsing context loaded event', async function () {
let navigationInfo = null
const browsingcontextInspector = await BrowsingContextInspector(driver)
browsingcontextInspector = await BrowsingContextInspector(driver)

await browsingcontextInspector.onBrowsingContextLoaded((entry) => {
navigationInfo = entry
Expand Down Expand Up @@ -162,7 +164,7 @@ suite(
'can listen to user prompt opened event',
async function () {
let userpromptOpened = null
const browsingcontextInspector = await BrowsingContextInspector(driver)
browsingcontextInspector = await BrowsingContextInspector(driver)

const browsingContext = await BrowsingContext(driver, {
browsingContextId: await driver.getWindowHandle(),
Expand Down Expand Up @@ -193,7 +195,7 @@ suite(
async function () {
const windowHandle = await driver.getWindowHandle()
let userpromptClosed = null
const browsingcontextInspector = await BrowsingContextInspector(driver, windowHandle)
browsingcontextInspector = await BrowsingContextInspector(driver, windowHandle)

const browsingContext = await BrowsingContext(driver, {
browsingContextId: windowHandle,
Expand Down
Loading

0 comments on commit 8290096

Please sign in to comment.