1.0.8 • Published 1 year ago

@ensdomains/headless-web3-provider v1.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Headless Web3 Provider

Playwright Tests NPM Downloads

headless-web3-provider fork by ENS. It uses viem, has fewer dependencies and supports EIP-6963 (ported from this PR).

Headless MetaMask for testing Ethereum apps.

Install

pnpm i -D @ensdomains/headless-web3-provider viem

About

The headless-web3-provider library emulates a Web3 wallet similar to Metamask and provides programmatic control over various operations, such as switching networks, connecting a wallet, and sending transactions, making it useful for end-to-end testing of Ethereum-based applications. It allows to programmatically accept or decline operations, making it handy for test automation.

Supported methods

MethodConfirmable
eth_requestAccountsYes
eth_accountsYes
eth_sendTransactionYes
wallet_addEthereumChainYes
wallet_switchEthereumChainYes
wallet_requestPermissionsYes
personal_signYes
eth_signTypedDataYes
eth_signTypedData_v1Yes
eth_signTypedData_v3Yes
eth_signTypedData_v4Yes
eth_callNo
eth_estimateGasNo
eth_blockNumberNo
eth_getBlockByNumberNo
eth_getTransactionByHashNo
eth_getTransactionReceiptNo
eth_chainIdNo
net_versionNo

Examples

Playwright

Below given a simple example. More complex scenarios you can find in tests/e2e folder.

Setup (add a fixture):

// tests/fixtures.js
import { test as base } from '@playwright/test'
import { injectHeadlessWeb3Provider } from '@ensdomains/headless-web3-provider'
import { anvil } from 'viem/chains'

export const test = base.extend({
  // signers - the private keys that are to be used in the tests
  signers: [process.env.PRIVATE_KEY],

  // injectWeb3Provider - function that injects web3 provider instance into the page
  injectWeb3Provider: async ({ signers }, use) => {
    await use((page, privateKeys = signers) =>
      injectHeadlessWeb3Provider(
       { page, privateKeys, chains: [anvil] }
      )
    )
  },
})

Usage:

// tests/e2e/example.spec.js
import { test } from '../fixtures'

test('connect the wallet', async ({ page, injectWeb3Provider }) => {
  // Inject window.ethereum instance
  const wallet = await injectWeb3Provider(page)

  await page.goto('https://metamask.github.io/test-dapp/')

  // Request connecting the wallet
  await page.getByRole('button', { name: 'Connect', exact: true }).click()

  // You can either authorize or reject the request
  await wallet.authorize(Web3RequestKind.RequestAccounts)

  // Verify if the wallet is really connected
  await test.expect(page.locator('text=Connected')).toBeVisible()
  await test
    .expect(page.locator('text=0x8b3a08b22d25c60e4b2bfd984e331568eca4c299'))
    .toBeVisible()
})
1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago