Skip to content

Commit

Permalink
Refactor onboarding with JSON
Browse files Browse the repository at this point in the history
We've pulled some of the code relating to JSON onboarding reused in several
tests to a `WalletPageHelper`'s `onboardWithJSON` method. Now the onboarding
with JSON in e2e tests requires just one command:
`await walletPageHelper.onboardWithJSON(<account>)`,
where `account` can be either `account1`, `account2`, or `custom`.
The `account1`'s and `account2`'s JSON data (body & password) is accessed via
the `onboarding.ts` file. The `custom` option allows for onboarding wit a custom
account and requires providing the `customJsonBody` and `customFilePassword`
parameters.
  • Loading branch information
michalinacienciala committed Aug 15, 2023
1 parent 1265763 commit 5f45f7f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 124 deletions.
70 changes: 15 additions & 55 deletions e2e-tests/fork-based/transactions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
import fs from "fs"
import { test, expect } from "../utils"
import { account2Name } from "../utils/onboarding"

test.describe("Transactions", () => {
test.beforeAll(async () => {
/**
* Create a JSON file with an encoded private key based on the file
* content passed from an environment variable. The further steps of
* the tests assume that the file encodes the pk of the `testertesting.eth`
* account. The JSON file can be generated using a script
* `scripts/key-generation/export-key-as-json.js`.
*/
const jsonBody = process.env.TEST_WALLET_JSON_BODY
if (jsonBody) {
fs.writeFileSync("./e2e-tests/utils/JSON.json", jsonBody)
} else {
throw new Error(
"TEST_WALLET_JSON_BODY environment variable is not defined."
)
}
})

test("User can send base asset", async ({
page: popup,
walletPageHelper,
Expand All @@ -29,19 +10,12 @@ test.describe("Transactions", () => {
}) => {
await test.step("Import account", async () => {
/**
* Onboard using JSON file.
* Import the `testertesting.eth` account using onboarding with a JSON
* file.
*/
const jsonPassword = process.env.TEST_WALLET_JSON_PASSWORD
if (jsonPassword) {
await walletPageHelper.onboardWithJSON(
"./e2e-tests/utils/JSON.json",
jsonPassword
)
} else {
throw new Error(
"TEST_WALLET_JSON_PASSWORD environment variable is not defined."
)
}
await walletPageHelper.onboardWithJSON("account2")
await walletPageHelper.goToStartPage()
await walletPageHelper.setViewportSize()

/**
* Verify we're on Ethereum network. Verify common elements on the main
Expand Down Expand Up @@ -212,19 +186,12 @@ test.describe("Transactions", () => {
}) => {
await test.step("Import account", async () => {
/**
* Onboard using JSON file.
* Import the `testertesting.eth` account using onboarding with a JSON
* file.
*/
const jsonPassword = process.env.TEST_WALLET_JSON_PASSWORD
if (jsonPassword) {
await walletPageHelper.onboardWithJSON(
"./e2e-tests/utils/JSON.json",
jsonPassword
)
} else {
throw new Error(
"TEST_WALLET_JSON_PASSWORD environment variable is not defined."
)
}
await walletPageHelper.onboardWithJSON("account2")
await walletPageHelper.goToStartPage()
await walletPageHelper.setViewportSize()

/**
* Verify we're on Ethereum network. Verify common elements on the main
Expand Down Expand Up @@ -360,19 +327,12 @@ test.describe("Transactions", () => {
}) => {
await test.step("Import account", async () => {
/**
* Onboard using JSON file.
* Import the `testertesting.eth` account using onboarding with a JSON
* file.
*/
const jsonPassword = process.env.TEST_WALLET_JSON_PASSWORD
if (jsonPassword) {
await walletPageHelper.onboardWithJSON(
"./e2e-tests/utils/JSON.json",
jsonPassword
)
} else {
throw new Error(
"TEST_WALLET_JSON_PASSWORD environment variable is not defined."
)
}
await walletPageHelper.onboardWithJSON("account2")
await walletPageHelper.goToStartPage()
await walletPageHelper.setViewportSize()

/**
* Verify we're on Ethereum network. Verify common elements on the main
Expand Down
35 changes: 4 additions & 31 deletions e2e-tests/regular/token-trust.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from "fs"
import { test, expect } from "../utils"
import { account1Name } from "../utils/onboarding"

Expand All @@ -21,36 +20,10 @@ test.describe("Token Trust", () => {
}) => {
await test.step("Import account and add addresses", async () => {
/**
* Create a JSON file with an encoded private key based on the file
* content passed from an environment variable. The further steps of
* the test assume that the file encodes the pk of the
* `e2e.testertesting.eth` account. The JSON file can be generated using
* the script `scripts/key-generation/export-key-as-json.js`.
*/
const jsonBody = process.env.E2E_TEST_ONLY_WALLET_JSON_BODY
if (jsonBody) {
fs.writeFileSync("./e2e-tests/utils/JSON.json", jsonBody)
} else {
throw new Error(
"E2E_TEST_ONLY_WALLET_JSON_BODY environment variable is not defined."
)
}

/**
* Onboard using JSON file.
*/
const jsonPassword = process.env.E2E_TEST_ONLY_WALLET_JSON_PASSWORD
if (jsonPassword) {
await walletPageHelper.onboardWithJSON(
"./e2e-tests/utils/JSON.json",
jsonPassword
)
} else {
throw new Error(
"E2E_TEST_ONLY_WALLET_JSON_PASSWORD environment variable is not defined."
)
}

* Import the `e2e.testertesting.eth` account using onboarding with a JSON
* file.
*/
await walletPageHelper.onboardWithJSON("account1")
await walletPageHelper.goToStartPage()
await walletPageHelper.setViewportSize()

Expand Down
34 changes: 5 additions & 29 deletions e2e-tests/regular/transactions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from "fs"
import { test, expect } from "../utils"
import { account2Address, account2Name } from "../utils/onboarding"

Expand All @@ -11,35 +10,12 @@ test.describe("Transactions", () => {
}) => {
await test.step("Import account", async () => {
/**
* Create a JSON file with an encoded private key based on the file
* content passed from an environment variable. The further steps of
* the test assume that the file encodes the pk of the `testertesting.eth`
* account. The JSON file can be generated using a script
* `scripts/key-generation/export-key-as-json.js`.
* Import the `testertesting.eth` account using onboarding with a JSON
* file.
*/
const jsonBody = process.env.TEST_WALLET_JSON_BODY
if (jsonBody) {
fs.writeFileSync("./e2e-tests/utils/JSON.json", jsonBody)
} else {
throw new Error(
"TEST_WALLET_JSON_BODY environment variable is not defined."
)
}

/**
* Onboard using JSON file.
*/
const jsonPassword = process.env.TEST_WALLET_JSON_PASSWORD
if (jsonPassword) {
await walletPageHelper.onboardWithJSON(
"./e2e-tests/utils/JSON.json",
jsonPassword
)
} else {
throw new Error(
"TEST_WALLET_JSON_PASSWORD environment variable is not defined."
)
}
await walletPageHelper.onboardWithJSON("account2")
await walletPageHelper.goToStartPage()
await walletPageHelper.setViewportSize()

/**
* Verify we're on Ethereum network. Verify common elements on the main page.
Expand Down
5 changes: 5 additions & 0 deletions e2e-tests/utils/onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ const DEFAULT_PASSWORD = "12345678"
// It owns some NFTs/badges.
export const account1Address = "0x6f1b1f1feb01235e15a7962f16c389c7f8218ed6"
export const account1Name = /^e2e\.testertesting\.eth$/
export const account1JsonBody = process.env.E2E_TEST_ONLY_WALLET_JSON_BODY
export const account1JsonPassword =
process.env.E2E_TEST_ONLY_WALLET_JSON_PASSWORD
// The account2 is the he testertesting.eth account. It's used for manual
// testing, so it's balance may fluctuate. It can be used to test features that
// don't depend on the constant balance or state of the assets.
export const account2Address = "0x0581470a8b62bd35dbf121a6329d43e7edd20fc7"
export const account2Name = /^testertesting\.eth$/
export const account2JsonBody = process.env.TEST_WALLET_JSON_BODY
export const account2JsonPassword = process.env.TEST_WALLET_JSON_PASSWORD

export default class OnboardingHelper {
constructor(
Expand Down
64 changes: 55 additions & 9 deletions e2e-tests/utils/walletPageHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import fs from "fs"
import { Page, BrowserContext, expect } from "@playwright/test"
import OnboardingHelper, { getOnboardingPage } from "./onboarding"
import OnboardingHelper, {
account1JsonBody,
account1JsonPassword,
account2JsonBody,
account2JsonPassword,
getOnboardingPage,
} from "./onboarding"

export default class WalletPageHelper {
readonly url: string
Expand Down Expand Up @@ -53,15 +60,54 @@ export default class WalletPageHelper {
/**
* Onboard using JSON with password-encrypted private key
*/
async onboardWithJSON(file: string, filePassword: string): Promise<void> {
async onboardWithJSON(
account: "account1" | "account2" | "custom",
customJsonBody?: string,
customFilePassword?: string
): Promise<void> {
let jsonBody: string | undefined
let jsonPassword: string | undefined
if (account === "account1") {
jsonBody = account1JsonBody
jsonPassword = account1JsonPassword
} else if (account === "account2") {
jsonBody = account2JsonBody
jsonPassword = account2JsonPassword
} else if (account === "custom") {
jsonBody = customJsonBody
jsonPassword = customFilePassword
} else {
throw new Error(`Invalid account: ${account}`)
}

/**
* Create JSON file.
*/
const filePath = "./e2e-tests/utils/JSON-tmp.json"
if (jsonBody) {
fs.writeFileSync(filePath, jsonBody)
} else {
throw new Error("`jsonBody` not defined.")
}

/**
* Onboard using JSON file.
*/
const onboardingPage = await getOnboardingPage(this.context)
await this.onboarding.addAccountFromJSON({
file,
filePassword,
onboardingPage,
})
await this.setViewportSize()
await this.goToStartPage()
if (jsonPassword) {
await this.onboarding.addAccountFromJSON({
file: filePath,
filePassword: jsonPassword,
onboardingPage,
})
} else {
throw new Error("`jsonPassword` not defined.")
}

/**
* Remove the previously created JSON file.
*/
fs.unlinkSync(filePath)
}

async assertTopWrap(network: RegExp, accountLabel: RegExp): Promise<void> {
Expand Down

0 comments on commit 5f45f7f

Please sign in to comment.