From 8040f9c19a95b7cba808cf8768accc547c3a1171 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 | 4 ++++ src/frontend/src/test-e2e/views.ts | 36 ++++++------------------------ 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/src/frontend/src/test-e2e/util.ts b/src/frontend/src/test-e2e/util.ts index 603fbdc748..d48d8e25b1 100644 --- a/src/frontend/src/test-e2e/util.ts +++ b/src/frontend/src/test-e2e/util.ts @@ -75,6 +75,10 @@ export async function runInBrowser( // setup test suite await addCustomCommands(browser); + // allow reading and writing clipboard (e.g. for recovery phrase copy) + await browser.setPermissions({ name: "clipboard-read" }, "granted"); + await browser.setPermissions({ name: "clipboard-write" }, "granted"); + try { // run test await test(browser, runConfig); diff --git a/src/frontend/src/test-e2e/views.ts b/src/frontend/src/test-e2e/views.ts index 401aa10399..4f405b4a2a 100644 --- a/src/frontend/src/test-e2e/views.ts +++ b/src/frontend/src/test-e2e/views.ts @@ -183,37 +183,15 @@ export class RecoveryMethodSelectorView extends View { } async getSeedPhrase(): Promise { - // This tries to read the recovery phrase by first copying it to the clipboard. - + // This 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; - }); - + // This 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; }