-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(e2e): Solana Phantom Wallet (#433)
- Loading branch information
Showing
10 changed files
with
3,848 additions
and
134 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
e2e-tests/runner/examples/connectors/SolanaConnector/SolanaConnector.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { | ||
expect, | ||
getButtonByText, | ||
getByAriaLabel, | ||
} from '@fuels/playwright-utils'; | ||
import type { Page } from '@playwright/test'; | ||
import phantom from '../../../node_modules/@phantom/synpress/commands/phantom'; | ||
import { test } from './setup'; | ||
|
||
phantom.confirmTransaction = async () => { | ||
const notificationPage = | ||
await phantom.playwright.switchToNotification('phantom'); | ||
await phantom.playwright.waitAndClick( | ||
'phantom', | ||
phantom.transactionPageElements.buttons.confirmTransaction, // Ensure this locator exists or define it | ||
notificationPage, | ||
{ waitForEvent: 'close' }, | ||
); | ||
return true; | ||
}; | ||
|
||
test.describe('SolanaConnector', () => { | ||
test.slow(); | ||
|
||
const connect = async (page: Page) => { | ||
await page.goto('/'); | ||
const connectButton = getButtonByText(page, 'Connect Wallet', true); | ||
await connectButton.click(); | ||
await getByAriaLabel(page, 'Connect to Solana Wallets', true).click(); | ||
await page.getByText('Proceed anyway').click(); | ||
await getButtonByText(page, 'Phantom').click(); | ||
await phantom.acceptAccess(); | ||
await page.waitForTimeout(3000); | ||
}; | ||
|
||
test('Fuel tests', async ({ page }) => { | ||
await connect(page); | ||
const addressElement = await page.locator('#address'); | ||
let address = null; | ||
if (addressElement) { | ||
address = await addressElement.getAttribute('data-address'); | ||
} | ||
test.step('Check if address is not null', () => { | ||
expect(address).not.toBeNull(); | ||
}); | ||
}); | ||
}); |
4 changes: 4 additions & 0 deletions
4
e2e-tests/runner/examples/connectors/SolanaConnector/phantom/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const PHANTOM_CONFIG = { | ||
MNEMONIC: 'test test test test test test test test test test test junk', | ||
WALLET_PASSWORD: 'Tester@1234', | ||
}; |
2 changes: 2 additions & 0 deletions
2
e2e-tests/runner/examples/connectors/SolanaConnector/phantom/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './setup'; | ||
export * from './config'; |
18 changes: 18 additions & 0 deletions
18
e2e-tests/runner/examples/connectors/SolanaConnector/phantom/setup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { BrowserContext } from '@playwright/test'; | ||
import { chromium } from '@playwright/test'; | ||
import phantomCommands from '../../../../node_modules/@phantom/synpress/commands/phantom'; | ||
import phantomHelpers from '../../../../node_modules/@phantom/synpress/helpers'; | ||
import { PHANTOM_CONFIG } from './config'; | ||
|
||
export async function phantomPath() { | ||
return await phantomHelpers.prepareProvider('phantom', 'latest'); | ||
} | ||
export async function setupPhantom(_context: BrowserContext) { | ||
await phantomCommands.initialSetup(chromium, { | ||
secretWordsOrPrivateKey: PHANTOM_CONFIG.MNEMONIC, | ||
network: 'localhost', | ||
password: PHANTOM_CONFIG.WALLET_PASSWORD, | ||
enableAdvancedSettings: true, | ||
enableExperimentalSettings: false, | ||
}); | ||
} |
37 changes: 37 additions & 0 deletions
37
e2e-tests/runner/examples/connectors/SolanaConnector/setup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { BrowserContext } from '@playwright/test'; | ||
import { test as base, chromium } from '@playwright/test'; | ||
import { phantomPath, setupPhantom } from './phantom'; | ||
|
||
import { getExtensionsData } from './utils/getExtensionsData'; | ||
import { waitForExtensions } from './utils/waitForExtensions'; | ||
|
||
export const test = base.extend<{ | ||
context: BrowserContext; | ||
extensionId: string; | ||
}>({ | ||
context: async ({ context: _ }, use) => { | ||
global.expect = expect; | ||
const path = await phantomPath(); | ||
const browserArgs = [ | ||
`--disable-extensions-except=${path}`, | ||
`--load-extension=${path}`, | ||
'--remote-debugging-port=9222', | ||
]; | ||
const context = await chromium.launchPersistentContext('', { | ||
headless: false, | ||
args: browserArgs, | ||
}); | ||
const extensions = await getExtensionsData(context); | ||
await waitForExtensions(context, extensions); | ||
await setupPhantom(); | ||
await use(context); | ||
}, | ||
extensionId: async ({ context }, use) => { | ||
let [background] = context.serviceWorkers(); | ||
if (!background) background = await context.waitForEvent('serviceworker'); | ||
const extensionId = background.url().split('/')[2]; | ||
await use(extensionId); | ||
}, | ||
}); | ||
|
||
export const expect = test.expect; |
52 changes: 52 additions & 0 deletions
52
e2e-tests/runner/examples/connectors/SolanaConnector/utils/getExtensionsData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* This code is required to avoid synpress to start without | ||
* the extensions be ready making it fail. | ||
* | ||
* This code was copied from the synpress codebase to be used as a standalone function. | ||
* https://github.com/Synthetixio/synpress/blob/81faa920fb683b1b579fde5214f923c758877157/commands/playwright.js#L445 | ||
*/ | ||
|
||
import type { BrowserContext } from '@playwright/test'; | ||
|
||
export async function getExtensionsData(context: BrowserContext) { | ||
const extensionsData = {}; | ||
const page = await context.newPage(); | ||
|
||
await page.goto('chrome://extensions'); | ||
await page.waitForLoadState('load'); | ||
await page.waitForLoadState('domcontentloaded'); | ||
|
||
const devModeButton = page.locator('#devMode'); | ||
await devModeButton.waitFor(); | ||
await devModeButton.focus(); | ||
await devModeButton.click(); | ||
|
||
const extensionDataItems = await page.locator('extensions-item').all(); | ||
for (const extensionData of extensionDataItems) { | ||
const extensionName = ( | ||
await extensionData | ||
.locator('#name-and-version') | ||
.locator('#name') | ||
.textContent() | ||
).toLowerCase(); | ||
|
||
const extensionVersion = ( | ||
await extensionData | ||
.locator('#name-and-version') | ||
.locator('#version') | ||
.textContent() | ||
).replace(/(\n| )/g, ''); | ||
|
||
const extensionId = ( | ||
await extensionData.locator('#extension-id').textContent() | ||
).replace('ID: ', ''); | ||
|
||
extensionsData[extensionName] = { | ||
version: extensionVersion, | ||
id: extensionId, | ||
}; | ||
} | ||
await page.close(); | ||
|
||
return extensionsData; | ||
} |
40 changes: 40 additions & 0 deletions
40
e2e-tests/runner/examples/connectors/SolanaConnector/utils/waitForExtensions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { setTimeout } from 'node:timers/promises'; | ||
import type { BrowserContext } from '@playwright/test'; | ||
|
||
export async function waitForExtensions( | ||
context: BrowserContext, | ||
extensions: Record< | ||
string, | ||
{ | ||
id: string; | ||
version: string; | ||
} | ||
>, | ||
maxAttempts = 5, | ||
attempt = 0, | ||
): Promise<boolean> { | ||
if (!context) throw new Error('BrowserContext is required.'); | ||
if (!extensions || typeof extensions !== 'object') { | ||
throw new Error('Invalid extensions object provided.'); | ||
} | ||
console.log(`Checking extensions (Attempt ${attempt + 1}/${maxAttempts})...`); | ||
const pages = context.pages(); | ||
if (!pages.length) { | ||
console.warn('No pages found in the context. Retrying...'); | ||
} | ||
const phantomPage = pages.find((page) => | ||
page.url().includes(extensions.phantom?.id), | ||
); | ||
if (phantomPage) { | ||
console.log('Phantom extension is ready!'); | ||
return true; | ||
} | ||
if (attempt >= maxAttempts - 1) { | ||
throw new Error( | ||
`Failed to detect extensions after ${maxAttempts} attempts.`, | ||
); | ||
} | ||
console.log('Phantom extension not found. Retrying in 3 seconds...'); | ||
await setTimeout(3000); | ||
return waitForExtensions(context, extensions, maxAttempts, attempt + 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.