Donghyeon Lee Donghyeon Lee - 1 year ago 92
Node.js Question

Mocha and Selenium WebDriver error thrown while assertion

I developing a web program with TDD methodology. Therefore, tests are using Selenium WebDriver library. But I wonder my program doesn't work correctly. Following is source codes.


<h1 class="hello">Hello, world!</h1>


'use strict'
var assert = require('assert')
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until

var driver;

before(() => {
driver = new webdriver.Builder()

describe('Index page', () => {
before(() => {

it('should show hello greetings', () => {
let hello = driver.findElement(By.css('h1.hello'))
assert.equal(hello.getText(), 'Hello, world!')


"name": "foobar",
"version": "1.0.0",
"description": "",
"main": "index.js",
"devDependencies": {
"mocha": "^2.5.3"
"selenium-webdriver": "^2.53.2"
"scripts": {
"start": "http-server public",
"test": "mocha"
"author": "",
"license": "ISC"

And I've run following command.

C:\Projects\foobar>npm install

So, when I run all tests with
npm test
command, it always failed regardless to
npm start
command was run or not.

C:\Projects\foobar>npm test

> foobar@1.0.0 test C:\Projects\foobar
> mocha

Index page
1) should show hello greetings

0 passing (62ms)
1 failing

1) Index page should show hello greetings:
AssertionError: ManagedPromise {
ControlFlow {
propagateUnhandledRejections_: true,
TaskQueue {
== 'Hello, world!'
at Context.<anonymous> (C:\Projects\foobar\test\test.js:22:16)
at callFn (C:\Projects\foobar\node_modules\mocha\lib\runnable.js:326:21)
at (C:\Projects\foobar\node_modules\mocha\lib\runnable.js:319:7)
at Runner.runTest (C:\Projects\foobar\node_modules\mocha\lib\runner.js:422:10)
at C:\Projects\foobar\node_modules\mocha\lib\runner.js:528:12
at next (C:\Projects\foobar\node_modules\mocha\lib\runner.js:342:14)
at C:\Projects\foobar\node_modules\mocha\lib\runner.js:352:7
at next (C:\Projects\foobar\node_modules\mocha\lib\runner.js:284:14)
at Immediate._onImmediate(C:\Projects\foobar\node_modules\mocha\lib\runner.js:320:5)

npm ERR! Test failed. See above for more details.

I think the main issue is in
snippet. The
method couldn't get text of hello element.

Answer Source

Most driver operations are asynchronous and return a promise, not the actual return value of that operation. In your case you should change findElement but also getText to work in that manner. Also, you should set a timeout since the default one is set to 2000 ms and most pages won't have finished loading by then. Try this:

'use strict'
var assert    = require('assert');
var webdriver = require('selenium-webdriver');

var By    = webdriver.By;
var until = webdriver.until;

var driver;

before(() => {
    driver = new webdriver.Builder()

describe('Index page',function() {


    before(() => driver.get('http://localhost:8080/') )

    it('should show hello greetings', () => 

            .then(elem => elem.getText() )
            .then(text => assert.equal(text, 'Hello, world!') )