首页 > 解决方案 > .then 不是函数 - Promise 和 .then 在不同的文件中

问题描述

我正在使用 nightwatch js,并且在从不同的 js 文件中调用 .then 方法时遇到问题。

我有一个 getCount.js 文件。

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);
            }
        });
    });

};

这是我的 home.js,我试图兑现我的承诺。但是低于错误

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)
            });
        }
    }]
};

呼叫::

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

现在我的总数未定义..这是输出:

Running:  Dashboards:firstTest

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

提前致谢!

标签: javascriptnode.jspromisenightwatch.js

解决方案


推荐阅读