Skip to content

Instantly share code, notes, and snippets.

@nadvolod
Last active May 21, 2025 11:38
Show Gist options
  • Save nadvolod/98c8caebe6b34c621e54976d92193026 to your computer and use it in GitHub Desktop.
Save nadvolod/98c8caebe6b34c621e54976d92193026 to your computer and use it in GitHub Desktop.
Data-driven test using Playwright
// Data-driven test that runs once for each data entry
test.describe('Login functionality', () => {
test(`Login as ${user.username}`, async ({ page }) => {
// Navigate to login page
await page.goto('https://example.com/login');
// Fill the form
//{ username: 'standardUser', password: 'secret1', expectedPage: '/dashboard' }
//{ username: 'adminUser', password: 'admin123', expectedPage: '/admin' },
await page.fill('input[name="username"]', user.username);
await page.fill('input[name="password"]', user.password);
// Submit the form
await page.click('button[type="submit"]');
// Check the result based on the expected outcome
if (user.expectedError) {
// For invalid login, check error message
const errorMessage = await page.locator('.error-message').textContent();
await expect(errorMessage).toContain(user.expectedError);
} else if (user.expectedPage) {
// For valid login, check we're redirected to the right page
//{ username: 'standardUser', password: 'secret1', expectedPage: '/dashboard' }
//{ username: 'adminUser', password: 'admin123', expectedPage: '/admin' },
await expect(page).toHaveURL(new RegExp(user.expectedPage));
}
});
});
//Will run 2 tests
// Input1 - { username: 'standardUser', password: 'secret1'} with Expected Output '/dashboard'
// Input2 - //{ username: 'adminUser', password: 'admin123'} with Expected Output '/admin'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment