Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Intermediate Selenium WebDriver!
You have completed Intermediate Selenium WebDriver!
Mocha lets us define a before callback that's called before each test, and an after callback that's called after each test. We can move duplicated test code to those two callbacks.
We're going to want to add more tests for this app. Here, we've added a second test to ensure the registration form is available under the correct ID.
const {Browser, By, Key, until} = require("selenium-webdriver");
const {suite} = require("selenium-webdriver/testing");
const assert = require('assert');
const url = "https://treehouse-projects.github.io/selenium-webdriver-intermediate/waits/app/index.html";
suite(function(env) {
describe('RSVP site', function() {
it('has invitee list', async function() {
let driver = await env.builder().build();
await driver.get(url);
let elements = await driver.findElements(By.id('invitedList'));
assert(elements.length > 0);
driver.quit();
});
it('has registration form', async function() {
let driver = await env.builder().build();
await driver.get(url);
let elements = await driver.findElements(By.id('registrar'));
assert(elements.length > 0);
driver.quit();
});
});
});
- But the code to build the browser driver, load the page, and quit the browser again when the tests are done is repeated between the two tests.
- Mocha lets us define a
before
callback that's called before each test, and anafter
callback that's called after each test. We can move the duplicated code to those two callbacks.
const {Browser, By, Key, until} = require("selenium-webdriver");
const {suite} = require("selenium-webdriver/testing");
const assert = require('assert');
const url = "https://treehouse-projects.github.io/selenium-webdriver-intermediate/waits/app/index.html";
suite(function(env) {
describe('RSVP site', async function() {
// Move variable definition here so it remains in scope
let driver;
// Call before() and pass it a callback function that will be called before each test.
// We make our callback function asynchronous so we can use await within it.
before(async function() {
// Move driver building here so it happens before each test
driver = await env.builder().build();
// Need to get the page before each test too
await driver.get(url);
});
// This test (and any others defined within the describe() callback) will be run after the
// before() callback, and before the after() callback.
it('has invitee list', async function() {
// These lines are specific to this test, so we leave them here.
let elements = await driver.findElements(By.id('invitedList'));
assert(elements.length > 0);
});
// The setup code in the before() callback and the teardown code in the after()
// callback are run before each test, so we can remove the duplicated code from
// this test too.
it('has registration form', async function() {
// We leave only the code that's specific to this particular test.
let elements = await driver.findElements(By.id('registrar'));
assert(elements.length > 0);
});
// Call after() and pass it another callback function that will be called after each test.
after(async function() {
// Move code to close browser here, because it needs to be run after each test.
driver.quit();
});
});
});
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up