[AI] GitHub Copilot cho Automation Tester

Posted by : on

Category : ai   github-copilot   testing


GitHub Copilot cho Automation Tester

Giới thiệu

GitHub Copilot là công cụ mạnh mẽ đặc biệt hữu ích cho Automation Testers. Tài liệu này tập trung vào cách sử dụng Copilot để:

  • Viết test scripts nhanh hơn
  • Tạo test data và fixtures
  • Generate locators và selectors
  • Debug test failures
  • Maintain test suites

1. Viết Test Cases với Copilot

Selenium WebDriver (Java/Python)

Ví dụ 1: Login Test

Prompt hiệu quả:

// Test login functionality with valid credentials
// Steps:
// 1. Navigate to login page
// 2. Enter username "testuser@example.com"
// 3. Enter password "Test@123"
// 4. Click login button
// 5. Verify user is redirected to dashboard
// 6. Verify welcome message is displayed
@Test
public void testLoginWithValidCredentials() {
    // Copilot sẽ generate test code
}

Kết quả Copilot generate:

@Test
public void testLoginWithValidCredentials() {
    driver.get("https://example.com/login");
    
    WebElement emailField = driver.findElement(By.id("email"));
    emailField.sendKeys("testuser@example.com");
    
    WebElement passwordField = driver.findElement(By.id("password"));
    passwordField.sendKeys("Test@123");
    
    WebElement loginButton = driver.findElement(By.cssSelector("button[type='submit']"));
    loginButton.click();
    
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    wait.until(ExpectedConditions.urlContains("/dashboard"));
    
    WebElement welcomeMessage = driver.findElement(By.className("welcome-message"));
    Assert.assertTrue(welcomeMessage.isDisplayed());
    Assert.assertTrue(welcomeMessage.getText().contains("Welcome"));
}

Ví dụ 2: Data-Driven Test

Prompt:

# Test login with multiple user credentials (data-driven)
# Test data: valid users, invalid users, locked users
# Verify appropriate message for each case
@pytest.mark.parametrize("username,password,expected_result", [
    # Copilot sẽ suggest test data
])
def test_login_multiple_credentials(username, password, expected_result):
    # Copilot generates test implementation

Playwright (TypeScript/JavaScript)

Ví dụ 1: E2E Test

Prompt hiệu quả:

// Test: Complete checkout flow
// 1. Add product to cart
// 2. Go to cart
// 3. Proceed to checkout
// 4. Fill shipping information
// 5. Select payment method
// 6. Place order
// 7. Verify order confirmation
test('complete checkout flow', async ({ page }) => {
    // Copilot generates implementation
});

Tips cho Playwright:

// Use user-facing locators (best practice)
// Prefer: getByRole, getByLabel, getByText, getByTestId
// Avoid: CSS selectors, XPath

// Example: Click login button
await page.getByRole('button', { name: 'Login' }).click();

// Example: Fill form field
await page.getByLabel('Email').fill('test@example.com');

// Example: Verify text
await expect(page.getByText('Welcome back')).toBeVisible();

Ví dụ 2: API Testing với Playwright

Prompt:

// Test: Create user via API
// POST /api/users
// Verify: 201 status, user object returned, user saved in database
test('create user via API', async ({ request }) => {
    // Copilot generates API test
});

Cypress

Prompt cho Cypress:

// Test: User registration flow
// Custom commands: cy.login(), cy.fillForm()
// Intercept API calls
// Verify success message and redirect
describe('User Registration', () => {
    it('should register new user successfully', () => {
        // Copilot generates Cypress test
    });
});

2. Generate Test Data và Fixtures

Tạo Test Data

Prompt:

// Generate test data for user registration
// Include: valid users, invalid emails, weak passwords, special characters
// Format: Array of objects with username, email, password, expected_result
const testData = [
    // Copilot generates comprehensive test data
];

Kết quả:

const testData = [
    // Valid cases
    { username: 'john_doe', email: 'john@example.com', password: 'SecurePass123!', expected: 'success' },
    { username: 'jane_smith', email: 'jane@test.com', password: 'MyP@ssw0rd', expected: 'success' },
    
    // Invalid email
    { username: 'invalid', email: 'notanemail', password: 'Pass123!', expected: 'invalid_email' },
    { username: 'invalid2', email: 'test@', password: 'Pass123!', expected: 'invalid_email' },
    
    // Weak password
    { username: 'weak', email: 'weak@test.com', password: '123', expected: 'weak_password' },
    { username: 'weak2', email: 'weak2@test.com', password: 'password', expected: 'weak_password' },
    
    // Special characters
    { username: 'special_!@#', email: 'special@test.com', password: 'Pass123!', expected: 'invalid_username' },
];

Fixtures cho Playwright

Prompt:

// Create fixture for authenticated user
// Fixture should: login, save auth state, reuse across tests
// File: fixtures/auth.fixture.ts
import { test as base } from '@playwright/test';

