Skip to content

Commit 4f0fbf3

Browse files
committed
[js] Add tests and update test setup
1 parent 374092b commit 4f0fbf3

File tree

7 files changed

+235
-27
lines changed

7 files changed

+235
-27
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>FedCM Example</title>
5+
</head>
6+
<body>
7+
<button id="triggerButton" onclick="triggerFedCm()">Trigger FedCM</button>
8+
<div id="result"></div>
9+
10+
<script>
11+
// Use a relative path for the configURL
12+
let configURL = `http://${location.host}/common/fedcm/config.json`;
13+
console.log(configURL)
14+
let result = null;
15+
16+
async function triggerFedCm() {
17+
console.log("Config URL:", configURL);
18+
try {
19+
let promise = await navigator.credentials.get({
20+
identity: {
21+
providers: [{
22+
configURL: configURL,
23+
clientId: '1',
24+
}]
25+
}
26+
});
27+
result = promise;
28+
29+
console.log("Promised!")
30+
console.log(result)
31+
document.getElementById('result').innerText = JSON.stringify(result);
32+
} catch (error) {
33+
console.error("FedCM Error:", error);
34+
result = { error: error.message };
35+
document.getElementById('result').innerText = JSON.stringify(result);
36+
}
37+
}
38+
</script>
39+
</body>
40+
</html>

javascript/node/selenium-webdriver/lib/fedcm/account.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Account {
3434
this._accountId = accountId
3535
this._email = email
3636
this._name = name
37+
this._givenName = givenName
3738
this._pictureUrl = pictureUrl
3839
this._idpConfigUrl = idpConfigUrl
3940
this._loginState = loginState
@@ -53,6 +54,10 @@ class Account {
5354
return this._name
5455
}
5556

57+
get givenName() {
58+
return this._givenName
59+
}
60+
5661
get pictureUrl() {
5762
return this._pictureUrl
5863
}

javascript/node/selenium-webdriver/lib/fedcm/dialog.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,46 @@ class Dialog {
2828
this._driver = driver
2929
}
3030

31-
title() {
32-
return this._driver.execute(new command.Command(command.Name.GET_FEDCM_TITLE))
31+
async title() {
32+
const result = await this._driver.execute(
33+
new command.Command(command.Name.GET_FEDCM_TITLE))
34+
35+
return result.title
3336
}
3437

3538
subtitle() {
36-
return this._driver.execute(new command.Command(command.Name.GET_FEDCM_TITLE))
39+
return this._driver.execute(
40+
new command.Command(command.Name.GET_FEDCM_TITLE))
3741
}
3842

3943
type() {
40-
return this._driver.execute(new command.Command(command.Name.GET_FEDCM_DIALOG_TYPE))
44+
return this._driver.execute(
45+
new command.Command(command.Name.GET_FEDCM_DIALOG_TYPE))
4146
}
4247

4348
accounts() {
44-
return this._driver.execute(new command.Command(command.Name.GET_ACCOUNTS))
49+
50+
const result = this._driver.execute(new command.Command(command.Name.GET_ACCOUNTS))
51+
52+
return result
4553
}
4654

4755
selectAccount(index) {
48-
return this._driver.execute(new command.Command(command.Name.SELECT_ACCOUNT).setParameter('accountIndex', index))
56+
return this._driver.execute(
57+
new command.Command(command.Name.SELECT_ACCOUNT).setParameter(
58+
'accountIndex', index))
4959
}
5060

5161
accept() {
52-
return this._driver.execute(new command.Command(command.Name.CLICK_DIALOG_BUTTON))
62+
return this._driver.execute(
63+
new command.Command(command.Name.CLICK_DIALOG_BUTTON))
5364
}
5465

5566
dismiss() {
5667
return this._driver.execute(new command.Command(command.Name.CANCEL_DIALOG))
5768
}
69+
}
70+
71+
module.exports = {
72+
Dialog
5873
}

javascript/node/selenium-webdriver/lib/http.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,17 @@ const W3C_COMMAND_MAP = new Map([
320320
[cmd.Name.GET_DOWNLOADABLE_FILES, get('/session/:sessionId/se/files')],
321321
[cmd.Name.DOWNLOAD_FILE, post(`/session/:sessionId/se/files`)],
322322
[cmd.Name.DELETE_DOWNLOADABLE_FILES, del(`/session/:sessionId/se/files`)],
323-
])
323+
// Federated Credential Management Command
324+
[cmd.Name.SET_DELAY_ENABLED, post(`/session/:sessionId/fedcm/setdelayenabled`)],
325+
[cmd.Name.CANCEL_DIALOG, post(`/session/:sessionId/fedcm/canceldialog`)],
326+
[cmd.Name.SELECT_ACCOUNT, post(`/session/:sessionId/fedcm/selectaccount`)],
327+
[cmd.Name.GET_FEDCM_TITLE, get(`/session/:sessionId/fedcm/gettitle`)],
328+
[cmd.Name.GET_FEDCM_DIALOG_TYPE , get('/session/:sessionId/fedcm/getdialogtype')],
329+
[cmd.Name.SET_DELAY_ENABLED,post(`/session/:sessionId/fedcm/setdelayenabled`)],
330+
[cmd.Name.RESET_COOLDOWN, post(`/session/:sessionId/fedcm/resetcooldown`)],
331+
[cmd.Name.CLICK_DIALOG_BUTTON, post(`/session/:sessionId/fedcm/clickdialogbutton`)],
332+
[cmd.Name.GET_ACCOUNTS, get(`/session/:sessionId/fedcm/accountlist`)]
333+
])
324334

