How to Validate Search Results, Filters and Empty States With Playwright
Imagine visiting an online shopping website and searching for Laptop.
The website displays 100 laptops.
Now you want to see only laptops priced below ₹50,000.
You select the price filter, but the website still displays laptops costing ₹80,000.
Is the search functionality working correctly?
No. The search has returned products, but the filtering functionality has failed.
This is why checking only whether search results are visible is not enough.
Search result validation is the process of checking whether an application displays the correct information and whether filters, sorting options and empty states behave according to business requirements.
With Playwright, testers can automate these checks and identify problems before users experience them.
In this guide, you will learn how to validate search results, filters, sorting and empty states using practical Playwright examples.
What is search result validation?
Search result validation checks whether the information displayed after a search matches the user's keyword and the application's expected behaviour.
For example, if a user searches for Laptop, the website should display relevant laptop products.
A tester should verify:
Whether the results are relevant.
Whether the result count is correct.
Whether product names are correct.
Whether prices are displayed properly.
Whether selected filters are applied.
Whether sorting works correctly.
Whether empty states appear when no matching products exist.
Search result validation helps ensure that users receive accurate and useful information.
What are filters in search functionality?
Filters help users narrow down search results.
Instead of viewing 100 products, users can select specific conditions to display only products matching their needs.
For example, an e-commerce website may provide:
When a user selects a filter, the displayed results should follow the selected condition.
What is an empty state in search testing?
An empty state appears when the application has no matching information to display.
For example, when a user searches for:
XYZ123UnknownProduct
The application may display:
"No products found. Try another search."
This is called an empty state.
A good empty state should clearly explain that no results were found and help users understand what they can do next.
Testers should verify that:
The empty-state message is visible.
The message is understandable.
Old search results are not displayed incorrectly.
Users can change their keyword.
Users can clear active filters.
The page does not show a broken layout.
Search result validation test scenarios
Before writing automation scripts, prepare test scenarios.
These test scenarios should be adjusted based on actual business requirements.
Step 1: Create a Playwright test
Create a file named:
search-validation.spec.ts
import { test, expect } from '@playwright/test';
test('Validate search results', async ({ page }) => {
await page.goto('https://example.com');
});
This creates a test case and opens your application.
Replace the example URL with your actual application URL.
Step 2: Search for a product
First, enter a keyword and execute the search.
const searchBox = page.getByPlaceholder('Search products');
await searchBox.fill('Laptop');
await page.getByRole('button', {
name: 'Search'
}).click();
This searches for laptops.
Step 3: Validate whether the correct results appear
Suppose the application displays two matching products:
Dell Inspiron
HP Pavilion
You can verify them using:
const products = page.locator('.product');
await expect(products).toHaveCount(2);
await expect(products).toContainText([
'Dell Inspiron',
'HP Pavilion'
]);
This checks that the result list contains the expected products.
Note that toContainText() with an array checks text content across matching elements. It does not necessarily prove that every product is individually correct or that no unexpected product exists. For exact result validation, use individual assertions or compare the complete expected list.
For example:
await expect(products).toHaveText([
'Dell Inspiron',
'HP Pavilion'
]);
Use this exact comparison only when the expected list and ordering are part of the requirement.
Step 4: Validate product prices
Product prices are important in e-commerce search testing.
Imagine that the application displays:
Dell Inspiron – ₹45,000
HP Pavilion – ₹48,000
You should verify that the correct price appears with the correct product.
const dellProduct = page.locator('.product-card')
.filter({ hasText: 'Dell Inspiron' });
await expect(dellProduct).toContainText('₹45,000');
This identifies the Dell product card and checks its displayed price.
Testing tip: If the price is calculated dynamically, use controlled test data or an API-backed expected value. Do not hardcode a price that changes between test runs.
Step 5: Validate price filters
Suppose the website has a price filter with the option:
Under ₹50,000
The application should display only products that meet the selected price condition.
Sample automation:
test('Validate price filter', async ({ page }) => {
await page.goto('https://example.com');
const searchBox = page.getByPlaceholder('Search products');
await searchBox.fill('Laptop');
await page.getByRole('button', {
name: 'Search'
}).click();
await page.getByLabel('Under ₹50,000').check();
const products = page.locator('.product-card');
await expect(products.first()).toBeVisible();
});
This example assumes the filter is implemented as an accessible checkbox.
However, visibility alone does not prove that the price filter works correctly.
You should also validate every displayed product's price.
const prices = await page.locator('.product-price').allTextContents();
for (const price of prices) {
const numericPrice = Number(price.replace(/[^\d.]/g, ''));
expect(numericPrice).toBeLessThan(50000);
}
This example assumes prices are formatted as comparable numeric values and that the filter means strictly less than ₹50,000. If the application uses an inclusive maximum, change the assertion to toBeLessThanOrEqual(50000).
Step 6: Validate brand filters
Imagine a user searches for Laptop and selects the Dell brand.
Only Dell laptops should appear.
test('Validate brand filter', async ({ page }) => {
await page.goto('https://example.com');
const searchBox = page.getByPlaceholder('Search products');
await searchBox.fill('Laptop');
await page.getByRole('button', {
name: 'Search'
}).click();
await page.getByLabel('Dell').check();
const products = page.locator('.product-card');
await expect(products.first()).toBeVisible();
await expect(products).toContainText(/Dell/);
});
For more complete validation, check every result individually rather than relying on a single aggregate text assertion.
For example:
const productNames = await products.allTextContents();
for (const productName of productNames) {
expect(productName.toLowerCase()).toContain('dell');
}
This confirms that each returned product contains the expected brand text, assuming the product cards include the brand name.
Step 7: Validate multiple filters together
Real customers often use more than one filter.
For example:
Brand: Dell
Price: Under ₹50,000
Rating: 4 stars and above
The application should display products that meet all selected conditions if the filter design uses AND logic.
test('Validate multiple search filters', async ({ page }) => {
await page.goto('https://example.com');
const searchBox = page.getByPlaceholder('Search products');
await searchBox.fill('Laptop');
await page.getByRole('button', {
name: 'Search'
}).click();
await page.getByLabel('Dell').check();
await page.getByLabel('Under ₹50,000').check();
await page.getByLabel('4 stars and above').check();
await expect(
page.getByText('Dell')
).toBeVisible();
});
This example checks that the selected conditions can be applied, but a production-quality test should also verify that every resulting product satisfies all three conditions.
Important: Some applications combine filters using OR logic within a category. Always confirm the expected filter rules before writing assertions.
Step 8: Validate sorting functionality
Sorting helps users arrange search results in a particular order.
Common sorting options include:
Price: Low to High
Price: High to Low
Newest First
Customer Rating
Popularity
Suppose a user selects Price: Low to High.
The products should appear from the lowest price to the highest price.
test('Validate price sorting', async ({ page }) => {
await page.goto('https://example.com');
await page.getByPlaceholder('Search products')
.fill('Laptop');
await page.getByRole('button', {
name: 'Search'
}).click();
await page.getByLabel('Sort by').selectOption({
label: 'Price: Low to High'
});
const prices = await page.locator('.product-price')
.allTextContents();
const numericPrices = prices.map(price =>
Number(price.replace(/[^\d.]/g, ''))
);
const sortedPrices = [...numericPrices].sort((a, b) => a - b);
expect(numericPrices).toEqual(sortedPrices);
});
This checks whether the displayed prices are in ascending order.
For an application with currency conversions or complex price formats, parse the values according to the application's data format.
Step 9: Validate empty search results
This is one of the most important negative testing scenarios.
Search for a keyword that does not exist in your controlled test data.
test('Validate empty search results', async ({ page }) => {
await page.goto('https://example.com');
const searchBox = page.getByPlaceholder('Search products');
await searchBox.fill('XYZ123UnknownProduct');
await page.getByRole('button', {
name: 'Search'
}).click();
await expect(
page.getByText('No products found')
).toBeVisible();
});
This checks whether the application displays the expected empty-state message.
Also verify that previous results do not remain visible.
await expect(
page.locator('.product-card')
).toHaveCount(0);
The exact message and selectors depend on the application.
Step 10: Validate Clear All Filters
When users select multiple filters, they should be able to remove them.
Suppose the website provides a Clear All button.
await page.getByRole('button', {
name: 'Clear All'
}).click();
await expect(
page.getByLabel('Dell')
).not.toBeChecked();
You should also verify that the selected price and rating filters are cleared and the expected default results return.
Complete search validation workflow
A complete search validation process should follow this sequence:
Open the application
Navigate to the search page.Enter a keyword
Search for a valid product.Validate results
Check names, prices and result count.Apply filters
Select brand, price and rating.Validate sorting
Check the expected product order.Test empty states
Search for a non-existing product.Clear filters
Verify the original result set returns.
Common mistakes in search result validation
Frequently asked questions
1. How do you validate search results in Playwright?
Enter a keyword, execute the search and use assertions to verify result names, counts, prices and other expected information.
2. How do you test filters in Playwright?
Use locators to select filter controls and assertions to verify that every displayed result follows the selected filter conditions.
3. How do you test an empty search result?
Enter a keyword that does not exist in the test dataset and verify that the application displays the correct no-results message.
4. How do you test multiple filters in Playwright?
Select multiple filters and verify that the resulting products satisfy the application's expected combination rules.
5. How do you validate price sorting?
Collect the displayed prices, convert them into numbers and compare their order with the expected ascending or descending sequence.
6. Why should testers validate empty states?
Empty-state testing ensures that users receive helpful feedback when no matching information is available instead of seeing a blank or broken page.
7. Can Playwright test e-commerce filters?
Yes. Playwright can interact with checkboxes, dropdowns, buttons and other filter controls, then validate the resulting product list.
Conclusion
Search result validation is more than checking whether products appear on a webpage.
A complete test should verify the accuracy of results, product information, selected filters, sorting order and empty-state behaviour.
Playwright makes these checks easier through locators, browser interactions and auto-retrying assertions.
When these scenarios are included in your automation framework, you can identify search-related defects earlier and improve the reliability of your application.
To understand the implementation side of search automation, read our companion article: How to Test Search Functionality With Playwright.
Explore more through Playwright Masters.

Comments
Post a Comment