export const test = base.extend({
    // Copilot generates fixture
});

3. Generate Locators và Selectors

Best Practices cho Locators

Prompt cho Copilot:

// Generate robust locators for login form
// Prefer: data-testid, role, label, text
// Avoid: CSS classes, XPath
// Elements: email field, password field, submit button, error message

const loginLocators = {
    // Copilot generates locators
};

Kết quả:

const loginLocators = {
    emailField: '[data-testid="email-input"]',
    emailFieldByLabel: 'input[aria-label="Email"]',
    passwordField: '[data-testid="password-input"]',
    submitButton: 'button[type="submit"]',
    submitButtonByRole: 'role=button[name="Sign In"]',
    errorMessage: '[data-testid="error-message"]',
    errorMessageByText: 'text=Invalid credentials'
};

Page Object Model

Prompt:

// Create Page Object for Login page
// Include: locators, actions (login, getErrorMessage), waits
// Use Playwright best practices
export class LoginPage {
    // Copilot generates complete Page Object
}

Kết quả:

export class LoginPage {
    readonly page: Page;
    
    // Locators
    readonly emailInput = () => this.page.getByLabel('Email');
    readonly passwordInput = () => this.page.getByLabel('Password');
    readonly loginButton = () => this.page.getByRole('button', { name: 'Login' });
    readonly errorMessage = () => this.page.getByTestId('error-message');
    
    constructor(page: Page) {
        this.page = page;
    }
    
    async goto() {
        await this.page.goto('/login');
    }
    
    async login(email: string, password: string) {
        await this.emailInput().fill(email);
        await this.passwordInput().fill(password);
        await this.loginButton().click();
    }
    
    async getErrorMessage() {
        return await this.errorMessage().textContent();
    }
    
    async isLoginSuccessful() {
        await this.page.waitForURL('/dashboard');
        return this.page.url().includes('/dashboard');
    }
}

4. Debug Test Failures

Sử dụng Copilot Chat

Khi test fail:

@workspace This test is failing: test_login_valid_credentials
Error: Element not found: #login-button
Help me debug this issue

Copilot sẽ:

  1. Phân tích error message
  2. Kiểm tra locator
  3. Đề xuất solutions:
    • Thêm wait
    • Sử dụng locator khác
    • Kiểm tra timing issues

Thêm Debug Code

Prompt:

// Add debug logging to this test
// Log: page URL, element visibility, network requests
// Use Playwright's built-in debugging tools
test('debug login test', async ({ page }) => {
    // Existing test code
    // Copilot adds debug statements
});

Screenshot và Video khi fail

Prompt:

// Configure Playwright to capture screenshot and video on failure
// Save to ./test-results folder
// Include timestamp in filename
// playwright.config.ts

5. API Testing

REST API Tests

Prompt:

// Test: CRUD operations for User API
// Base URL: https://api.example.com
// Endpoints: GET/POST/PUT/DELETE /users
// Verify: status codes, response body, error handling
describe('User API Tests', () => {
    // Copilot generates API tests
});

Với Playwright Request:

// Test: User API with authentication
// 1. Login to get token
// 2. Create user with token
// 3. Verify user created
// 4. Update user
// 5. Delete user
test('User API CRUD with auth', async ({ request }) => {
    // Copilot generates complete API test flow
});

GraphQL Testing

Prompt:

// Test GraphQL query: Get user by ID
// Query: { user(id: "123") { id, name, email } }
// Verify: correct data structure, no errors
test('GraphQL: Get user by ID', async () => {
    // Copilot generates GraphQL test
});

6. Test Utilities và Helpers

Random Data Generators

Prompt:

// Create utility functions to generate random test data
// Functions: randomEmail, randomPassword, randomUsername, randomPhone
// Use faker.js or similar library
// Ensure data meets validation requirements

export const testDataGenerator = {
    // Copilot generates utility functions
};

Wait Utilities

Prompt:

// Create custom wait utilities for common scenarios
// waitForElement, waitForText, waitForAPIResponse, waitForPageLoad
// Include timeout and error handling
export class WaitUtils {
    // Copilot generates wait utilities
}

Assertion Helpers

Prompt:

// Create custom assertion helpers
// assertElementVisible, assertTextContains, assertAPIResponse
// Include detailed error messages
export const customAssertions = {
    // Copilot generates assertion helpers
};

7. CI/CD Integration

GitHub Actions Workflow

Prompt:

# Create GitHub Actions workflow for Playwright tests
# Trigger: on push and pull request
# Steps: install dependencies, run tests, upload artifacts
# Matrix: test on multiple browsers (chromium, firefox, webkit)
# Upload: test results, screenshots, videos

name: Playwright Tests
# Copilot generates complete workflow

Test Reports

Prompt:

// Configure Playwright to generate HTML report
// Include: test results, screenshots, videos, traces
// Publish report to GitHub Pages
// playwright.config.ts

