lecture 12 of 200 completed

Forms and what they refuse

A login test that only tries the correct password would pass on a login that lets anyone in.

Here is a login test that most beginners write, and it hides a serious problem:

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

const welcome = await driver.wait(
  until.elementLocated(By.css('[data-testid="welcome-message"]')),
  5000
);
assert.ok((await welcome.getText()).includes('Welcome back'));

It checks that the right password lets you in. That is good.

Now imagine a developer breaks the password check, and the app accepts any password. Would this test fail?

No. The correct password still works. The test passes, and your app now lets anyone into any account.

The most important part of a form is what it refuses. So test that too:

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

const error = await driver.wait(
  until.elementLocated(By.css('[data-testid="login-error"]')),
  5000
);
assert.ok((await error.getText()).includes('Invalid email or password'));

const welcome = await driver.findElements(By.css('[data-testid="welcome-message"]'));
assert.strictEqual(welcome.length, 0);

Look at those last two lines carefully. To prove something is not there, use findElements with an "s". It returns a list, and the list is empty when nothing matches. If you used findElement instead, it would throw an error and your test would crash rather than pass.

Why check both? I have seen an app show a red error message and log the user in anyway. The screen said "Invalid password" while the dashboard loaded behind it. A test that only checked the error message passed.

Before you test any form, list what it should refuse: wrong password, empty fields, bad email format, short card number. Then write one test for each. In most real projects, those tests find more bugs than the happy path.

Remember this: use findElements and check for length 0 to prove something is absent.

Reference: Finding elements.

now prove it

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