Mocha: TypeError: test.describe is not a function when using nodejs, selenium and mocha

Created on 23 May 2018  路  5Comments  路  Source: mochajs/mocha

I am getting TypeError: test.describe is not a function when using 'test' with describe, it, before etc.

Using node, selenium and mocha to run tests.

Please find the code below:

Test that I am executing:

    var LoginObj = require('../pages/Login.js');
    var HomeObj = require('../pages/Home.js');
   var LoginData = require('../testdata/LoginData.json');
   var using = require('jasmine-data-provider');
   var basetest = require('../pages/BaseTest.js');
   var test = require('selenium-webdriver/testing')
   var driver = basetest.getDriver();

     test.describe('Checking  Login Functionality', function() {

        test.beforeEach(function(){
            driver.get('https://applicationurl.com');
           driver.manage().window().maximize();
         //driver.manage().timeouts().implicitlyWait(30000);

            });

       LoginData.forEach(function(data, username, password) {
       test.it('Login with: '+data.username+" and "+data.password, function() {

         //LoginObj.get();

          LoginObj.login(data.username, data.password, data.answer);

         assert.isTrue(HomeObj.isLogoutDisplays(),'Login successful, Passing 
         Test!')
         console.log('Login flag is: '+HomeObj.isLogoutDisplays()+', Failing 
      Test!');



               })

      });

           test.afterEach(function() {

               HomeObj.logout();


                  }) 

   });

Is that anything I am lacking somewhere? or it's an issue., I have installed mocha using npm install mocha --save and running my test using npm test.

integration question

Most helpful comment

I believe that selenium-webdriver/testing v4.0.0-alpha.1 removed these API.

Changes to testing/index
Since the promise manager has been removed, it is no longer necessary to wrap the Mocha test hooks; instead, users can simply use async functions. The following have all been removed:
describe
before
beforeEach
after
afterEach
it

It's not a mocha issue.
I have no experience with selenium-webdriver/testing, but it may work if you use selenium-webdriver/testing 3.x.

All 5 comments

I am sorry but I am not following where is the usage mocha in the code you provided?

If using "mocha", it would be describe(), not test.describe().
Same for beforeEach() and afterEach().

@Bamieh @plroebuck , describe is already used and 'test' is basically comes from var test = require('selenium-webdriver/testing'), which is actually used when executing tests with selenium and mocha .

I have used this implementation after referring so many articles on the web.

some of them are:
https://blog.testproject.io/2017/06/07/javascript-testing-with-selenium-webdriver-mocha/
https://simpleprogrammer.com/selenium-with-node-js/
https://dzone.com/articles/selenium-nodejs-and-mocha

May be I am lacking somewhere. However If I remove 'test' and run using npm test, tests execute but all of them faililng. Might be mocha runner execution completes before invoking my test flow in browser.

I am getting this in console:

D:\Day_User\Rohit\Eclipse\Workspace\Mochatest>npm test

[email protected] test D:\Day_User\Rohit\Eclipse\Workspace\Mochatest
mocha tests/AllevaLoginTest2.js

Checking Alleva Login Functionality
1) Login with: rohitnegi and Test@12345
2) Login with: testuser and Test@12345

0 passing (28ms)
2 failing

1) Checking Alleva Login Functionality
Login with: rohitnegi and Test@12345:
ReferenceError: assert is not defined
at Context. (tests\AllevaLoginTest2.js:25:3)

2) Checking Alleva Login Functionality
Login with: testuser and Test@12345:
ReferenceError: assert is not defined
at Context. (tests\AllevaLoginTest2.js:25:3)

DevTools listening on ws://127.0.0.1:12555/devtools/browser/694d41b2-857e-4664-9d3c-6be7904a4d06
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@ng-model,'UserName')]"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2592) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate
the Node.js process with a non-zero exit code.
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@ng-model,'UserName')]"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@ng-model,'Password')]"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@ng-model,'Password')]"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='submit' and @value='Log In']"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 5)
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='submit' and @value='Log In']"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@ng-model,'twoFactorModel.answer')]"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 7)
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//input[contains(@ng-model,'twoFactorModel.answer')]"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 8)
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='submit' and @value='Proceed']"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 9)
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='submit' and @value='Proceed']"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 10)
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//a[text()='Logout' and @class='logoutbtn']"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 11)
(node:2592) UnhandledPromiseRejectionWarning: NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"//a[text()='Logout' and @class='logoutbtn']"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.2.9200 x86_64)
at Object.checkLegacyResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (D:\Day_User\Rohit\Eclipse\Workspace\Mochatest\node_modules\selenium-webdriver\lib\http.js:468:26)
at
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:2592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 12)
npm ERR! Test failed. See above for more details.

Let me know if anything is needed from my end.

I believe that selenium-webdriver/testing v4.0.0-alpha.1 removed these API.

Changes to testing/index
Since the promise manager has been removed, it is no longer necessary to wrap the Mocha test hooks; instead, users can simply use async functions. The following have all been removed:
describe
before
beforeEach
after
afterEach
it

It's not a mocha issue.
I have no experience with selenium-webdriver/testing, but it may work if you use selenium-webdriver/testing 3.x.

We had the same issue when we run mocha using ts-node in our node + typescript serverless project.

Our tsconfig.json had "sourceMap": true . So generated, .js and .js.map files cause some funny transpiling issues (similar to this). When we run mocha runner using ts-node. So, I will set to sourceMap flag to false and deleted all .js and .js.map file in our src directory. Then the issue is gone.

If you have already generated files in your src folder, commands below would be really helpful.

find src -name "*.js.map" -exec rm {} \;
find src -name "*.js" -exec rm {} \;
Was this page helpful?
0 / 5 - 0 ratings