8. Tips và Tricks cho Automation Testers

Tip 1: Sử dụng Comments để Generate Tests

Thay vì viết test từ đầu:

// Test: User can add item to cart and checkout
// Given: User is on product page
// When: User clicks "Add to Cart"
// And: User goes to cart
// And: User proceeds to checkout
// Then: User sees checkout page
// And: Order total is correct

test('add to cart and checkout', async ({ page }) => {
    // Copilot generates complete test based on comments
});

Tip 2: Generate Test từ Manual Test Cases

Copy manual test case vào comment:

/*
Manual Test Case: TC_001_Login
Precondition: User has valid account
Steps:
1. Navigate to login page
2. Enter email: test@example.com
3. Enter password: Test@123
4. Click Login button
Expected Result: User is logged in and redirected to dashboard
*/

test('TC_001_Login', async ({ page }) => {
    // Copilot converts manual test to automated test
});

Tip 3: Refactor Tests với Copilot

Prompt:

@workspace Refactor these tests to use Page Object Model
Files: tests/login.spec.ts, tests/signup.spec.ts
Create: pages/LoginPage.ts, pages/SignupPage.ts

Tip 4: Generate Test Data từ Schema

Prompt:

// Generate test data based on this User schema
interface User {
    id: string;
    username: string; // 3-20 chars, alphanumeric
    email: string; // valid email format
    age: number; // 18-100
    role: 'user' | 'admin' | 'moderator';
}

// Generate 10 valid users and 5 invalid users for testing
const testUsers = [
    // Copilot generates test data matching schema
];

Tip 5: Parallel Test Execution

Prompt:

// Configure Playwright for parallel test execution
// Workers: 4
// Retry failed tests: 2 times
// Timeout: 30 seconds per test
// playwright.config.ts

Tip 6: Visual Regression Testing

Prompt:

// Add visual regression test for homepage
// Compare screenshot with baseline
// Highlight differences
// Use Playwright's screenshot comparison
test('visual regression: homepage', async ({ page }) => {
    // Copilot generates visual test
});

Tip 7: Mobile Testing

Prompt:

// Create mobile device tests
// Devices: iPhone 13, Samsung Galaxy S21, iPad
// Test: responsive layout, touch interactions
// Use Playwright device emulation
test.describe('Mobile Tests', () => {
    // Copilot generates mobile tests
});

Tip 8: Performance Testing

Prompt:

// Add performance assertions to test
// Measure: page load time, API response time
// Assert: load time < 3 seconds
// Use Playwright's performance APIs
test('performance: page load time', async ({ page }) => {
    // Copilot generates performance test
});

9. Common Patterns cho Automation Testing

Pattern 1: Setup và Teardown

Prompt:

// Setup: Create test user, login, navigate to page
// Teardown: Logout, delete test user, clear data
describe('User Management Tests', () => {
    beforeEach(async () => {
        // Copilot generates setup
    });
    
    afterEach(async () => {
        // Copilot generates teardown
    });
});

Pattern 2: Test Data Management

Prompt:

// Create test data manager
// Load data from JSON file
// Clean up after tests
// Support multiple environments (dev, staging, prod)
class TestDataManager {
    // Copilot generates data manager
}

Pattern 3: Custom Reporters

Prompt:

// Create custom test reporter
// Send results to Slack/Teams
// Include: pass/fail count, duration, failed test details
// Trigger: after test run completes
class CustomReporter {
    // Copilot generates reporter
}

10. Troubleshooting với Copilot

Flaky Tests

Prompt:

@workspace These tests are flaky: test_checkout, test_search
Help me identify and fix the flakiness
Common issues: timing, race conditions, network delays

Slow Tests

Prompt:

@workspace Optimize these slow tests
Current duration: 5 minutes
Target: under 2 minutes
Suggestions: parallel execution, reduce waits, mock APIs

Maintenance

Prompt:

@workspace Update all tests to use new locator strategy
Old: CSS selectors
New: data-testid attributes
Files: tests/**/*.spec.ts

Kết luận

GitHub Copilot là công cụ mạnh mẽ cho Automation Testers:

Tăng tốc độ viết tests - Generate test cases nhanh chóng
Cải thiện chất lượng - Best practices và patterns
Giảm maintenance - Refactor và update dễ dàng
Debug hiệu quả - Phân tích lỗi và đề xuất fixes
Học hỏi - Khám phá techniques và tools mới

Lưu ý quan trọng:

  • Luôn review và test code được generate
  • Hiểu rõ test logic trước khi chạy
  • Customize theo nhu cầu dự án
  • Kết hợp với kinh nghiệm testing của bạn

Bài viết được viết bằng AI 🚀


About Nguyen Chung
Nguyen Chung

Hi I am Nguyen Chung, an Automation Tester.

Email : ndchungict@gmail.com

Website : https://ndchungict.github.io

About Nguyen Chung

Hi, my name is Nguyen Duc Chung. Nice to see you!