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 8040f9c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
4 changes: 4 additions & 0 deletions src/frontend/src/test-e2e/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 7 additions & 29 deletions src/frontend/src/test-e2e/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,37 +183,15 @@ export class RecoveryMethodSelectorView extends View {
}

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

Expand Down

0 comments on commit 8040f9c

Please sign in to comment.