Skip to content
On this page
[Academy]

Advanced testing techniques

Four subjects a test automation engineer runs into after the basics: the design patterns that keep a suite maintainable, load testing with JMeter and k6, security testing with OWASP ZAP, and where machine learning genuinely helps. The code here is vendor-neutral and runs outside xtest.

Advanced Test Design Patterns

Common Test Patterns Overview

PatternDescriptionWhen to Use
Page Object Model (POM)Encapsulates page elements and actions for maintainabilityUI testing with frequent changes
Screenplay PatternFocuses on user goals and tasks rather than UI detailsBDD-style testing with complex interactions
Factory PatternCreates test data objects dynamicallyNeed varied test data scenarios
Builder PatternConstructs complex test objects step by stepComplex test data with many optional fields

The Page Object Model (POM) is a design pattern that creates an object repository for storing web elements. It helps reduce code duplication and improves test maintenance.

Advanced POM with Inheritance
typescript
// Base Page class with common functionality
abstract class BasePage {
  protected driver: WebDriver;
  
  constructor(driver: WebDriver) {
    this.driver = driver;
  }
  
  protected async waitForElement(locator: By, timeout = 10000): Promise<WebElement> {
    const wait = new WebDriverWait(this.driver, timeout);
    return wait.until(elementLocated(locator));
  }
  
  protected async clickWhenReady(locator: By): Promise<void> {
    const element = await this.waitForElement(locator);
    await wait.until(elementIsClickable(element));
    await element.click();
  }
  
  protected async typeWhenReady(locator: By, text: string): Promise<void> {
    const element = await this.waitForElement(locator);
    await element.clear();
    await element.sendKeys(text);
  }
  
  abstract async isLoaded(): Promise<boolean>;
}

// Specific page implementation
class LoginPage extends BasePage {
  private readonly usernameInput = By.id('username');
  private readonly passwordInput = By.id('password');
  private readonly loginButton = By.css('button[type="submit"]');
  private readonly errorMessage = By.className('error-message');
  
  async login(username: string, password: string): Promise<void> {
    await this.typeWhenReady(this.usernameInput, username);
    await this.typeWhenReady(this.passwordInput, password);
    await this.clickWhenReady(this.loginButton);
  }
  
  async getErrorMessage(): Promise<string> {
    const element = await this.waitForElement(this.errorMessage);
    return element.getText();
  }
  
  async isLoaded(): Promise<boolean> {
    try {
      await this.waitForElement(this.loginButton, 5000);
      return true;
    } catch {
      return false;
    }
  }
}

// Usage in tests
test('Login with invalid credentials', async () => {
  const loginPage = new LoginPage(driver);
  await loginPage.login('invalid@user.com', 'wrongpassword');
  
  const error = await loginPage.getErrorMessage();
  expect(error).toBe('Invalid credentials');
});
Fluent Page Objects
typescript
// Fluent interface pattern for better readability
class SearchPage extends BasePage {
  async searchFor(query: string): SearchPage {
    await this.typeWhenReady(By.id('search'), query);
    return this;
  }
  
  async filterByCategory(category: string): SearchPage {
    await this.clickWhenReady(By.id(`category-${category}`));
    return this;
  }
  
  async sortBy(criteria: 'price' | 'rating' | 'date'): SearchPage {
    await this.selectDropdown(By.id('sort'), criteria);
    return this;
  }
  
  async getResults(): Promise<SearchResult[]> {
    const elements = await this.driver.findElements(By.className('result'));
    return Promise.all(elements.map(el => this.parseResult(el)));
  }
}

// Clean, readable test
test('Search with filters', async () => {
  const searchPage = new SearchPage(driver);
  
  const results = await searchPage
    .searchFor('laptop')
    .filterByCategory('electronics')
    .sortBy('price')
    .getResults();
  
  expect(results).toHaveLength(20);
  expect(results[0].price).toBeLessThan(results[19].price);
});

The Screenplay pattern focuses on modeling user behavior through actors, tasks, and questions, providing better abstraction than traditional page objects.

typescript
// Actor represents a user
class Actor {
  private name: string;
  private abilities: Map<string, Ability> = new Map();
  
  constructor(name: string) {
    this.name = name;
  }
  
  can(ability: Ability): Actor {
    this.abilities.set(ability.constructor.name, ability);
    return this;
  }
  
  attemptsTo(...tasks: Task[]): Promise<void> {
    return tasks.reduce(
      (promise, task) => promise.then(() => task.performAs(this)),
      Promise.resolve()
    );
  }
  
  asks<T>(question: Question<T>): Promise<T> {
    return question.answeredBy(this);
  }
}

// Tasks represent user actions
class Login implements Task {
  constructor(
    private username: string,
    private password: string
  ) {}
  
  static withCredentials(username: string, password: string): Login {
    return new Login(username, password);
  }
  
  async performAs(actor: Actor): Promise<void> {
    const browse = actor.abilityTo(BrowseTheWeb);
    await browse.type(LoginForm.USERNAME_FIELD, this.username);
    await browse.type(LoginForm.PASSWORD_FIELD, this.password);
    await browse.click(LoginForm.SUBMIT_BUTTON);
  }
}

// Questions verify state
class TheCurrentUrl implements Question<string> {
  async answeredBy(actor: Actor): Promise<string> {
    const browse = actor.abilityTo(BrowseTheWeb);
    return browse.getCurrentUrl();
  }
}

// Usage in tests
test('User can login and navigate', async () => {
  const james = new Actor('James')
    .can(BrowseTheWeb.using(driver));
  
  await james.attemptsTo(
    Navigate.to('/login'),
    Login.withCredentials('james@example.com', 'password123'),
    Navigate.to('/dashboard')
  );
  
  const currentUrl = await james.asks(new TheCurrentUrl());
  expect(currentUrl).toContain('/dashboard');
});

Where to take this next

Build one small framework end to end rather than reading a second article about frameworks. Put it behind a pipeline, point a load test at it, and run ZAP over it. The patterns above only make sense once a suite of your own has hurt you.