From 9b6aca1dba7790d1103494787ec4803826f800ce Mon Sep 17 00:00:00 2001 From: Frederik Rothenberger Date: Thu, 26 Oct 2023 13:19:49 +0200 Subject: [PATCH] Simplify selenium clipboard interactions This PR simplifies the clipboard reading code and introduces a check to make sure it actually works. This avoids test failing later when the recovery phrase should be used. --- src/frontend/src/test-e2e/util.ts | 1 + src/frontend/src/test-e2e/views.ts | 40 +++++++++--------------------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/frontend/src/test-e2e/util.ts b/src/frontend/src/test-e2e/util.ts index 603fbdc748..8ef190cecb 100644 --- a/src/frontend/src/test-e2e/util.ts +++ b/src/frontend/src/test-e2e/util.ts @@ -36,6 +36,7 @@ export async function runInBrowser( args: [ "--ignore-certificate-errors", // allow self-signed certificates "--disable-gpu", + "--headless", ...(nonNullish(userAgent) ? [`--user-agent=${userAgent}`] : []), ], diff --git a/src/frontend/src/test-e2e/views.ts b/src/frontend/src/test-e2e/views.ts index 401aa10399..58e9792ded 100644 --- a/src/frontend/src/test-e2e/views.ts +++ b/src/frontend/src/test-e2e/views.ts @@ -183,37 +183,21 @@ export class RecoveryMethodSelectorView extends View { } async getSeedPhrase(): Promise { - // This tries to read the recovery phrase by first copying it to the clipboard. + // Allow reading and writing clipboard (e.g. for recovery phrase copy). + // We do this here rather than when the browser is created, because permissions + // are granted for the current origin only. + await this.browser.setPermissions({ name: "clipboard-read" }, "granted"); + await this.browser.setPermissions({ name: "clipboard-write" }, "granted"); + // Writes the recovery phrase to the clipboard await this.copySeedPhrase(); - // Our CSP policy prevents us from directly reading the clipboard. - // Instead, we mock user input to paste the clipboard content in textarea element and - // read the element's value. - - // First, create a new textarea element where the phrase will be pasted - await this.browser.execute(() => { - const elem = document.createElement("textarea"); - elem.setAttribute("id", "my-paste-area"); - document.body.prepend(elem); - }); - - // Select the element and mock "Ctrl + V" for pasting the clipboard content into said element - await this.browser.$("#my-paste-area").click(); - await this.browser.keys(["Control", "v"]); - - // Read the element's value and clean up - const seedPhrase = await this.browser.execute(() => { - const elem = document.querySelector( - "#my-paste-area" - ) as HTMLTextAreaElement; - // NOTE: we could also query the value with wdio's $(..).getValue(), but since we have - // the element here might as well. - const seedPhrase = elem.value!; - elem.remove(); - return seedPhrase; - }); - + // Read the value + const seedPhrase = await this.browser.execute(() => + navigator.clipboard.readText() + ); + // immediately fail, if it did not work + assert(seedPhrase.length > 0, "Seed phrase is empty!"); return seedPhrase; }