Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
In the previous video, we showed you how to use the `then` method on the promises returned by various Selenium WebDriver methods. But the resulting code was kind of a mess. In this video, we'll show you a better way to handle those promises, a way that we'll use for the rest of this course.
In this video, we'll be using this version of the RSVP site.
In the previous video, we showed you how to use the then
method on the promises returned by various Selenium WebDriver methods. But the resulting code was kind of a mess. In this video, we'll show you a better way to handle those promises, a way that we'll use for the rest of this course.
Node.js supports the async
and await
keywords, which are a couple recent additions to the JavaScript language that make working with promises much easier. async
marks a function as asynchronous. Within asynchronous functions, you can use the await
keyword before function calls that return a promise. await
waits for the promise to resolve, then return's the promise's resulting value. It's a much cleaner way to write code that uses promises.
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() {
// In order to use the await keyword, we need to indicate this function is
// asynchronous. We do that by adding the async keyword before the
// function keyword.
it('has invitee list', async function() {
// Calling build() to build a driver still returns a promise. But instead
// of calling then() on that promise, we can place the await keyword right
// before the call that returns the promise. JavaScript will pause the
// execution of the asynchronous function, and wait for the promise to
// resolve. When it does, await will return the resolved promise's value,
// that is, the new driver object. We then assign that object to a variable
// named driver.
let driver = await env.builder().build();
// Same idea here. get() still returns a promise, but typing await before
// the call pauses execution until the page is retrieved and the promise
// is resolved. In this case, we don't need to assign the promise's value
// anywhere.
await driver.get(url);
// Another promise, another await keyword. When it resolves, the returned
// list of elements is assigned to the elements variable.
let elements = await driver.findElements(By.id('invitedList'));
// Now we can check the list of elements to ensure it's not empty.
assert(elements.length > 0);
// Finally, we can tell the driver to close the browser.
driver.quit();
});
});
});
If we return to the terminal and run mocha
, we'll get the same result.
This code looks a lot cleaner, right? async
and await
are a new addition to JavaScript, so they're not supported everywhere yet. A lot of the Selenium JavaScript examples out on the web still use then
. But async
and await
will be everywhere soon. The Selenium maintainers are strongly encouraging developers to start using them, so now's the time to switch.
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