How to Test a Search Filter in Playwright, Cypress and Selenium

updated December 2, 2026

You type a word into a search box. The result you wanted appears. The test passes. The search can still be broken.

This guide shows why that happens, and how to write a search test that fails when the filter does not filter. The examples use Playwright, Cypress and Selenium on an invented app. The problem at the end uses a practice app you can open and test.

The example app

Harbour Ferries is an invented timetable app. Its home page lists five ferry routes in a table:

Route
Dover to Calais
Dover to Ostend
Harwich to Hook
Ramsgate to Ostend
Newhaven to Dieppe

Each row has the test id route-row. A test id is an attribute the developers add to an element only so tests can find it. A search box, labelled Search routes with the test id route-search, filters the table as you type. Two routes go to Ostend.

A search test that passes on a broken search

Here is the test most people write first:

import { test, expect } from '@playwright/test';

test('finds the Ostend routes', async ({ page }) => {
  await page.goto('/');
  await page.getByLabel('Search routes').fill('Ostend');
  await expect(page.getByText('Ramsgate to Ostend')).toBeVisible();
});

It types Ostend and checks that one Ostend route is visible. It passes.

Now suppose a developer breaks the search, so it ignores what you type. All five routes stay on the screen. Ramsgate to Ostend is one of them, so it is still visible. The test still passes.

The test proves the route exists. It does not prove the search did anything. Teams often find this out when a user reports that search "shows everything", and the search test has been green the whole time.

What a filter promises

A filter makes two promises:

  • It keeps the rows that match.
  • It removes the rows that do not.

The test above checks only the first promise. A search that returns everything breaks the second and keeps the first, so the test cannot see it.

To check both promises, a search test needs three things:

  • The count before you search. This proves the table started with all its rows.
  • The count after you search. This proves the search removed some.
  • Every row that is left matches. This proves it removed the right ones.

Playwright

test('the search keeps only the Ostend routes', async ({ page }) => {
  await page.goto('/');
  const rows = page.getByTestId('route-row');
  await expect(rows).toHaveCount(5);

  await page.getByLabel('Search routes').fill('Ostend');

  await expect(rows).toHaveCount(2);
  await expect(rows).toHaveText([/Ostend/, /Ostend/]);
});

Line by line:

  • page.getByTestId('route-row') describes every row. It does not grab them yet, so it always reads the current table.
  • toHaveCount(5) checks the table before the search.
  • fill('Ostend') types into the search box.
  • toHaveCount(2) checks that the search removed three rows.
  • toHaveText([/Ostend/, /Ostend/]) checks each remaining row, in order. Each one must contain Ostend.

Every expect here retries until it passes or times out. If the table updates a moment after you type, the test waits for it. On the broken search, the count stays at five and the test fails with the number it found.

Cypress

it('the search keeps only the Ostend routes', () => {
  cy.visit('/');
  cy.get('[data-testid="route-row"]').should('have.length', 5);

  cy.get('[data-testid="route-search"]').type('Ostend');

  cy.get('[data-testid="route-row"]').should(($rows) => {
    expect($rows).to.have.length(2);
    $rows.each((index, row) => {
      expect(row.textContent).to.contain('Ostend');
    });
  });
});

Line by line:

  • should('have.length', 5) checks the table before the search.
  • type('Ostend') types into the search box.
  • The last should takes a function. Cypress runs the whole function again and again until every check inside it passes, or the timeout ends.
  • $rows is the list of rows, wrapped in jQuery. jQuery is a small library for working with page elements. $rows.each visits each row, and row.textContent reads its text.

Keep both checks in the same should. Then Cypress retries them together against the same, current table.

Selenium

Selenium does not wait for you. You ask it to wait, with a condition.

const { Builder, By } = require('selenium-webdriver');
const assert = require('assert');

describe('Harbour Ferries search', function () {
  it('keeps only the Ostend routes', async function () {
    const driver = await new Builder().forBrowser('chrome').build();
    try {
      await driver.get(`${process.env.BASE_URL}/`);
      const before = await driver.findElements(By.css('[data-testid="route-row"]'));
      assert.strictEqual(before.length, 5);

      await driver.findElement(By.css('[data-testid="route-search"]')).sendKeys('Ostend');

      const rows = await driver.wait(async () => {
        const found = await driver.findElements(By.css('[data-testid="route-row"]'));
        return found.length === 2 ? found : null;
      }, 5000);
      for (const row of rows) {
        assert.ok((await row.getText()).includes('Ostend'));
      }
    } finally {
      await driver.quit();
    }
  });
});

Line by line:

  • BASE_URL is an environment variable that holds the app's address, such as your local server. Selenium needs a full address.
  • findElements, with an "s", returns a list. It can be empty, which is why it is safe for counting.
  • driver.wait calls your function until it returns something that is not null. Here it returns the rows only when there are exactly two.
  • The loop reads each row's text and checks that it contains Ostend.
  • finally closes the browser even when a check fails.

On the broken search, the count never reaches two. driver.wait gives up after five seconds with a timeout error. That error is your test catching the bug.

Why count before you search

If the table is empty, or still loading when you type, "two rows after" can come from something else. Counting first proves the search went from five rows to two.

Exact counts have one cost. In a shared test environment, someone can add a route, and the count changes. Two ways to handle it:

  • Create the routes your test needs in its own setup, so the data is always the same.
  • Or check that the count went down and that every remaining row matches, without an exact number.

Edge cases worth one test each

A search box usually makes more promises than the first one. Read the requirements, and write one test for each promise you find there:

  • No match. Search for a place with no route. The table is empty, and a message such as "No routes found" appears.
  • Letter case. Does a search for "ostend" find "Ostend"? Check what the requirement says before you assume either answer.
  • Clearing the search. Delete the text, and every row comes back.
  • Part of a word. Does "Ost" find Ostend? Again, the requirement decides.

Three mistakes to avoid

  • Checking only that a match is visible. It passes when the search does nothing.
  • Checking the count but not the rows. A search that returns the wrong two rows still returns two rows.
  • Reading the rows once, without waiting. If you read the text before the table updates, you check the old table.

Remember this

A filter is defined by what it removes. Count the rows before and after, and check that every row left matches.

A search test proves the filter only when a search that returns everything makes it fail. The problem "Prove the search actually filters" practises proving that a search removed the rows that do not match.

now prove it

Prove the search actually filters

Reading is the easy part. Write the test yourself, then we break the app on purpose to check it would really catch the bug.

Solve this problem →

Keep reading