Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Quality Assurance Intermediate Selenium WebDriver Use Selenium with Testing Frameworks The async and await Keywords

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Mocha test failing on asynchronous testing

Anyone able to decipher these testing results?

$ mocha
[INFO] Searching for WebDriver executables installed on the current system...
[INFO] ... located chrome
[INFO] Running tests against [chrome]


  [chrome]
    RSVP site

DevTools listening on ws://127.0.0.1:12688/devtools/browser/409013eb-5398-4c42-a520-a5b113a79a41
      1) has invitee list


  0 passing (2s)
  1 failing

  1) [chrome]
       RSVP site
         has invitee list:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\xamp\htdocs\intermediate-selenium\s1v4\test\invitees.js)

I thought it was maybe because I was trying to run multiple files in a for a single test on mocha but I brought this into its own directory and it's the same result. Am I missing another package? Do I need to incorporate the done() method somehow?

invitees.js
// Run `node`, demo `require("selenium-webdriver")`
const {Browser, By, Key, until} = require("selenium-webdriver");
const {suite} = require("selenium-webdriver/testing");
const assert = require('assert');

//connect to URL or "localhost"
const url = "https://treehouse-projects.github.io/selenium-webdriver-intermediate/waits/app/index.html";

suite(function(env) {
    describe('RSVP site', function() {
        it('has invitee list', async function() {
            let driver = await env.builder().build();
            await driver.get(url);
            let elements = await driver.findElements(By.id('invitedList'));
            assert(elements.length > 0);
            driver.quit();
        });
    });
});

3 Answers

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Hmm, odd that you're encountering this with a locally-hosted app and the Chrome driver. I've only encountered this with a remote app and Firefox.

Mocha tests have a default timeout of 2000 milliseconds. (This is separate from the WebDriver timeout.) Try setting it to 3000 or 4000 and see if that helps. https://mochajs.org/#timeouts Would be curious to know your results, and what timeout value you wound up having to use.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hmmm. Still no joy.

This is the code I tried and the result in the console. Incidentally doing this in Integerated Terminal of Visual Studio code at least loads a page in the test window but in Git Bash on Windows it only loads a blank test window.

mocha
[INFO] Searching for WebDriver executables installed on the current system...
[INFO] ... located chrome
[INFO] Running tests against [chrome]


  [chrome]
    RSVP site

DevTools listening on ws://127.0.0.1:12536/devtools/browser/b83ddaa6-01ec-456a-a302-efd907b2f8c4
      1) has invitee list


  0 passing (4s)
  1 failing

  1) [chrome]
       RSVP site
         has invitee list:
     Error: Timeout of 4000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\xamp\htdocs\intermediate-selenium\s1v4\test\invitees.js)
invitees.js
mocha
[INFO] Searching for WebDriver executables installed on the current system...
[INFO] ... located chrome
[INFO] Running tests against [chrome]


  [chrome]
    RSVP site

DevTools listening on ws://127.0.0.1:12536/devtools/browser/b83ddaa6-01ec-456a-a302-efd907b2f8c4
      1) has invitee list


  0 passing (4s)
  1 failing

  1) [chrome]
       RSVP site
         has invitee list:
     Error: Timeout of 4000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\xamp\htdocs\intermediate-selenium\s1v4\test\invitees.js)
Jay McGavren
Jay McGavren
Treehouse Teacher

Jonathan Grieve your code didn't get included; can you paste again?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Apologies, Jay :)

Here's the file.

invitees.js
// Run `node`, demo `require("selenium-webdriver")`
const {Browser, By, Key, until} = require("selenium-webdriver");
const {suite} = require("selenium-webdriver/testing");
const assert = require('assert');

//connect to URL or "localhost"
const url = "https://treehouse-projects.github.io/selenium-webdriver-intermediate/waits/app/index.html";

suite(function(env) {
    describe('RSVP site', function() {
        this.timeout(4000);

        it('has invitee list', async function(done) { 
            setTimeout(done, 4000);   
            let driver = await env.builder().build();
            await driver.get(url);
            let elements = await driver.findElements(By.id('invitedList'));
            assert(elements.length > 0);
            driver.quit();
        });
    });
});
Jay McGavren
Jay McGavren
Treehouse Teacher

Removing the done parameter from it('has invitee list', async function() {/*...*/}) seems to have fixed your code for me. It works without the this.timeout(4000); on Chrome, but only with the this.timeout(4000); on Firefox.

I'm also not sure how the setTimeout(done, 4000); line was meant to work; I had to remove that as well.

// Run `node`, demo `require("selenium-webdriver")`
const {Browser, By, Key, until} = require("selenium-webdriver");
const {suite} = require("selenium-webdriver/testing");
const assert = require('assert');

//connect to URL or "localhost"
const url = "https://treehouse-projects.github.io/selenium-webdriver-intermediate/waits/app/index.html";

suite(function(env) {
    describe('RSVP site', function() {
        this.timeout(4000);

        it('has invitee list', async function() {
            let driver = await env.builder().build();
            await driver.get(url);
            let elements = await driver.findElements(By.id('invitedList'));
            assert(elements.length > 0);
            driver.quit();
        });
    });
});
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

I'll have to look at this closer tomorrow, but I was experimenting with the timeout methods based on the examples from the docs. I'll be sure to try it the way you directed when I get back to my machine I've been working from. :)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Maybe I need to reinstall Mocha?

I'm not doing any tests with Firefox since I believe I got to the point in the course it was recommended we stop testing with the Gecko driver.

However, I've tried every variation I can think of with the same response in the console.

So, my next questions are maybe about reinstalling mocha and secondly, I do see that the page is loading when I run mocha before it then quits as directed. Should that be happening of there are failing tests?

Jay McGavren
Jay McGavren
Treehouse Teacher

Actually, we don't recommend uninstalling the Gecko/Firefox driver. I only did that in the videos to keep the output shorter. I'd encourage you to run with as many drivers and browsers as your system supports, so you can ensure your code works with all of them.

driver.quit() will definitely be called when you move it to an after() callback as demonstrated in a later video. I would not expect it to be called with the code shown here, though; I would expect the test to stop cold once the assertion failed. Perhaps that's a hint at another issue.

You can try reinstalling Mocha and/or your browser drivers, if it's convenient to do so. At this point I'm kinda grasping at straws. I'd recommend trying additional browsers/drivers first, though.

I upped the timeout to 6000 and that worked for me.

after describe I added this.timeout(6000); this worked for me. Now all the test are passing!