Package: auto-metamask
OverviewTo 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.Installationauto-metamask can be installed using pip:$pipinstallauto-metamaskEnsure you have either theChromiumorChromebrowser 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, visithere.Usage Examplesimportosimportsysimporttimefromselenium.webdriver.common.byimportByfromselenium.webdriver.supportimportexpected_conditionsasECfromselenium.webdriver.support.waitimportWebDriverWaitsys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))fromauto_metamaskimport*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 environmentsetupMetamask('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 environmentimportPK("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' buttonwait=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 connectionconnect()# Click the 'Request Permissions' buttonwait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[id='requestPermissions']"))).click()# MetaMask will pop up a windowconnect()# Click the 'Personal Sign' buttonwait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[id='personalSign']"))).click()# MetaMask will pop up a windowconfirm()# Click the 'Send Transaction' buttonwait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[id='sendButton']"))).click()# MetaMask will pop up a windowconfirm()waitPending(20)# Click the 'Create Token' buttonwait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[id='createToken']"))).click()# MetaMask will pop up a windowconfirm()waitPending(20)# Click the 'Approve Tokens' buttonwait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[id='approveTokens']"))).click()# MetaMask will pop up a windowapproveTokens(6)waitPending(20)time.sleep(60)driver.quit()API ReferencedownloadMetamaskdefdownloadMetamask(url)Download the metamask extensionArguments:url(String): Metamask extension download address (.zip)Returns:String: Extension file pathsetupWebdriverdefsetupWebdriver(metamask_path,chrome_path=None,version=None,chromedriver_path=None)Initialize chrome browser and install metamask extensionArguments:metamask_path(String): Extension file pathchrome_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 WebDriversetupMetamask@switchPagedefsetupMetamask(recovery_phrase,password)Setup metamask walletArguments:recovery_phrase(String): Recovery phrase (12 words)password(String): Wallet password (minimum 8 characters)addNetwork@switchPagedefaddNetwork(network_name,rpc_url,chain_id,currency_symbol)Add a custom networkArguments:network_name(String): Network namerpc_url(String): RPC URLchain_id(String): Chain IDcurrency_symbol(String): Currency symbolchangeNetwork@switchPagedefchangeNetwork(network_name)Switch to a networkArguments:network_name(String): Network nameimportPK@switchPagedefimportPK(priv_key)Import private keyArguments:priv_key(String): Private keyconnect@switchPagedefconnect()Connect walletapprove@switchPagedefapprove()Approve walletapproveTokens@switchPagedefapproveTokens(cap=None)Approve tokensArguments:cap(Number): Spending limit, must be greater than 0, default is None.confirm@switchPagedefconfirm()Confirm walletUse for Transaction, Sign, Deploy Contract, Create Token, Add Token, Sign In, etc.waitPending@switchPagedefwaitPending(timeout=40)Wait pendingArguments:timeout(Number): Timeout (seconds)Creditsmetamask-extensionseleniumselenium-stealth