lecture 7 of 200 completed

Acting on the page

click, sendKeys, clear and Select. The element must be found before it can be used.

Every test is made of two things: actions and checks. You have seen checks already. Now let's cover the actions.

In Selenium an action always has two steps: find the element, then act on it.

await driver.findElement(By.css('[data-testid="email-input"]')).sendKeys('demo@nimbus.app');
await driver.findElement(By.css('[data-testid="login-button"]')).click();

The actions that cover most user behaviour:

  • sendKeys(text) types into a field. Important: it appends. If the field already has text, clear it first:
const field = await driver.findElement(By.css('[data-testid="qty"]'));
await field.clear();
await field.sendKeys('3');
  • click() clicks. The element must be visible and enabled at that moment, Selenium does not wait for that by itself. If the element appears after a delay, wait first (driver.wait(until.elementLocated(...))), then act.
  • Dropdowns: for a real <select>, the Select helper class picks options by visible text or value. For custom dropdowns (divs styled as menus), you click them like any other elements.
  • sendKeys(Key.ENTER) submits many forms, const { Key } = require('selenium-webdriver').

The discipline Selenium teaches, which the other tools hide: know what state the page is in before you act. A click on an element that is not there yet throws NoSuchElementException; a click on a covered element fails. When an action fails, first ask "was the page ready?", and if not, wait for the condition that makes it ready, never for a fixed time.

The full interactions reference: Interacting with web elements.

The problem below is pure actions-and-assertions: remove the last item from the cart and prove the empty state appears.

now prove it

Reading is the setup. Solve this problem in the editor. We break the app on purpose to check your test would catch it.