[JS][Mocha] - Beginner's help wanted

Hello there,

I’ve been spending the last couple of weeks working on an automatisation project, using appium & the javascript webdriver. I’m also using mocha to run the tests. All I’m doing is based on the sample code available on github.

Now that I’m able to find and click elements, I need to go to the next step and start writing scripts that will be able to browse my apps and accurately detect errors.

While working on this I encountered a number of problems. I believe most are rather simple, yet I was unable to overcome them despite reading various documentation on the webdriver, promises, mocha, chai-as-promised. It appears that most tutorials and forum posts are for the Java webdriver, so that doesn’t help me much.

Here is what I’m trying to achieve

  1. Find element1 → Fail test if not found → click otherwise
  2. if element2 → do various stuff (ex: scroll a bit)
  3. If any fail → take screenshot

Step 1 is ok, however I’m having trouble doing 2. and 3.

Code for step1 (works just fine)

Note: I didn’t include the initialization of driver or “describe” & “it” blocks for mocha. Basically all the following is in one “it” block.

return driver

Step 2. Detect the presence of a banner, if it’s there I need to scroll down


  1. it will fail the test if banner is not present, I don’t know how to do a simple if/else with this architecture
  2. it doesn’t scroll. I do see the call on appium side though, and it answers status 200

info: → POST /wd/hub/session/98c63d7d-ee4c-456a-96dc-c6bb981e58e7/touch/perform {“actions”:[{“action”:“press”,“options”:{“x”:360,“y”:360}},{“action”:“wait”,“options”:{“ms”:500}},{“action”:“moveTo”,“options”:{“x”:360,“y”:0}},{“action”:“release”,“options”:{}}]}
info: ← POST /wd/hub/session/98c63d7d-ee4c-456a-96dc-c6bb981e58e7/touch/perform 200 1004.205 ms - 76 {“status”:0,“value”:true,“sessionId”:“98c63d7d-ee4c-456a-96dc-c6bb981e58e7”}

            console.log('a banner was found on hp');
            return el.getSize().then(function(bannerSize){
                console.log('Banner size: '+JSON.stringify(bannerSize));
                return driver.swipe({startX: bannerSize.width/2, startY: bannerSize.height , endX: bannerSize.width/2, endY: 0, duration: 500})

Step 3. Take a screenshot if an error occured

According to the promises documentation I can do that by simply listening to the onRejected handler at the end of the chain of promises. I still need to throw the error for mocha to report it.
The problem I’m facing is that in current example I don’t wait for the screenshot to be done before throwing the error, which obviously leads to early termination of the script and no screenshot. I’ve tried other variations where I throw the error after I’m fully done with the screenshot thing. It did save the screenshot but then the errors were not properly caught anymore.

        // this will be called if there was any error above
        .then(null, function(err){
            //console.log('taking screenshot');
            // take a screenshot
            driver.takeScreenshot(function(err, screenshot) {
                if(!err) {
                    //console.log('saving screenshot');
                    fs.writeFileSync('error_screenshot.png', screenshot, 'base64');
                } else {
                    //console.log('error taking screenshot: ' + err);

            throw err;

Thanks for reading. Any suggestions, examples, hints would be greatly appreciated !