Automating Dropdown Selection

Introduction to Automating Dropdown Selection

Welcome to an exciting part of our course where we delve into automating dropdown selections using Playwright. Dropdowns are common on web pages for user selections, such as choosing a category of books or filtering search results. This lesson will guide you through writing automated tests to interact with dropdowns, enhancing your Playwright skills.

Key Concepts and Techniques

In this lesson, you will learn how to automate dropdown selection with Playwright. We'll cover the process of identifying and interacting with dropdown elements on a webpage to select a specific option. Here's a sneak peek:

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

test('automate dropdown selection', async ({ page }) => {
  await page.goto('http://localhost:3000/books');
  await page.selectOption('select#categories', 'Fantasy');
  
  const selectedValue = await page.$eval('select#categories', select => select.value);
  expect(selectedValue).toBe('Fantasy');
  
  await expect(page.locator('.book-title >> text=Harry Potter and the Philosopher\'s Stone')).toBeVisible();
  await expect(page.locator('.book-title >> text=The Hobbit')).toBeVisible();
  await expect(page.locator('.book-title >> text=The Great Gatsby')).not.toBeVisible();
});

In this test, we perform the following actions:

  1. Navigate to a Web Page: Using page.goto('http://localhost:3000/books'), Playwright opens the specified URL, which hosts the dropdown we want to manipulate.

  2. Select an Option from a Dropdown: We use page.selectOption('select#categories', 'Fantasy') to automate the action of selecting the 'Fantasy' option. The CSS selector select#categories is used to fetch the 'select' element with an id of 'categories', which corresponds to an HTML element like <select id="categories"…>. This simulates the user action of choosing an option from the dropdown.

  3. Verify the Selected Value: By evaluating the current value of the dropdown with page.$eval('select#categories', select => select.value), we ensure it's set to 'Fantasy'. This serves as a check to confirm that the selection occurred as expected.

    • The page.$eval method fetches the element matching the CSS selector 'select#categories', which is the dropdown element.
    • The arrow function select => select.value then takes this element as input and returns its value attribute.
  4. Expectations for Page Content: We employ expect assertions to check that specific content is visible on the page following the selection. Specifically, we assert that elements with classes '.book-title' containing the texts 'Harry Potter and the Philosopher's Stone' and 'The Hobbit' are visible, and that 'The Great Gatsby' is not visible. This confirms that selecting 'Fantasy' filters the page contents correctly.

These steps collectively demonstrate how to automate dropdown selection in Playwright, confirming that the webpage's filtering functionality responds accurately after interaction.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal