Skip to content

Commit

Permalink
[bidi][js] Add handle user prompt command
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Oct 31, 2023
1 parent eac0e28 commit 0291a70
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
21 changes: 21 additions & 0 deletions common/src/web/userprompt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>
<head>
<title>Testing Alerts</title>
</head>
<script type='text/javascript'>
function myFunction() {
let message = prompt('Please enter a message');
if (message != null) {
document.getElementById('result').innerHTML =
'Message: ' + message ;
}
}
</script>
<style>

</style>
<body >
<button id='alert' onclick='myFunction()'>Try it</button>
<p id="result"></p>
</body>
</html>
16 changes: 16 additions & 0 deletions javascript/node/selenium-webdriver/bidi/browsingContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,22 @@ class BrowsingContext {
throw Error(result['error'])
}
}

async handleUserPrompt(accept = undefined, userText = undefined) {
const params = {
method: 'browsingContext.handleUserPrompt',
params: {
context: this._id,
accept: accept,
userText: userText,
},
}

let result = await this.bidi.send(params)
if ('error' in result) {
throw Error(result['error'])
}
}
}

class NavigateResult {
Expand Down
1 change: 1 addition & 0 deletions javascript/node/selenium-webdriver/lib/test/fileserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const Pages = (function () {
addPage('webComponents', 'webComponents.html')
addPage('xhtmlTestPage', 'xhtmlTest.html')
addPage('uploadInvisibleTestPage', 'upload_invisible.html')
addPage('userpromptPage', 'userprompt.html')
addPage('virtualAuthenticator', 'virtual-authenticator.html')
addPage('logEntryAdded', 'bidi/logEntryAdded.html')
addPage('scriptTestAccessProperty', 'bidi/scriptTestAccessProperty.html')
Expand Down
117 changes: 117 additions & 0 deletions javascript/node/selenium-webdriver/test/bidi/bidi_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,123 @@ suite(

assert.equal(result2, true)
})

it('can handle user prompt', async function () {
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})

await driver.get(Pages.alertsPage)

await driver.findElement(By.id('alert')).click()

await driver.wait(until.alertIsPresent())

await browsingContext.handleUserPrompt()

const result = await driver.getTitle()

assert.equal(result, 'Testing Alerts')
})

it('can accept user prompt', async function () {
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})

await driver.get(Pages.alertsPage)

await driver.findElement(By.id('alert')).click()

await driver.wait(until.alertIsPresent())

await browsingContext.handleUserPrompt(true)

const result = await driver.getTitle()

assert.equal(result, 'Testing Alerts')
})

it('can dismiss user prompt', async function () {
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})

await driver.get(Pages.alertsPage)

await driver.findElement(By.id('alert')).click()

await driver.wait(until.alertIsPresent())

await browsingContext.handleUserPrompt(false)

const result = await driver.getTitle()

assert.equal(result, 'Testing Alerts')
})

it('can pass user text to user prompt', async function () {
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})

await driver.get(Pages.userpromptPage)

await driver.findElement(By.id('alert')).click()

await driver.wait(until.alertIsPresent())

const userText = 'Selenium automates browsers'

await browsingContext.handleUserPrompt(undefined, userText)

const result = await driver.getPageSource()
assert.equal(result.includes(userText), true)
})

it('can accept user prompt with user text', async function () {
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})

await driver.get(Pages.userpromptPage)

await driver.findElement(By.id('alert')).click()

await driver.wait(until.alertIsPresent())

const userText = 'Selenium automates browsers'

await browsingContext.handleUserPrompt(true, userText)

const result = await driver.getPageSource()
assert.equal(result.includes(userText), true)
})

it('can dismiss user prompt with user text', async function () {
const id = await driver.getWindowHandle()
const browsingContext = await BrowsingContext(driver, {
browsingContextId: id,
})

await driver.get(Pages.userpromptPage)

await driver.findElement(By.id('alert')).click()

await driver.wait(until.alertIsPresent())

const userText = 'Selenium automates browsers'

await browsingContext.handleUserPrompt(false, userText)

const result = await driver.getPageSource()
assert.equal(result.includes(userText), false)
})
})

describe('Browsing Context Inspector', function () {
Expand Down

0 comments on commit 0291a70

Please sign in to comment.