325335
/**
326336
* Handles sending HTTP messages to a remote end.

javascript/node/selenium-webdriver/lib/test/fileserver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const Pages = (function () {
118118
addPage('emptyText', 'bidi/emptyText.txt')
119119
addPage('redirectedHttpEquiv', 'bidi/redirected_http_equiv.html')
120120
addPage('releaseAction', 'bidi/release_action.html')
121-
addPage('fedcm', '/fedcm/fedcm.html')
121+
addPage('fedcm', 'fedcm/fedcm_async_js.html')
122122
return pages
123123
})()
124124

javascript/node/selenium-webdriver/lib/webdriver.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const { PinnedScript } = require('./pinnedScript')
4545
const JSZip = require('jszip')
4646
const Script = require('./script')
4747
const Network = require('./network')
48+
const Dialog = require('./fedcm/dialog')
4849

4950
// Capability names that are defined in the W3C spec.
5051
const W3C_CAPABILITY_NAMES = new Set([
@@ -1115,7 +1116,7 @@ class WebDriver {
11151116
}
11161117

11171118
getFederalCredentialManagementDialog() {
1118-
return new FederalCredentialManagementDialog()
1119+
return new Dialog.Dialog(this)
11191120
}
11201121

11211122

javascript/node/selenium-webdriver/test/fedcm/fedcm_test.js

Lines changed: 154 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,166 @@
1818
'use strict'
1919

2020
const assert = require('node:assert')
21-
const { Browser } = require('selenium-webdriver')
22-
const { Pages, suite } = require('../../lib/test')
23-
const until = require('selenium-webdriver/lib/until')
21+
const {Browser} = require('selenium-webdriver')
22+
const {Pages, suite} = require('../../lib/test')
23+
const {By} = require("../../index");
2424

2525
suite(
26-
function (env) {
27-
let driver
26+
function (env) {
27+
let driver
2828

29-
beforeEach(async function () {
30-
driver = await env.builder().build()
31-
})
29+
beforeEach(async function () {
30+
driver = await env.builder().build()
31+
})
32+
33+
afterEach(async function () {
34+
await driver.quit()
35+
})
36+
37+
describe('Federated Credential Management Test', function () {
38+
it('credential mangement dialog should appear', async function () {
39+
await driver.get(Pages.fedcm)
40+
41+
let triggerButton = await driver.findElement(By.id("triggerButton"));
42+
await triggerButton.click();
43+
44+
let dialog
45+
46+
await driver.wait(
47+
async () => {
48+
try {
49+
dialog = await driver.getFederalCredentialManagementDialog()
50+
return await dialog.type() === 'AccountChooser'
51+
} catch (error) {
52+
return false
53+
}
54+
},
55+
10000,
56+
'Expected dialog type to be "AccountChooser"',
57+
2000
58+
)
59+
60+
assert.equal(await dialog.type(), 'AccountChooser')
61+
let title = await dialog.title()
62+
assert.equal(title.includes('Sign in to'), true)
63+
})
64+
65+
it('can dismiss dialog', async function () {
66+
await driver.get(Pages.fedcm)
67+
68+
let triggerButton = await driver.findElement(By.id("triggerButton"));
69+
await triggerButton.click();
70+
71+
let dialog = await driver.getFederalCredentialManagementDialog()
72+
73+
await driver.wait(
74+
async () => {
75+
try {
76+
return await dialog.type() === 'AccountChooser'
77+
} catch (error) {
78+
return false
79+
}
80+
},
81+
10000,
82+
'Expected dialog type to be "AccountChooser"',
83+
2000
84+
)
85+
86+
assert.equal(await dialog.type(), 'AccountChooser')
87+
let title = await dialog.title()
88+
assert.equal(title.includes('Sign in to'), true)
89+
90+
await dialog.dismiss()
91+
92+
try {
93+
await dialog.type()
94+
assert.fail("Above command should throw error")
95+
} catch (error) {
96+
assert.equal(error.message.includes('no such alert'), true)
97+
}
98+
})
99+
100+
it('can select account', async function () {
101+
await driver.get(Pages.fedcm)
102+
103+
let triggerButton = await driver.findElement(By.id("triggerButton"));
104+
await triggerButton.click();
105+
106+
let dialog = await driver.getFederalCredentialManagementDialog()
107+
108+
await driver.wait(
109+
async () => {
110+
try {
111+
return await dialog.type() === 'AccountChooser'
112+
} catch (error) {
113+
return false
114+
}
115+
},
116+
10000,
117+
'Expected dialog type to be "AccountChooser"',
118+
2000
119+
)
120+
121+
assert.equal(await dialog.type(), 'AccountChooser')
122+
let title = await dialog.title()
123+
assert.equal(title.includes('Sign in to'), true)
124+
125+
await dialog.selectAccount(1)
126+
})
127+
128+
it('can get account list', async function () {
129+
await driver.get(Pages.fedcm)
130+
131+
let triggerButton = await driver.findElement(By.id("triggerButton"));
132+
await triggerButton.click();
133+
134+
let dialog = await driver.getFederalCredentialManagementDialog()
135+
136+
await driver.wait(
137+
async () => {
138+
try {
139+
return await dialog.type() === 'AccountChooser'
140+
} catch (error) {
141+
return false
142+
}
143+
},
144+
10000,
145+
'Expected dialog type to be "AccountChooser"',
146+
2000
147+
)
148+
149+
assert.equal(await dialog.type(), 'AccountChooser')
150+
let title = await dialog.title()
151+
assert.equal(title.includes('Sign in to'), true)
152+
153+
const accounts = await dialog.accounts()
32154

33-
afterEach(async function () {
34-
await driver.quit()
35-
})
155+
assert.equal(accounts.length, 2)
36156

37-
describe('Federated Credential Management Tess', function () {
38-
it('dailog test', async function () {
157+
const account1 = accounts[0]
158+
const account2 = accounts[1]
39159

40-
await driver.get(Pages.fedcm)
160+
assert.strictEqual(account1.name, "John Doe");
161+
assert.strictEqual(account1.email, "[email protected]");
162+
assert.strictEqual(account1.accountId, "1234");
163+
assert.strictEqual(account1.givenName, "John");
164+
assert(account1.idpConfigUrl.includes("/fedcm/config.json"), true);
165+
assert.strictEqual(account1.pictureUrl, "https://idp.example/profile/123");
166+
assert.strictEqual(account1.loginState, "SignUp");
167+
assert.strictEqual(account1.termsOfServiceUrl, "https://rp.example/terms_of_service.html");
168+
assert.strictEqual(account1.privacyPolicyUrl, "https://rp.example/privacy_policy.html");
41169

170+
assert.strictEqual(account2.name, "Aisha Ahmad");
171+
assert.strictEqual(account2.email, "[email protected]");
172+
assert.strictEqual(account2.accountId, "5678");
173+
assert.strictEqual(account2.givenName, "Aisha");
174+
assert(account2.idpConfigUrl.includes("/fedcm/config.json"), true);
175+
assert.strictEqual(account2.pictureUrl, "https://idp.example/profile/567");
176+
assert.strictEqual(account2.loginState, "SignUp");
177+
assert.strictEqual(account2.termsOfServiceUrl, "https://rp.example/terms_of_service.html");
178+
assert.strictEqual(account2.privacyPolicyUrl, "https://rp.example/privacy_policy.html");
179+
})
42180
})
43-
})
44-
},
45-
{ browsers: [Browser.CHROME] },
181+
},
182+
{browsers: [Browser.CHROME]},
46183
)

0 commit comments

Comments
 (0)