Welcome to the Playwright Project! This README will guide you through the setup, usage, and development process for our Playwright-based automated testing framework.
Playwright is a Node.js library that provides a high-level API to interact with browsers and automate web application testing. This project leverages Playwright to provide a robust and scalable testing framework.
- Cross-browser testing (Chromium, Firefox, WebKit)
- Parallel test execution
- Visual testing and screenshots
- Network interception and mocking
- Automatic waiting for elements and actions
- CI/CD integration ready
Ensure you have the following software installed:
- Node.js (version 14 or higher)
- npm or yarn
Clone the repository:
git clone https://github.com/FC122/playwright-example
then:
cd playwright-project
Install dependencies:
npm install
or
yarn install
Install Playwright browsers:
npx playwright install
To run the Playwright tests, use the following command:
npx playwright test
The Page Object Model is a design pattern that creates an object repository for web UI elements. This makes tests more readable, maintainable, and reusable.
- Create a new file in the pages directory, e.g., pages/examplePage.js.
- Define a class representing the page and its elements. Example pages/examplePage.js:
class ExamplePage {
constructor(page) {
this.page = page;
this.exampleElement = page.locator('selector-for-element');
}
async navigate() {
await this.page.goto('https://example.com');
}
async getExampleElementText() {
return await this.exampleElement.textContent();
}
}
module.exports = ExamplePage;
- Import the page object in your test file.
- Instantiate the page object and use its methods in your tests. Example tests/example.spec.js:
const { test, expect } = require('@playwright/test');
const ExamplePage = require('../pages/examplePage');
test('example page test', async ({ page }) => {
const examplePage = new ExamplePage(page);
await examplePage.navigate();
const text = await examplePage.getExampleElementText();
expect(text).toBe('Expected Text');
});
Configuration for Playwright can be found in the playwright.config.js file. Here you can specify browser options, test directories, and other settings.
Example playwright.config.js:
const { devices } = require('@playwright/test');
module.exports = {
timeout: 30000,
use: {
headless: true,
viewport: { width: 1280, height: 720 },
ignoreHTTPSErrors: true,
},
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},
],
};
Tests are located in the tests directory. Each test file should follow the .spec.js or .test.js naming convention.
Example test file tests/example.spec.js:
const { test, expect } = require('@playwright/test');
test('basic test', async ({ page }) => {
await page.goto('https://example.com');
const title = await page.title();
expect(title).toBe('Example Domain');
});
Run all tests:
npx playwright test
Run tests in a specific file:
npx playwright test tests/example.spec.js
Run tests with a specific project configuration (e.g., Chromium):
npx playwright test --project=chromium
Integrate Playwright tests into your CI/CD pipeline by adding the following commands to your pipeline configuration:
Example for GitHub Actions:
Copy code
name: Playwright Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Install Playwright browsers
run: npx playwright install
- name: Run tests
run: npx playwright test
We welcome contributions to the Playwright Project. Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (git checkout -b feature-branch).
- Make your changes and commit them (git commit -m 'Add new feature').
- Push to the branch (git push origin feature-branch).
- open a pull request.
The MIT License (MIT) Copyright © 2024
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.