Skip to content

Commit

Permalink
[bidi][js] Add types for user prompt related events (SeleniumHQ#14097)
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani authored and sandeepsuryaprasad committed Oct 29, 2024
1 parent aa4dd0d commit 0e68742
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 35 deletions.
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 { BrowsingContextInfo, NavigationInfo } = require('./browsingContextTypes')
const { BrowsingContextInfo, NavigationInfo, UserPromptOpened, UserPromptClosed } = require('./browsingContextTypes')

/**
* Represents a browsing context related events.
Expand Down Expand Up @@ -127,8 +127,10 @@ class BrowsingContextInspector {
let response = null
if ('navigation' in params) {
response = new NavigationInfo(params.context, params.navigation, params.timestamp, params.url)
} else if ('type' in params) {
response = new UserPromptOpened(params.context, params.type, params.message)
} else if ('accepted' in params) {
/* Needs to be updated when browsers implement other events */
response = new UserPromptClosed(params.context, params.accepted, params.userText)
} else {
response = new BrowsingContextInfo(params.context, params.url, params.children, params.parent)
}
Expand Down
18 changes: 17 additions & 1 deletion javascript/node/selenium-webdriver/bidi/browsingContextTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,20 @@ class NavigationInfo {
}
}

module.exports = { BrowsingContextInfo, NavigationInfo }
class UserPromptOpened {
constructor(browsingContextId, type, message) {
this.browsingContextId = browsingContextId
this.type = type
this.message = message
}
}

class UserPromptClosed {
constructor(browsingContextId, accepted, userText = undefined) {
this.browsingContextId = browsingContextId
this.accepted = accepted
this.userText = userText
}
}

module.exports = { BrowsingContextInfo, NavigationInfo, UserPromptOpened, UserPromptClosed }
Original file line number Diff line number Diff line change
Expand Up @@ -158,52 +158,67 @@ suite(
assert(navigationInfo.url.includes('linkToAnchorOnThisPage'))
})

xit('can listen to user prompt opened event', async function () {
let userpromptOpened = null
const browsingConextInspector = await BrowsingContextInspector(driver)
ignore(env.browsers(Browser.EDGE, Browser.CHROME)).it(
'can listen to user prompt opened event',
async function () {
let userpromptOpened = null
const browsingcontextInspector = await BrowsingContextInspector(driver)

const browsingContext = await BrowsingContext(driver, {
browsingContextId: await driver.getWindowHandle(),
})
const browsingContext = await BrowsingContext(driver, {
browsingContextId: await driver.getWindowHandle(),
})

await driver.get(Pages.alertsPage)
await browsingcontextInspector.onUserPromptOpened((entry) => {
userpromptOpened = entry
})

await driver.findElement(By.id('alert')).click()
await driver.get(Pages.alertsPage)

await driver.wait(until.alertIsPresent())
await driver.findElement(By.id('alert')).click()

await browsingConextInspector.onUserPromptOpened((entry) => {
userpromptOpened = entry
})
await driver.wait(until.alertIsPresent())

assert.equal(userpromptOpened.browsingContextId, browsingContext.id)
assert.equal(userpromptOpened.type, 'alert')
})
await browsingContext.handleUserPrompt(true)

xit('can listen to user prompt closed event', async function () {
let userpromptClosed = null
const browsingConextInspector = await BrowsingContextInspector(driver)
// Chrome/Edge do not return the window's browsing context id as per the spec.
// This assertion fails.
// It is probably a bug in the Chrome/Edge driver.
assert.equal(userpromptOpened.browsingContextId, browsingContext.id)
assert.equal(userpromptOpened.type, 'alert')
},
)

const browsingContext = await BrowsingContext(driver, {
browsingContextId: await driver.getWindowHandle(),
})
ignore(env.browsers(Browser.EDGE, Browser.CHROME)).it(
'can listen to user prompt closed event',
async function () {
const windowHandle = await driver.getWindowHandle()
let userpromptClosed = null
const browsingcontextInspector = await BrowsingContextInspector(driver, windowHandle)

await driver.get(Pages.alertsPage)
const browsingContext = await BrowsingContext(driver, {
browsingContextId: windowHandle,
})

await driver.findElement(By.id('prompt')).click()
await driver.get(Pages.alertsPage)

await driver.wait(until.alertIsPresent())
await driver.findElement(By.id('prompt')).click()

await browsingConextInspector.onUserPromptClosed((entry) => {
userpromptClosed = entry
})
await driver.wait(until.alertIsPresent())

await browsingContext.handleUserPrompt(true, 'selenium')
await browsingcontextInspector.onUserPromptClosed((entry) => {
userpromptClosed = entry
})

assert.equal(userpromptClosed.browsingContextId, browsingContext.id)
assert.equal(userpromptClosed.accepted, true)
assert.equal(userpromptClosed.userText, 'selenium')
})
await browsingContext.handleUserPrompt(true, 'selenium')

// Chrome/Edge do not return the window's browsing context id as per the spec.
// This assertion fails.
// It is probably a bug in the Chrome/Edge driver.
assert.equal(userpromptClosed.browsingContextId, browsingContext.id)
assert.equal(userpromptClosed.accepted, true)
assert.equal(userpromptClosed.userText, 'selenium')
},
)
})
},
{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },
Expand Down

0 comments on commit 0e68742

Please sign in to comment.