Skip to content

Commit b74eb13

Browse files
committed
Add Playwright TypeScript test automation framework
1 parent 6dd871d commit b74eb13

15 files changed

+681
-5
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,29 @@ mvn test -DbaseUrl=https://staging.ultimateqa.com -Dbrowser=firefox -Dheadless=t
143143
2. Add appropriate unit tests
144144
3. Update documentation
145145
4. Follow code style guidelines
146+
147+
## �� Running Tests
148+
149+
Now you can run the tests in several ways:
150+
151+
1. Run all tests in headless mode:
152+
```bash
153+
npm test
154+
```
155+
156+
2. Run tests in headed mode (browser visible):
157+
```bash
158+
npm run test:headed
159+
```
160+
161+
3. Run tests with Playwright UI mode (interactive):
162+
```bash
163+
npm run test:ui
164+
```
165+
166+
4. Run tests in debug mode:
167+
```bash
168+
npm run test:debug
169+
```
170+
171+
Let's try running the tests in headed mode so you can see what's happening:

package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"devDependencies": {
3+
"@cucumber/cucumber": "^11.2.0",
4+
"@playwright/test": "^1.50.1",
5+
"@types/node": "^22.13.9",
6+
"@typescript-eslint/eslint-plugin": "^8.26.0",
7+
"@typescript-eslint/parser": "^8.26.0",
8+
"cucumber-html-reporter": "^7.2.0",
9+
"dotenv": "^16.4.7",
10+
"eslint": "^9.21.0",
11+
"prettier": "^3.5.3",
12+
"ts-node": "^10.9.2",
13+
"typescript": "^5.8.2"
14+
}
15+
}

playwright-typescript/.gitignore

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Playwright TypeScript specific
2+
playwright-report/
3+
test-results/
4+
playwright/.cache/
5+
/playwright/.auth/
6+
7+
# TypeScript
8+
dist/
9+
*.tsbuildinfo
10+
11+
# Node
12+
node_modules/
13+
npm-debug.log*
14+
yarn-debug.log*
15+
yarn-error.log*
16+
17+
# Environment variables
18+
.env
19+
.env.local
20+
.env.*.local
21+
22+
# IDE - VSCode
23+
.vscode/*
24+
!.vscode/settings.json
25+
!.vscode/tasks.json
26+
!.vscode/launch.json
27+
!.vscode/extensions.json
28+
29+
# Testing
30+
/coverage
31+
/screenshots
32+
/videos
33+
/traces
34+
35+
# Logs
36+
logs
37+
*.log
38+
npm-debug.log*
39+
40+
# Operating System
41+
.DS_Store
42+
Thumbs.db

playwright-typescript/README.md

+182
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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.

playwright-typescript/package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "playwright-typescript",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "playwright test",
8+
"test:headed": "playwright test --headed",
9+
"test:ui": "playwright test --ui",
10+
"test:debug": "playwright test --debug",
11+
"report": "playwright show-report"
12+
},
13+
"keywords": [],
14+
"author": "",
15+
"license": "ISC",
16+
"devDependencies": {
17+
"@playwright/test": "^1.42.1",
18+
"@types/node": "^20.11.24",
19+
"typescript": "^5.3.3",
20+
"dotenv": "^16.4.5"
21+
}
22+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { PlaywrightTestConfig, devices } from '@playwright/test';
2+
import dotenv from 'dotenv';
3+
4+
dotenv.config();
5+
6+
const config: PlaywrightTestConfig = {
7+
testDir: './tests',
8+
timeout: 30000,
9+
expect: {
10+
timeout: 5000
11+
},
12+
fullyParallel: true,
13+
forbidOnly: !!process.env.CI,
14+
retries: process.env.CI ? 2 : 0,
15+
workers: process.env.CI ? 1 : undefined,
16+
reporter: [
17+
['html'],
18+
['json', { outputFile: 'test-results/test-results.json' }]
19+
],
20+
use: {
21+
actionTimeout: 0,
22+
baseURL: process.env.BASE_URL || 'http://localhost:3000',
23+
trace: 'on-first-retry',
24+
video: 'on-first-retry',
25+
screenshot: 'only-on-failure'
26+
},
27+
projects: [
28+
{
29+
name: 'chromium',
30+
use: {
31+
...devices['Desktop Chrome']
32+
}
33+
},
34+
{
35+
name: 'firefox',
36+
use: {
37+
...devices['Desktop Firefox']
38+
}
39+
},
40+
{
41+
name: 'webkit',
42+
use: {
43+
...devices['Desktop Safari']
44+
}
45+
}
46+
],
47+
outputDir: 'test-results/'
48+
};
49+
50+
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Page } from '@playwright/test';
2+
import { BasePage } from '../core/BasePage';
3+
4+
export class ComplicatedPageComponent extends BasePage {
5+
// Locators for Complicated Page elements
6+
private readonly firstButton = "//a[@class='et_pb_button et_pb_button_0 et_pb_bg_layout_light']";
7+
8+
constructor(page: Page) {
9+
super(page);
10+
}
11+
12+
async isButtonVisible(): Promise<boolean> {
13+
return await this.isVisible(this.firstButton);
14+
}
15+
16+
async hoverOverButton(): Promise<void> {
17+
await this.page.hover(this.firstButton);
18+
}
19+
20+
async getButtonColor(): Promise<string> {
21+
const button = await this.page.locator(this.firstButton);
22+
return await button.evaluate((el) => window.getComputedStyle(el).color);
23+
}
24+
}

0 commit comments

Comments
 (0)