Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(webkit): polyfill `PublicKeyCredential` (#35702)

Simon Knott71eb3b9f0fc6
diff --git a/packages/playwright-core/src/server/webkit/wkPage.ts b/packages/playwright-core/src/server/webkit/wkPage.ts
index 2482f369a4..cbaf7b57fd 100644
--- a/packages/playwright-core/src/server/webkit/wkPage.ts
+++ b/packages/playwright-core/src/server/webkit/wkPage.ts
@@ -781,10 +781,33 @@ export class WKPage implements PageDelegate {
}
scripts.push('if (!window.safari) window.safari = { pushNotification: { toString() { return "[object SafariRemoteNotification]"; } } };');
scripts.push('if (!window.GestureEvent) window.GestureEvent = function GestureEvent() {};');
+ scripts.push(this._publicKeyCredentialScript());
scripts.push(...this._page.allInitScripts().map(script => script.source));
return scripts.join(';\n');
}
+ private _publicKeyCredentialScript(): string {
+ function polyfill() {
+ /**
+ * Some sites don't check existance of PublicKeyCredentials because all browsers except Webkit on Linux implement it.
+ * We polyfill the subset that's used for feature detection, so that login flows that'd work in Safari don't crash with "PublicKeyCredential is not defined" in CI.
+ * https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredential
+ */
+ window.PublicKeyCredential ??= {
+ async getClientCapabilities() {
+ return {};
+ },
+ async isConditionalMediationAvailable() {
+ return false;
+ },
+ async isUserVerifyingPlatformAuthenticatorAvailable() {
+ return false;
+ },
+ } as any;
+ }
+ return `(${polyfill.toString()})();`;
+ }
+
async _updateBootstrapScript(): Promise<void> {
await this._updateState('Page.setBootstrapScript', { source: this._calculateBootstrapScript() });
}
diff --git a/tests/library/capabilities.spec.ts b/tests/library/capabilities.spec.ts
index 8b065834ab..23cb4ac399 100644
--- a/tests/library/capabilities.spec.ts
+++ b/tests/library/capabilities.spec.ts
@@ -480,3 +480,17 @@ it('should not auto play audio', {
await page.goto('http://127.0.0.1/audio.html');
await expect(page.locator('#log')).toHaveText('State: suspended');
});
+
+it('should not crash on feature detection for PublicKeyCredential', {
+ annotation: {
+ type: 'issue',
+ description: 'https://github.com/microsoft/playwright/issues/35433'
+ }
+}, async ({ page, server }) => {
+ await page.goto(server.EMPTY_PAGE);
+ await page.evaluate(async () => {
+ await PublicKeyCredential.getClientCapabilities();
+ await PublicKeyCredential.isConditionalMediationAvailable();
+ await PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable();
+ });
+});
diff --git a/tests/library/modernizr.spec.ts b/tests/library/modernizr.spec.ts
index 8777b3f7db..3c4edb141e 100644
--- a/tests/library/modernizr.spec.ts
+++ b/tests/library/modernizr.spec.ts
@@ -55,7 +55,6 @@ it('Safari Desktop', async ({ browser, browserName, platform, server, headless }
if (platform === 'linux') {
expected.speechrecognition = false;
- expected.publickeycredential = false;
expected.mediastream = false;
if (headless)
expected.todataurlwebp = true;
@@ -118,7 +117,6 @@ it('Mobile Safari', async ({ playwright, browser, browserName, platform, server,
if (platform === 'linux') {
expected.speechrecognition = false;
- expected.publickeycredential = false;
expected.mediastream = false;
if (headless)
expected.todataurlwebp = true;