Skip to content

Commit

Permalink
loading cache into browser for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Seroxdesign committed Jun 14, 2024
1 parent acdf38c commit 68a6094
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 95 deletions.
7 changes: 5 additions & 2 deletions packages/cache/src/prepareExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import { downloadFile, ensureCacheDirExists, unzipArchive } from '.'
export const DEFAULT_METAMASK_VERSION = '11.9.1'
export const EXTENSION_DOWNLOAD_URL = `https://github.com/MetaMask/metamask-extension/releases/download/v${DEFAULT_METAMASK_VERSION}/metamask-chrome-${DEFAULT_METAMASK_VERSION}.zip`

export const DEFAULT_KEPLR_VERSION = '0.12.101'
export const EXTENSION_KEPLR_DOWNLOAD_URL = `https://github.com/chainapsis/keplr-wallet/releases/download/v${DEFAULT_KEPLR_VERSION}/keplr-extension-manifest-v2-v${DEFAULT_KEPLR_VERSION}.zip`;

// NOTE: This function is copied from `wallets/metamask/src/prepareExtension.ts` only TEMPORARILY!
export async function prepareExtension() {
const cacheDirPath = ensureCacheDirExists()

const downloadResult = await downloadFile({
url: EXTENSION_DOWNLOAD_URL,
url: EXTENSION_KEPLR_DOWNLOAD_URL,
outputDir: cacheDirPath,
fileName: `metamask-chrome-${DEFAULT_METAMASK_VERSION}.zip`
fileName: `metamask-chrome-${DEFAULT_KEPLR_VERSION}.zip`
})

const unzipResult = await unzipArchive({
Expand Down
1 change: 1 addition & 0 deletions wallets/keplr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
],
"scripts": {
"build": "pnpm run clean && pnpm run build:dist && pnpm run build:types",
"build:cache": "synpress-cache test/wallet-setup",
"build:dist": "tsup --tsconfig tsconfig.build.json",
"build:types": "tsc --emitDeclarationOnly --project tsconfig.build.json",
"clean": "rimraf dist types",
Expand Down
49 changes: 30 additions & 19 deletions wallets/keplr/src/KeplrWallet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { BrowserContext, Page } from '@playwright/test'
import { chromium, type BrowserContext, type Page } from '@playwright/test'
import { playwright } from './playwright-kepler'
import { onboardingElements } from './pages/LockPage/selectors/index'
import { notificationPageElements } from './pages/NotificationPage/selectors/index'
import { LockPage } from './pages/LockPage/page'

export class KeplrWallet {
seedPhrase = ''
Expand Down Expand Up @@ -52,73 +53,78 @@ export class KeplrWallet {
* @returns true if the wallet was imported successfully.
*/
async importWallet(secretWords: string, password: string) {
console.log('im', 1, playwright)
await playwright.waitAndClickByText(
onboardingElements.createWalletButton,
await playwright.keplrWindow(),
);
console.log('im', 2)
await playwright.waitAndClickByText(
onboardingElements.importRecoveryPhraseButton,
await playwright.keplrWindow(),
);
console.log('im', 3)
await playwright.waitAndClickByText(
onboardingElements.useRecoveryPhraseButton,
await playwright.keplrWindow(),
);
console.log('im', 4)
await playwright.waitAndClickByText(
onboardingElements.phraseCount24,
await playwright.keplrWindow(),
);

console.log('im', 5)
for (const [index, word] of secretWords.split(' ').entries()) {
await playwright.waitAndTypeByLocator(
onboardingElements.textAreaSelector,
word,
index,
);
}

console.log('im', 6)
await playwright.waitAndClick(
onboardingElements.submitPhraseButton,
await playwright.keplrWindow(),
);

console.log('im', 7)
await playwright.waitAndType(
onboardingElements.walletInput,
onboardingElements.walletName,
);
console.log('im', 8)
await playwright.waitAndType(onboardingElements.passwordInput, password);
await playwright.waitAndType(
onboardingElements.confirmPasswordInput,
password,
);

console.log('im', 9)
await playwright.waitAndClick(
onboardingElements.submitWalletDataButton,
await playwright.keplrWindow(),
{ number: 1 },
);

console.log('im', 10)
await playwright.waitForByText(
onboardingElements.phraseSelectChain,
await playwright.keplrWindow(),
);

console.log('im', 11)
await playwright.waitAndClick(
onboardingElements.submitChainButton,
await playwright.keplrWindow(),
);

console.log('im', 12)
await playwright.waitForByText(
onboardingElements.phraseAccountCreated,
await playwright.keplrWindow(),
);

console.log('im', 13)
await playwright.waitAndClick(
onboardingElements.finishButton,
await playwright.keplrWindow(),
{ dontWait: true },
);

console.log('im', 14)
return true;
}

Expand Down Expand Up @@ -160,16 +166,21 @@ export class KeplrWallet {
* @param password. The password to set.
*/
async setupWallet(
playwrightInstance: any,
page: any,
{ secretWordsOrPrivateKey, password }: { secretWordsOrPrivateKey: string; password: string },
) {
if (playwrightInstance) {
await playwright.init(playwrightInstance);
}

await playwright.assignWindows();
await playwright.assignActiveTabName('keplr');
await this.getExtensionDetails();
await this.importWallet(secretWordsOrPrivateKey, password);
console.log('in', 1)
const playwright = chromium
console.log('in', 2)
const lockpage = new LockPage(page)
console.log('in', 3, secretWordsOrPrivateKey, password, lockpage)
const wallet = await lockpage.unlock(secretWordsOrPrivateKey, password)
console.log('in', 4)
console.log('chromium', playwright,'wallet', wallet)
// await playwright.assignWindows();
// await playwright.assignActiveTabName('keplr');
// await this.getExtensionDetails();
// await this.importWallet(secretWordsOrPrivateKey, password);
return wallet;
}
}
2 changes: 1 addition & 1 deletion wallets/keplr/src/fixtureActions/getExtensionId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export async function getExtensionId(context: BrowserContext, extensionName: str
await page.goto('chrome://extensions')

const unparsedExtensions = await page.evaluate('chrome.management.getAll()')

const allExtensions = Extensions.parse(unparsedExtensions)
console.log('all', allExtensions)
const targetExtension = allExtensions.find(
(extension: any) => extension.name.toLowerCase() === extensionName.toLowerCase()
)
Expand Down
5 changes: 1 addition & 4 deletions wallets/keplr/src/fixtures/keplrFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
defineWalletSetup,
removeTempContextDir
} from '@synthetixio/synpress-cache'
import { type Anvil, type CreateAnvilOptions } from '@viem/anvil'
import fs from 'fs-extra'
import { persistLocalStorage } from '../fixtureActions/persistLocalStorage'
import { prepareExtension, getExtensionId, unlockForFixture } from '../fixtureActions'
Expand All @@ -20,8 +19,6 @@ type KeplrFixtures = {
keplr: KeplrWallet
extensionId: string
keplrPage: Page
createAnvilNode: (options?: CreateAnvilOptions) => Promise<{ anvil: Anvil; rpcUrl: string; chainId: number }>
connectToAnvil: () => Promise<void>
}

let _keplrPage: Page
Expand All @@ -45,7 +42,7 @@ export const keplrFixtures = (walletSetup: ReturnType<typeof defineWalletSetup>,
const keplrPath = await prepareExtension()
// We don't need the `--load-extension` arg since the extension is already loaded in the cache.
const browserArgs = [`--disable-extensions-except=${keplrPath}`]

console.log('keplrPath', keplrPath)
if (process.env.HEADLESS) {
browserArgs.push('--headless=new')

Expand Down
128 changes: 66 additions & 62 deletions wallets/keplr/src/pages/LockPage/actions/importWallet.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,77 @@
import { playwright } from "../../../playwright-kepler";
import { onboardingElements } from "../selectors";
// import { playwright } from "../../../playwright-kepler";
// import { onboardingElements } from "../selectors";
import type { Page } from "@playwright/test";

export async function importWallet(secretWords: string, password: string) {
await playwright.waitAndClickByText(
onboardingElements.createWalletButton,
await playwright.keplrWindow(),
);
await playwright.waitAndClickByText(
onboardingElements.importRecoveryPhraseButton,
await playwright.keplrWindow(),
);
await playwright.waitAndClickByText(
onboardingElements.useRecoveryPhraseButton,
await playwright.keplrWindow(),
);
await playwright.waitAndClickByText(
onboardingElements.phraseCount24,
await playwright.keplrWindow(),
);
export async function importWallet(page: Page, secretWords: string, password: string) {

for (const [index, word] of secretWords.split(' ').entries()) {
await playwright.waitAndTypeByLocator(
onboardingElements.textAreaSelector,
word,
index,
);
}
await page.waitForSelector('button.submit-button');
console.log(secretWords, password)

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

This logs sensitive data returned by
an access to password
as clear text.
This logs sensitive data returned by
an access to password
as clear text.
This logs sensitive data returned by
an access to password
as clear text.
// await playwright.waitAndClickByText(
// onboardingElements.createWalletButton,
// await playwright.keplrWindow(),
// );
// await playwright.waitAndClickByText(
// onboardingElements.importRecoveryPhraseButton,
// await playwright.keplrWindow(),
// );
// await playwright.waitAndClickByText(
// onboardingElements.useRecoveryPhraseButton,
// await playwright.keplrWindow(),
// );
// await playwright.waitAndClickByText(
// onboardingElements.phraseCount24,
// await playwright.keplrWindow(),
// );

await playwright.waitAndClick(
onboardingElements.submitPhraseButton,
await playwright.keplrWindow(),
);
// for (const [index, word] of secretWords.split(' ').entries()) {
// await playwright.waitAndTypeByLocator(
// onboardingElements.textAreaSelector,
// word,
// index,
// );
// }

await playwright.waitAndType(
onboardingElements.walletInput,
onboardingElements.walletName,
);
await playwright.waitAndType(onboardingElements.passwordInput, password);
await playwright.waitAndType(
onboardingElements.confirmPasswordInput,
password,
);
// await playwright.waitAndClick(
// onboardingElements.submitPhraseButton,
// await playwright.keplrWindow(),
// );

await playwright.waitAndClick(
onboardingElements.submitWalletDataButton,
await playwright.keplrWindow(),
{ number: 1 },
);
// await playwright.waitAndType(
// onboardingElements.walletInput,
// onboardingElements.walletName,
// );
// await playwright.waitAndType(onboardingElements.passwordInput, password);
// await playwright.waitAndType(
// onboardingElements.confirmPasswordInput,
// password,
// );

await playwright.waitForByText(
onboardingElements.phraseSelectChain,
await playwright.keplrWindow(),
);
// await playwright.waitAndClick(
// onboardingElements.submitWalletDataButton,
// await playwright.keplrWindow(),
// { number: 1 },
// );

await playwright.waitAndClick(
onboardingElements.submitChainButton,
await playwright.keplrWindow(),
);
// await playwright.waitForByText(
// onboardingElements.phraseSelectChain,
// await playwright.keplrWindow(),
// );

await playwright.waitForByText(
onboardingElements.phraseAccountCreated,
await playwright.keplrWindow(),
);
// await playwright.waitAndClick(
// onboardingElements.submitChainButton,
// await playwright.keplrWindow(),
// );

await playwright.waitAndClick(
onboardingElements.finishButton,
await playwright.keplrWindow(),
{ dontWait: true },
);
// await playwright.waitForByText(
// onboardingElements.phraseAccountCreated,
// await playwright.keplrWindow(),
// );

return true;
// await playwright.waitAndClick(
// onboardingElements.finishButton,
// await playwright.keplrWindow(),
// { dontWait: true },
// );

// return true;
}
5 changes: 2 additions & 3 deletions wallets/keplr/src/pages/LockPage/page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Page } from '@playwright/test'
import { onboardingElements } from './selectors'
import { importWallet } from './actions'
import { SEED_PHRASE } from '../../utils'

export class LockPage {
static readonly selectors = onboardingElements
Expand All @@ -13,7 +12,7 @@ export class LockPage {
this.page = page
}

async unlock(password: string) {
await importWallet(SEED_PHRASE, password)
async unlock(seedPhrase: string, password: string) {
await importWallet(this.page, seedPhrase, password)
}
}
2 changes: 1 addition & 1 deletion wallets/keplr/test/synpress.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { testWithSynpress } from "@synthetixio/synpress-core";
import { keplrFixtures } from "../src";
import connectedSetup from "./wallet-setup/connected.setup";
import connectedSetup from "./wallet-setup/connected-keplr.setup";

export default testWithSynpress(keplrFixtures(connectedSetup))
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { defineWalletSetup } from "@synthetixio/synpress-cache";
import { KeplrWallet } from "../../src";
import { getExtensionId } from "../../src/fixtureActions";
import { getExtensionId, prepareExtension } from "../../src/fixtureActions";

const SEED_PHRASE = 'orbit bench unit task food shock brand bracket domain regular warfare company announce wheel grape trust sphere boy doctor half guard ritual three ecology'
const PASSWORD = 'Test1234'

export default defineWalletSetup(PASSWORD, async (context, lockPage) => {
await prepareExtension()
console.log('Extension', context)
const extensionId = await getExtensionId(context, 'keplr')
const keplr = new KeplrWallet(lockPage, context, PASSWORD, extensionId)

await keplr.importWallet(SEED_PHRASE, PASSWORD)
console.log('SEEDP', SEED_PHRASE, PASSWORD)
const page = await context.newPage()
await page.goto(`chrome-extension://${extensionId}/home.html`)
const wallet = await keplr.setupWallet(page, { secretWordsOrPrivateKey: SEED_PHRASE, password: PASSWORD})
console.log('in here2', wallet)
// const page = await context.newPage()
// await page.close()
})

0 comments on commit 68a6094

Please sign in to comment.