Sobes.tech
Middle+

Have you written unit tests and E2E tests? How did you choose elements in tests, are you familiar with the Page Object pattern?

sobes.tech AI

Answer from AI

Unit tests are written to verify individual functions or components in isolation, while E2E tests are for verifying the entire application, simulating user actions.

Selectors based on data-testid or unique classes/identifiers are often used in tests to make them resilient to UI changes.

The Page Object pattern helps structure E2E tests by creating abstractions of pages or components with methods for interaction and verification. This improves readability and maintainability of tests.

Example of using Page Object in JavaScript tests with Playwright:

class LoginPage {
  constructor(page) {
    this.page = page;
    this.usernameInput = page.locator('#username');
    this.passwordInput = page.locator('#password');
    this.loginButton = page.locator('button[type=submit]');
  }

  async login(username, password) {
    await this.usernameInput.fill(username);
    await this.passwordInput.fill(password);
    await this.loginButton.click();
  }
}

// In the test
const loginPage = new LoginPage(page);
await loginPage.login('user', 'pass');