Skip to content

Commit

Permalink
fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Seroxdesign committed Jul 4, 2024
1 parent e7fb082 commit f9b211e
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 13 deletions.
25 changes: 14 additions & 11 deletions wallets/phantom/src/PhantomWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { type BrowserContext, type Page } from '@playwright/test'
import { HomePage } from './pages/HomePage/page'
import { LockPage } from './pages/LockPage/page'
import { NotificationPage } from './pages/NotificationPage/page'
import { SettingsPage } from './pages/SettingsPage/page'
import { ConfirmationPage } from './pages/ConfirmationPage/page'

export class KeplrWallet {
export class PhantomWallet {
readonly lockPage: LockPage
readonly homePage: HomePage
readonly notificationPage: NotificationPage
readonly settingsPage: SettingsPage
readonly confirmationPage: ConfirmationPage

constructor(
readonly page: Page,
Expand All @@ -17,6 +21,8 @@ export class KeplrWallet {
this.lockPage = new LockPage(page)
this.homePage = new HomePage(page)
this.notificationPage = new NotificationPage(page)
this.settingsPage = new SettingsPage(page)
this.confirmationPage = new ConfirmationPage(page)
}
/**
* Does initial setup for the wallet.
Expand All @@ -26,33 +32,30 @@ export class KeplrWallet {
* @param password. The password to set.
*/
async setupWallet({ secretWordsOrPrivateKey, password }: { secretWordsOrPrivateKey: string; password: string }) {
const wallet = await this.lockPage.importWallet(secretWordsOrPrivateKey, password)
return wallet
console.log(secretWordsOrPrivateKey, password)
}

async createWallet(password: string) {
const wallet = await this.lockPage.createWallet(password)
return wallet
console.log(password)
}

async getWalletAddress(wallet: string) {
const walletAddress = await this.homePage.getWalletAddress(wallet)
return walletAddress
console.log(wallet)
}

async addNewTokensFound() {
return await this.homePage.addNewTokensFound()
console.log('')
}

async disconnectWalletFromDapps() {
return await this.homePage.disconnectWalletFromDapps()
console.log('')
}

async acceptAccess() {
return await this.notificationPage.acceptAccess()
console.log('')
}

async rejectAccess() {
return await this.notificationPage.rejectAccess()
console.log('')
}
}
105 changes: 105 additions & 0 deletions wallets/phantom/src/fixtures/phantomFixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import path from 'node:path'
import { type Page, chromium } from '@playwright/test'

import { test as base } from '@playwright/test'
import {
CACHE_DIR_NAME,
createTempContextDir,
defineWalletSetup,
removeTempContextDir
} from '@synthetixio/synpress-cache'
import { prepareExtension } from '@synthetixio/synpress-cache'
import fs from 'fs-extra'
import { PhantomWallet } from '../PhantomWallet'
import { getExtensionId } from '../fixtureActions'
import { persistLocalStorage } from '../fixtureActions/persistLocalStorage'
// import unlockForFixtures from '../fixtureActions/unlockForFixtures'
import { PASSWORD } from '../utils'

type PhantomFixtures = {
_contextPath: string
phantom: PhantomWallet
phantomPage: Page
extensionId: string
}

let _phantomPage: Page

export const phantomFixtures = (walletSetup: ReturnType<typeof defineWalletSetup>, slowMo = 0) => {
return base.extend<PhantomFixtures>({
_contextPath: async ({ browserName }, use, testInfo) => {
const contextDir = await createTempContextDir(browserName, testInfo.testId)

await use(contextDir)

const error = await removeTempContextDir(contextDir)
if (error) {
console.log('contextDir', error)
}
},
context: async ({ context: currentContext, _contextPath }, use) => {
// @todo: This is some weird behaviour, if there is only 1 setup file the hash function will always produce a different hash than cache.
const cacheDirPath = path.join(process.cwd(), CACHE_DIR_NAME, walletSetup.hash)
if (!(await fs.exists(cacheDirPath))) {
throw new Error(`Cache for ${cacheDirPath} does not exist. Create it first!`)
}

// Copying the cache to the temporary context directory.
await fs.copy(cacheDirPath, _contextPath)

const phantomPath = await prepareExtension('Phantom')
// We don't need the `--load-extension` arg since the extension is already loaded in the cache.
const browserArgs = [`--disable-extensions-except=${phantomPath}`, '--enable-features=SharedClipboardUI']
if (process.env.HEADLESS) {
browserArgs.push('--headless=new')

if (slowMo) {
console.warn('[WARNING] Slow motion makes no sense in headless mode. It will be ignored!')
}
}

const context = await chromium.launchPersistentContext(_contextPath, {
headless: false,
args: browserArgs,
slowMo: process.env.HEADLESS ? 0 : slowMo
})

const { cookies, origins } = await currentContext.storageState()

if (cookies) {
await context.addCookies(cookies)
}
if (origins && origins.length > 0) {
await persistLocalStorage(origins, context)
}

const extensionId = await getExtensionId(context, 'Phantom')

_phantomPage = context.pages()[0] as Page

await _phantomPage.goto(`chrome-extension://${extensionId}/popup.html`)

// await unlockForFixtures(_phantomPage, PASSWORD)

await use(context)
},
phantomPage: async ({ context: _ }, use) => {
await use(_phantomPage)
},
extensionId: async ({ context }, use) => {
const extensionId = await getExtensionId(context, 'Keplr')

await use(extensionId)
},
phantom: async ({ context, extensionId }, use) => {
const phantomWallet = new PhantomWallet(_phantomPage, context, PASSWORD, extensionId)

await use(phantomWallet)
},

page: async ({ page }, use) => {
await page.goto('')
await use(page)
}
})
}
2 changes: 1 addition & 1 deletion wallets/phantom/src/pages/NotificationPage/page.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Page } from '@playwright/test'
import { notificationPageElements } from './selectors'

export class LockPage {
export class NotificationPage {
static readonly selectors = notificationPageElements
readonly selectors = notificationPageElements

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Page } from '@playwright/test'
import { settingsPageElements } from './selectors'

export class LockPage {
export class SettingsPage {
static readonly selectors = settingsPageElements
readonly selectors = settingsPageElements

Expand Down
Empty file.
1 change: 1 addition & 0 deletions wallets/phantom/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './selectors/loading'

0 comments on commit f9b211e

Please sign in to comment.