Skip to main content

Overview

To streamline the testing process for developers, this tool automates the management of the MetaMask component, a wallet operation often essential for the functionality of Web3 applications.

Installation

auto-metamask can be installed using pip:

$ pip install auto-metamask

Ensure you have either the Chromium or Chrome browser installed. Without a specified directory, the system's default Chrome will be utilized. It's advisable to avoid the newest Chrome version due to potential Chrome driver mismatches.

For compatibility, use the Metamask extension version 10.34.0 dated July 10, 2023. If opting for an older version, install version 0.1.3 of this package.

For manual installations, you can download a specific version of the Chromium browser and its corresponding Chrome driver. Then, provide its path to the setupWebdriver function.

For a comprehensive list of Chromium browser versions and their matching Chrome drivers, visit here.

Usage Examples

import os
import sys
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from auto_metamask import *

if __name__ == '__main__':

    metamask_path = downloadMetamask(
        'https://github.com/MetaMask/metamask-extension/releases/download/v10.34.0/metamask-chrome-10.34.0.zip')
    driver = setupWebdriver(metamask_path, '/Applications/Chromium.app/Contents/MacOS/Chromium', None, 'chromedriver_mac64/chromedriver')
    # Test account, please do not use for production environment
    setupMetamask(
        'whip squirrel shine cabin access spell arrow review spread code fire marine', 'testtest')
    # Please use a special network name to avoid conflicts with built-in networks, such as 'MY_MATIC'
    addNetwork('MY_MATIC', 'https://rpc-mumbai.maticvigil.com', '80001', 'MATIC')
    changeNetwork('MY_MATIC')
    # Test account, please do not use for production environment
    importPK("bb334564f93fc3a40a3b6a89e0560101bb86e5b75c773381f1e6d2f37fc5c5ba")

    driver.switch_to.new_window()
    driver.get('https://metamask.github.io/test-dapp/')

    # Wait 20s for the page to load, and click the 'Connect' button
    wait = WebDriverWait(driver, 20, 1)
    wait.until(EC.element_to_be_clickable(
        (By.CSS_SELECTOR, "button[id='connectButton']"))).click()
    # MetaMask will pop up a window, complete the connection
    connect()

    # Click the 'Request Permissions' button
    wait.until(EC.element_to_be_clickable(
        (By.CSS_SELECTOR, "button[id='requestPermissions']"))).click()
    # MetaMask will pop up a window
    connect()

    # Click the 'Personal Sign' button
    wait.until(EC.element_to_be_clickable(
        (By.CSS_SELECTOR, "button[id='personalSign']"))).click()
    # MetaMask will pop up a window
    confirm()

    # Click the 'Send Transaction' button
    wait.until(EC.element_to_be_clickable(
        (By.CSS_SELECTOR, "button[id='sendButton']"))).click()
    # MetaMask will pop up a window
    confirm()
    waitPending(20)

    # Click the 'Create Token' button
    wait.until(EC.element_to_be_clickable(
        (By.CSS_SELECTOR, "button[id='createToken']"))).click()
    # MetaMask will pop up a window
    confirm()
    waitPending(20)

    # Click the 'Approve Tokens' button
    wait.until(EC.element_to_be_clickable(
        (By.CSS_SELECTOR, "button[id='approveTokens']"))).click()
    # MetaMask will pop up a window
    approveTokens(6)
    waitPending(20)

    time.sleep(60)
    driver.quit()

API Reference

downloadMetamask

def downloadMetamask(url)

Download the metamask extension

Arguments:

  • url (String): Metamask extension download address (.zip)

Returns:

String: Extension file path

setupWebdriver

def setupWebdriver(metamask_path,
                   chrome_path=None,
                   version=None,
                   chromedriver_path=None)

Initialize chrome browser and install metamask extension

Arguments:

  • metamask_path (String): Extension file path
  • chrome_path (String): Chrome browser path, default is None.
  • version (String): Chrome browser version, make sure it matches the chromedriver version, if not provided, the latest version will be used, default is None. if chromedriver_path is provided, this parameter will be ignored.
  • chromedriver_path (String): Chromedriver file path, default is None.

Returns:

WebDriver: Selenium Chrome WebDriver

setupMetamask

@switchPage
def setupMetamask(recovery_phrase, password)

Setup metamask wallet

Arguments:

  • recovery_phrase (String): Recovery phrase (12 words)
  • password (String): Wallet password (minimum 8 characters)

addNetwork

@switchPage
def addNetwork(network_name, rpc_url, chain_id, currency_symbol)

Add a custom network

Arguments:

  • network_name (String): Network name
  • rpc_url (String): RPC URL
  • chain_id (String): Chain ID
  • currency_symbol (String): Currency symbol

changeNetwork

@switchPage
def changeNetwork(network_name)

Switch to a network

Arguments:

  • network_name (String): Network name

importPK

@switchPage
def importPK(priv_key)

Import private key

Arguments:

  • priv_key (String): Private key

connect

@switchPage
def connect()

Connect wallet

approve

@switchPage
def approve()

Approve wallet

approveTokens

@switchPage
def approveTokens(cap=None)

Approve tokens

Arguments:

  • cap (Number): Spending limit, must be greater than 0, default is None.

confirm

@switchPage
def confirm()

Confirm wallet

Use for Transaction, Sign, Deploy Contract, Create Token, Add Token, Sign In, etc.

waitPending

@switchPage
def waitPending(timeout=40)

Wait pending

Arguments:

  • timeout (Number): Timeout (seconds)

Credits

Release files for auto-metamask 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for auto-metamask 0.2.4
File Size Uploaded
auto-metamask-0.2.4.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for auto-metamask 0.2.4
File Interpreter ABI Platform
auto_metamask-0.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 25.5 kB

Release files / auto-metamask-0.2.4.tar.gz

Download URL auto-metamask-0.2.4.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
db19318c30e1933e6fdbb469dbd1e100cd394e6b26257a23bc5eb1de53d991d1
BLAKE2b-256 checksum
How to use checksums
16bc738ddb32b3cb8713b81b9d6885871beeb235161018444060f8e42be9624e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.16

Release files / auto_metamask-0.2.4-py3-none-any.whl

Download URL auto_metamask-0.2.4-py3-none-any.whl
Size 14.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2d65aaed66e581c594d1bf5a420770908f4339956e891b973f87b27712cde66d
BLAKE2b-256 checksum
How to use checksums
64e78375dd486816a9bada23bc08cd600d985004325c5d7908bf61b2a03658f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.16

Release history Release notifications | RSS feed

This release

0.2.4 This release

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.0

2 release files

0.1.3

1 release file

0.1.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page