Skip to content

Implemented Page Object Model (POM) structure for Synpress (Playwright) to conduct end-to-end (E2E) testing on a lending smart contract. Developed a Solidity smart contract for lending ETH to WETH, supporting deposit, withdraw, borrow, and repay flows (with an annual interest rate), Also built a staking contract that allows users to stake WETH

Notifications You must be signed in to change notification settings

Thanasornsawan/end_to_end_testing_dapp_playwright_synpress

Repository files navigation

🏦 End-to-End Testing of a DeFi Lending Protocol with Synpress & Playwright

This repository provides a complete end-to-end (E2E) testing setup for a DeFi lending smart contract using Synpress v4, Playwright, and Hardhat.
It covers:
✅ Smart contract deployment & interaction (Hardhat)
✅ Using Anvil to simulate different network and gas cost on local network
✅ Automated testing with Synpress + Playwright (MetaMask interaction included!)
✅ Testing across Layer 1 (Ethereum) and Layer 2 (Optimism) networks
Database integration testing with PostgreSQL (Prisma ORM)

This project is perfect for QA engineers, blockchain testers, and developers who want to learn end-to-end blockchain testing using real-world tools. 🚀

⭐ Why Use This Repo?

  • Hands-on Learning: Learn how to test DeFi smart contracts with real tools.
  • Complete Setup: Covers both frontend and backend testing, including MetaMask interaction.
  • Mainnet Forking: Test against real Ethereum mainnet data using Alchemy & Hardhat.
  • Database Testing: Use PostgreSQL + Prisma for off-chain integration testing.

🔹 If you find this repo useful, please ⭐ star it to support the project!

Project Structure Overview

The project is organized into three main sections, each with its own dependencies and functionality:

E2E_blockchain_testing/
├── contracts/                  # Smart contract code (Solidity)
├── scripts/                    # Deployment and utility scripts
├── test/                       # Backend tests for smart contracts
├── frontend/                   # React/Next.js frontend application
├── web3_test/                  # Playwright/Synpress E2E tests
├── services/                   # Shared services
├── prisma/                     # Database schema and migrations
├── hardhat.config.ts           # Hardhat configuration
└── package.json                # Backend package configuration

🛠️ How to Setup project 🛠️

  1. Install dependencies:

1.1 Install dependencies for Backend (smart contract):

npm install

Complie all the smart contract to your local project:

npx hardhat compile

1.2 Install dependencies for Frontend (react + nexjs):

cd frontend & npm install

1.3 Install dependencies for Synpress and Playwright:

cd web3_test & npm install
  1. Set up environment variables:
cp .env.example .env

Edit .env with your configuration:

  1. database setup (off-chain data for tracking events):

3.1 Install dependencies:

npm install @prisma/client
npm install prisma --save-dev

3.2 Start postgresql db with Prisma on docker compose:

docker-compose up -d

db4

3.3 Push schema to database:
Creates the tables according to your Prisma schema:

npx prisma db push

db2

Generate client
Generates the TypeScript client for your application

npx prisma generate

3.4 Check database tables:

npx prisma db pull

db3

3.5 Test db connection:

npx ts-node test-db-connection.ts

db1

Optional: Delete all db data

npx ts-node scripts/utils/clearDatabase.ts
  1. Create wallet setup cache:
cd web3_test
npx synpress wallet-setup

wallet

  1. Setup anvil (for custom network chainId and gas)
curl -L https://foundry-paradigm.xyz
foundryup

anvil

How to run project locally (DApp test cases)

# Terminal 1: Start local network (if you want to test single network on local)
npx hardhat node

# or use anvil to start 2 networks in the same time by custom script
node scripts/utils/run-local-l1-l2.js

anvil_script1 anvil_script2 anvil_script3

# Terminal 2: Deploy smart contract to both network
npx hardhat run scripts/deploy/deploy-l1.ts --network mainnetFork 
npx hardhat run scripts/deploy/deploy-l2.ts --network optimismFork

# Terminal 3: Start the front-end server (localhost:3000)
cd frontend & npm run dev

# Terminal 4: Run test cases with Playwright
npm run test:playwright:headless

## or with UI headful

npm run test:playwright:headful

If you want to update oracle price manually, run command:

npx hardhat run scripts/utils/updatePrice.ts --network local

If you want to simulate time pass for interest rate when borrow on local network, run command:

default time advance 15 seconds

npx hardhat run scripts/utils/simulate-time-passage.js --network local

or simulate time advance 30 day

SIMULATE_30_DAYS=true npx hardhat run scripts/utils/simulate-time-passage.js --network local

e2e

Topic Node Tesing on smart contract (Defi)

Command to run all test cases (refer backend test cases folder ./test/network)

# Terminal 1: Start local fork
npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/YOUR_KEY

# Terminal 2: Run tests
npm run test:main_network_fork_report

References

Blog

QA Blockchain Testing: Smart Contract & Network Performance with Hardhat
Guideline to be QA Web3: Complete E2E DeFi Project with Synpress (Playwright) and Hardhat/Anvil

About

Implemented Page Object Model (POM) structure for Synpress (Playwright) to conduct end-to-end (E2E) testing on a lending smart contract. Developed a Solidity smart contract for lending ETH to WETH, supporting deposit, withdraw, borrow, and repay flows (with an annual interest rate), Also built a staking contract that allows users to stake WETH

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published