|
| 1 | +# Playwright TypeScript UI Testing Framework |
| 2 | + |
| 3 | +A modern UI testing framework built with Playwright and TypeScript, implementing the Page Object Model with a three-layer architecture. |
| 4 | + |
| 5 | +## 🎯 Key Features |
| 6 | + |
| 7 | +- ✅ Three-layer Page Object Model (Components/Functions/State) |
| 8 | +- ✅ Strong TypeScript typing |
| 9 | +- ✅ Parallel test execution |
| 10 | +- ✅ Automatic screenshot capture on failure |
| 11 | +- ✅ Environment-based configuration |
| 12 | +- ✅ HTML and JSON reporting |
| 13 | + |
| 14 | +## 🏗 Architecture |
| 15 | + |
| 16 | +### Three-Layer Approach |
| 17 | + |
| 18 | +1. **Component Layer** (`src/components/`) |
| 19 | + - Element locators |
| 20 | + - Basic element interactions |
| 21 | + - Example: `ComplicatedPageComponent.ts` |
| 22 | + |
| 23 | +2. **Functions Layer** (`src/functions/`) |
| 24 | + - Business-level actions |
| 25 | + - Complex interactions |
| 26 | + - Example: `ComplicatedPageFunctions.ts` |
| 27 | + |
| 28 | +3. **State Layer** (`src/state/`) |
| 29 | + - Test assertions |
| 30 | + - State verification |
| 31 | + - Example: `ComplicatedPageState.ts` |
| 32 | + |
| 33 | +### Core Components |
| 34 | + |
| 35 | +- `BasePage` - Base class with common page interactions |
| 36 | +- `TestContext` - Singleton for managing test state |
| 37 | +- `Configuration` - Environment configuration management |
| 38 | + |
| 39 | +## 🚀 Getting Started |
| 40 | + |
| 41 | +### Prerequisites |
| 42 | + |
| 43 | +- Node.js 14 or higher |
| 44 | +- npm/yarn |
| 45 | +- Git |
| 46 | + |
| 47 | +### Installation |
| 48 | + |
| 49 | +1. Clone the repository: |
| 50 | + ```bash |
| 51 | + git clone <repository-url> |
| 52 | + cd playwright-typescript |
| 53 | + ``` |
| 54 | + |
| 55 | +2. Install dependencies: |
| 56 | + ```bash |
| 57 | + npm install |
| 58 | + ``` |
| 59 | + |
| 60 | +3. Set up environment: |
| 61 | + ```bash |
| 62 | + # Copy environment template |
| 63 | + cp .env.template .env |
| 64 | + |
| 65 | + # Edit .env with your settings |
| 66 | + ``` |
| 67 | + |
| 68 | +### Running Tests |
| 69 | + |
| 70 | +```bash |
| 71 | +# Run all tests |
| 72 | +npm test |
| 73 | + |
| 74 | +# Run in headed mode (browser visible) |
| 75 | +npm run test:headed |
| 76 | + |
| 77 | +# Run with UI mode |
| 78 | +npm run test:ui |
| 79 | + |
| 80 | +# Run specific test file |
| 81 | +npx playwright test tests/complicated-page.spec.ts |
| 82 | +``` |
| 83 | + |
| 84 | +## 📁 Project Structure |
| 85 | + |
| 86 | +``` |
| 87 | +playwright-typescript/ |
| 88 | +├── src/ |
| 89 | +│ ├── components/ # Element locators & basic interactions |
| 90 | +│ │ └── ComplicatedPageComponent.ts |
| 91 | +│ ├── functions/ # Business logic & complex actions |
| 92 | +│ │ └── ComplicatedPageFunctions.ts |
| 93 | +│ ├── state/ # Assertions & state verification |
| 94 | +│ │ └── ComplicatedPageState.ts |
| 95 | +│ ├── core/ # Framework core |
| 96 | +│ │ ├── BasePage.ts |
| 97 | +│ │ └── TestContext.ts |
| 98 | +│ └── config/ # Configuration management |
| 99 | +│ └── Configuration.ts |
| 100 | +├── tests/ # Test specifications |
| 101 | +│ └── complicated-page.spec.ts |
| 102 | +├── playwright.config.ts |
| 103 | +└── package.json |
| 104 | +``` |
| 105 | + |
| 106 | +## ⚙️ Configuration |
| 107 | + |
| 108 | +Configuration can be managed through: |
| 109 | +1. `.env` file (recommended for local development) |
| 110 | +2. Environment variables |
| 111 | +3. Command line parameters |
| 112 | + |
| 113 | +Example `.env`: |
| 114 | +```env |
| 115 | +BASE_URL=https://ultimateqa.com/complicated-page |
| 116 | +BROWSER=chromium |
| 117 | +HEADLESS=true |
| 118 | +DEFAULT_TIMEOUT=30000 |
| 119 | +``` |
| 120 | + |
| 121 | +## 🔍 Test Examples |
| 122 | + |
| 123 | +### Button Visibility Test |
| 124 | +```typescript |
| 125 | +test('verify button is visible on complicated page', async () => { |
| 126 | + await complicatedPageState.assertButtonIsVisible(); |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +### Button Color Change Test |
| 131 | +```typescript |
| 132 | +test('verify button changes color on hover', async () => { |
| 133 | + await complicatedPageState.assertButtonChangesColorOnHover(); |
| 134 | +}); |
| 135 | +``` |
| 136 | + |
| 137 | +## 📝 Development Guidelines |
| 138 | + |
| 139 | +1. **Adding New Tests** |
| 140 | + - Create component class for element interactions |
| 141 | + - Create functions class for business logic |
| 142 | + - Create state class for assertions |
| 143 | + - Add test file in `tests/` directory |
| 144 | + |
| 145 | +2. **Code Style** |
| 146 | + - Use TypeScript types |
| 147 | + - Follow existing patterns |
| 148 | + - Add JSDoc comments for functions |
| 149 | + |
| 150 | +3. **Best Practices** |
| 151 | + - Keep components focused and small |
| 152 | + - Use meaningful names |
| 153 | + - Write self-documenting code |
| 154 | + - Add appropriate error handling |
| 155 | + |
| 156 | +## 🐛 Debugging |
| 157 | + |
| 158 | +1. Run in headed mode: |
| 159 | + ```bash |
| 160 | + npm run test:headed |
| 161 | + ``` |
| 162 | + |
| 163 | +2. Use UI mode for interactive debugging: |
| 164 | + ```bash |
| 165 | + npm run test:ui |
| 166 | + ``` |
| 167 | + |
| 168 | +3. Check generated reports in: |
| 169 | + - `playwright-report/` - HTML reports |
| 170 | + - `test-results/` - Test artifacts |
| 171 | + |
| 172 | +## 🤝 Contributing |
| 173 | + |
| 174 | +1. Create feature branch |
| 175 | +2. Make changes |
| 176 | +3. Add/update tests |
| 177 | +4. Update documentation |
| 178 | +5. Submit pull request |
| 179 | + |
| 180 | +## 📄 License |
| 181 | + |
| 182 | +This project is licensed under the MIT License. |
0 commit comments