Running Your First WebDriver Recipe
Understand how to execute your first Selenium WebDriver test using Node.js and the Mocha framework. Learn the role of browser drivers like chromedriver, how to run tests manually and automatically, and familiarize yourself with the key directory structure for organizing your test files.
We'll cover the following...
First Selenium recipe
Having learned all the basics, let’s move to the practical working of Selenium. Take a look at the following piece of code:
Here, lines 1 to 3 imports Selenium for executing the 'User Authentication' test, and lines 5 to 7 tells Selenium to acquire the chromedriver. This is an example of a helping driver, which is necessary for enabling Selenium to work with the browser. Each browser has its own WebDriver available) and provides special instructions for opening that browser in a terminal that is without GUI.
Running the recipe
Throughout this course, all the recipes will be in execution ready state, i.e., you just need to click on the Run button and the rest is taken care of. However, if you do want to experiment with the code yourself, then there are certain things you should be aware of.
First of all, let’s discuss the directory structure. The directory structure of our course is: root\recipe\test.
You will start in the test directory. When you first click the RUN button, the terminal will automatically execute the underlying recipe. However, for any subsequent run, you have to handle it manually. Clicking on the RUN button will save your changes, but the terminal won’t run the recipe automatically this time. Instead, you have to type the following command in the /recipes/test directory to see the output:
mocha <space> <filename.js>
Now for practice, run the below recipe with the Run button, and observe the terminal result. Once the test is executed, go to the first_recipe.js file, and comment out the Invalid user test case and run again. This time, nothing will happen. To see the effects, type the following command:
mocha first_recipe.js
const webdriver = require('selenium-webdriver');
const chromeCapabilities = webdriver.Capabilities.chrome();
chromeCapabilities.set('chromeOptions', {args: ['--headless', '--disable-gpu','--no-sandbox','--disable-dev-shm-usage']});
var test = require('selenium-webdriver/testing');
var assert = require('assert');
var driver;
const timeOut = 15000;
test.describe('User Authentication', function () {
test.before(function() {
this.timeout(timeOut);
driver = new webdriver.Builder()
.forBrowser('chrome')
.withCapabilities(chromeCapabilities)
.build();
});
test.beforeEach(function() {
this.timeout(timeOut);
driver.get('http://travel.agileway.net');
});
test.after(function() {
driver.quit();
});
test.it('Invalid user', function() {
driver.findElement(webdriver.By.name('username')).sendKeys('agileway');
driver.findElement(webdriver.By.name('password')).sendKeys('badpass');
driver.findElement(webdriver.By.name('commit')).click();
});
test.it('User can login successfully', function() {
driver.findElement(webdriver.By.name('username')).sendKeys('agileway');
driver.findElement(webdriver.By.name('password')).sendKeys('testwise');
driver.findElement(webdriver.By.name('commit')).click();
driver.getTitle().then( function(the_title){
assert.equal("Agile Travel", the_title);
});
});
});Another important directory to keep in mind is the site directory, present at 'root/site'. This directory contains the test HTML files, which we need for later recipes.