You click a button to turn on a lamp. The button works, but the lamp stays off. A test that clicks and then stops cannot see this bug. It passes anyway.
A Selenium test has a shape you will write hundreds of times.
Reading example. Reading Desk is a fictional app. You can read this example here. The exercise below uses a different app.
const { Builder, By, until } = require('selenium-webdriver');
const assert = require('assert');
describe('Reading Desk lamp', function () {
it('turns the lamp on when you press the button', async function () {
const driver = await new Builder().forBrowser('chrome').build();
await driver.get('/');
await driver.findElement(By.css('[data-testid="lamp-button"]')).click();
const message = await driver.wait(
until.elementLocated(By.css('[data-testid="lamp-message"]')),
5000
);
assert.strictEqual(await message.getText(), 'Lamp on');
await driver.quit();
});
});
Read it as five layers:
describeanditcome from the test runner (Mocha here). A test runner is the tool that finds your tests and runs them.itis one test. Its name is a claim about the app. "turns the lamp on when you press the button" tells a reader exactly what broke when it fails.- Setup.
build()starts the browser.get('/')loads the page. - Act like a user. Find the lamp button and click it.
- Wait, then assert.
driver.wait(until.elementLocated(...), 5000)checks again and again until the message exists, for up to 5 seconds. Thenassert.strictEqual(...)states what must be true: the message says "Lamp on". The assert is the test. Without it, the code runs, clicks, finishes, and passes whether the lamp works or not. - Teardown.
quit()ends the session. Always.
That fourth point is the difference between code that runs and a test that proves something. A test with no check passes against a completely broken app.
A test file can contain fourteen tests, every one clicking through a form and finishing, with no checks anywhere. The suite is green. It has never tested anything.
When you solve problems here, we check this for real: we break the app on purpose, and your test must turn red.
Your exercise. In Nimbus, submit the login form with both fields empty. Check that Email shows an error. Check that Password shows an error too. Use the same five layers as the lamp test.
These lessons assume you can write a simple loop. If you cannot, start with the JavaScript guide to loops. Come back when you can read each value in a list.
You can try the shop exercise for free. The subtotal forgets the quantity.
Course members can continue with Choose data that exposes a bug.
Remember this. After an action, check what the user should see. A test without a check proves nothing.
The official first-script guide: Your first Selenium script.