Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Selenium by (5k points)


<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 the following command.

C:\Projects\foobar>npm install

The moment I run all of the tests with npm test command, it always fails. 

C:\Projects\foobar>npm test

> [email protected] 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 hello.getText() snippet. The getText() method couldn't get text of the hello element. Any help will be appreciated.

1 Answer

0 votes
by (10.2k points)

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(function () {

    driver = new webdriver.Builder()




describe('Index page', function () {


    before(function () {



    it('should show hello greetings', function (done) {


            .then(elem => elem.getText() )

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




Note that arrow functions don't define their own object. You have to use regular callback functions to access this.timeout. Their use is, in any case, discouraged in mocha, see the following:

Passing arrow functions to Mocha is discouraged. Their lexical binding of this value makes them unable to access the Mocha context, and statements like this.timeout(1000); will not work inside an arrow function.

If you want to become an expert in Selenium, check out this Selenium Certification offered by Intellipaat. 

Browse Categories