0

I'm working with nightwatch js and have issues calling .then method on promise from a different js file.

I've a getCount.js file.

module.exports.command =  function () {

    return  new Promise((resolve,reject) => {
        var localSelector = 'div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody';
        var rowSelector = localSelector + ' > tr';
        this.elements('css selector', rowSelector, function (result) {
            if (result.status) {
                console.log("Inside IF do promise " + result.value.length)
                reject(result.status);
            } else {
                console.log("Inside ELSE do promise " + result.value.length)
                resolve(result.value.length);
            }
        });
    });

};

and here is my home.js where I'm trying to call my promise. However getting below error

module.exports = {

    url: 'http://localhost:8080/',

    elements: {

        servicePageTitle :{
            selector : 'div.v-card__title > div > span:nth-child(2)'
        },
        tableBody :{
            selector : 'div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody'
        }
    },

    commands: [{
        clickService(selector,serviceName){
            return this
            .searchTable(selector,serviceName)
        },

        testPromise(){
            this.getCount().then(function(v){
                console.log("Total is " + v)
            });
        }
    }]
};

Calling::

const page = browser.page.home();
page.navigate().testPromise();

Now my total is undefined.. Here is output:

Running:  Dashboards:firstTest

Inside ELSE do promise 10
Total is undefined
No assertions ran.

Thanks in advance!

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Tajinder Singh
  • 205
  • 1
  • 5
  • 12
  • 2
    Where does `this.getCount` come from? – Bergi Feb 24 '20 at 01:10
  • getCount is a custom command that I've created as a part of getCount.js https://nightwatchjs.org/guide/extending-nightwatch/ – Tajinder Singh Feb 24 '20 at 01:23
  • 1
    where is `this.getCount` defined in the *code in the question* (rather than some random external link) – Jaromanda X Feb 24 '20 at 01:37
  • Basically the way custom commands works in nightwatch.js is you call them with a filename …. sorry, i'm new to both nightwatch and javascript that is why I pasted the link. – Tajinder Singh Feb 24 '20 at 01:52
  • so, what you're saying is that `module.exports.command = function () {` in the first code snippet, creates a `this.getCount` function? so clearly the `this.getCount()` is working, but not returning a promise - are you sure `this.getCount` isn't some other code you've written? – Jaromanda X Feb 24 '20 at 02:04
  • if you put `console.log(typeof this.getCount())` in `testPromise(){` what does it output? – Jaromanda X Feb 24 '20 at 02:06
  • Yes you got it right. I'm 100% sure I don't have any other getCount method. – Tajinder Singh Feb 24 '20 at 02:07
  • output of console statement is object – Tajinder Singh Feb 24 '20 at 02:09
  • How are you calling `testPromise()`? The value of `this` will depend on how you call `testPromise`. It is highly likely that you want to call `getCount` instead of `this.getCount` because if you call `testPromise` like: `module.commands[0].testPromise()` then I don't see any method `getCount` in the testPormise object. But only if you call it that way. If you call it another way `this` may point to something else. See this answer to see how `this` works: https://stackoverflow.com/questions/13441307/how-does-the-this-keyword-in-javascript-act-within-an-object-literal/13441628#13441628 – slebetman Feb 24 '20 at 02:21
  • I'm calling it like this … const page = browser.page.home(); page.navigate().testPromise(); where testpromise is a method of home.js – Tajinder Singh Feb 24 '20 at 02:43
  • I've updated the code, seems like its not complaining about .then any more but the return value is underfined... and from the console we can clearly see that inside the getCount/promise object the output is correct, but somehow its undefined inside the then... – Tajinder Singh Feb 24 '20 at 03:19
  • 1
    The problem is in code that you are not including in your question. The way that you are using this in your code is an anti-pattern, because it relies on how those functions are assembled (which you don't show). As a rule, never use `this` to refer to something in another file, for the sake of clarity and maintenance. – Josh Wulf Feb 24 '20 at 03:30
  • I've included everything i.e. all the files I;ve – Tajinder Singh Feb 24 '20 at 03:48
  • "Promise and .then are in different files" is a very very strange statement. – Roamer-1888 Feb 24 '20 at 14:25

0 Answers0