Skip to content

Commit

Permalink
Simplify selenium clipboard interactions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Frederik Rothenberger committed Oct 26, 2023
1 parent bcf5bfa commit 9b6aca1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/frontend/src/test-e2e/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function runInBrowser(
args: [
"--ignore-certificate-errors", // allow self-signed certificates
"--disable-gpu",
"--headless",
...(nonNullish(userAgent) ? [`--user-agent=${userAgent}`] : []),
],

Expand Down
40 changes: 12 additions & 28 deletions src/frontend/src/test-e2e/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,37 +183,21 @@ export class RecoveryMethodSelectorView extends View {
}

async getSeedPhrase(): Promise<string> {
// 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;
}

Expand Down

0 comments on commit 9b6aca1

Please sign in to comment.