Polkadot.js
Testing with Polkadot.js wallet extension
Polkadot.js is the official wallet extension for the Polkadot ecosystem. Chroma provides full support for automating Polkadot.js wallet interactions.
Setup
Create a test with Polkadot.js wallet using createWalletTest:
import { createWalletTest } from '@avalix/chroma'
const test = createWalletTest({
wallets: [{
type: 'polkadot-js'
}]
})
test('polkadot.js test', async ({ wallets }) => {
const polkadotJs = wallets['polkadot-js']
})Import Account
Import an account using a mnemonic seed phrase:
await polkadotJs.importMnemonic({
seed: 'bottom drive obey lake curtain smoke basket hold race lonely fit walk',
name: 'My Test Account', // optional
})Authorize Connection
When your dApp requests wallet access:
// Your dApp triggers a connection request
await page.click('button:has-text("Connect Wallet")')
// Approve the request
await polkadotJs.authorize()Approve Transaction
When your dApp requests transaction signing:
// Your dApp triggers a transaction
await page.click('button:has-text("Submit")')
// Approve the transaction
await polkadotJs.approveTx()To reject a transaction:
await polkadotJs.rejectTx()Complete Example
import { createWalletTest, expect } from '@avalix/chroma'
const test = createWalletTest({
wallets: [{
type: 'polkadot-js'
}]
})
test.describe('Polkadot.js Wallet', () => {
test.beforeEach(async ({ wallets }) => {
const polkadotJs = wallets['polkadot-js']
await polkadotJs.importMnemonic({
seed: 'bottom drive obey lake curtain smoke basket hold race lonely fit walk',
})
})
test('connect and transfer', async ({ page, wallets }) => {
const polkadotJs = wallets['polkadot-js']
await page.goto('http://localhost:3000')
await page.click('button:has-text("Connect")')
await polkadotJs.authorize()
await expect(page.locator('.connected')).toBeVisible()
await page.fill('input[name="amount"]', '1.0')
await page.click('button:has-text("Send")')
await polkadotJs.approveTx()
await expect(page.locator('.success')).toBeVisible()
})
})Troubleshooting
Extension not loading
Make sure you've downloaded the extensions:
npx chroma download-extensionsPopup not appearing
Increase the timeout in your test:
test.setTimeout(60000)Account not found
Ensure the mnemonic is imported before interacting with the dApp:
// Import FIRST
await polkadotJs.importMnemonic({ seed: '...' })
// THEN navigate to dApp
await page.goto('http://localhost:3000')