/** * MetaMask E2E Tests * * End-to-end tests for MetaMask integration using Playwright */ import { test, expect } from '@playwright/test'; const CHAIN_ID = '0x8a'; const NETWORK_NAME = 'DeFi Oracle Meta Mainnet'; test.describe('MetaMask Integration', () => { test.beforeEach(async ({ page }) => { // Navigate to test page await page.goto('http://localhost:3000'); }); test('should add network to MetaMask', async ({ page, context }) => { // Mock window.ethereum await page.addInitScript(() => { (window as any).ethereum = { isMetaMask: true, request: async (args: { method: string; params?: unknown[] }) => { if (args.method === 'wallet_addEthereumChain') { return null; } if (args.method === 'eth_chainId') { return CHAIN_ID; } return null; }, on: () => {}, removeListener: () => {}, }; }); // Click add network button await page.click('button:has-text("Add ChainID 138")'); // Wait for network addition await page.waitForTimeout(1000); // Verify network was added const status = await page.textContent('#status'); expect(status).toContain('Network added successfully'); }); test('should switch to ChainID 138', async ({ page }) => { // Mock window.ethereum await page.addInitScript(() => { (window as any).ethereum = { isMetaMask: true, request: async (args: { method: string; params?: unknown[] }) => { if (args.method === 'wallet_switchEthereumChain') { return null; } if (args.method === 'eth_chainId') { return CHAIN_ID; } return null; }, on: () => {}, removeListener: () => {}, }; }); // Click switch network button await page.click('button:has-text("Switch to ChainID 138")'); // Wait for network switch await page.waitForTimeout(1000); // Verify network was switched const status = await page.textContent('#status'); expect(status).toContain('Switched to ChainID 138'); }); test('should add token to MetaMask', async ({ page }) => { // Mock window.ethereum await page.addInitScript(() => { (window as any).ethereum = { isMetaMask: true, request: async (args: { method: string; params?: unknown[] }) => { if (args.method === 'wallet_watchAsset') { return null; } return null; }, on: () => {}, removeListener: () => {}, }; }); // Click add token button await page.click('button:has-text("Add WETH Token")'); // Wait for token addition await page.waitForTimeout(1000); // Verify token was added const status = await page.textContent('#status'); expect(status).toContain('Token added successfully'); }); test('should handle MetaMask not installed', async ({ page }) => { // Don't mock window.ethereum // Click add network button await page.click('button:has-text("Add ChainID 138")'); // Verify error message const status = await page.textContent('#status'); expect(status).toContain('MetaMask is not installed'); }); test('should handle user rejection', async ({ page }) => { // Mock window.ethereum with user rejection await page.addInitScript(() => { (window as any).ethereum = { isMetaMask: true, request: async () => { const error = new Error('User rejected'); (error as any).code = 4001; throw error; }, on: () => {}, removeListener: () => {}, }; }); // Click add network button await page.click('button:has-text("Add ChainID 138")'); // Wait for error await page.waitForTimeout(1000); // Verify error message const status = await page.textContent('#status'); expect(status).toContain('User rejected'); }); });