【问题标题】:I'm unable to return the Value from the Function in one file to another file in Node.js, I'm getting undefined我无法将一个文件中的函数的值返回到 Node.js 中的另一个文件,我得到未定义
【发布时间】:2018-03-27 07:23:37
【问题描述】:

add.js

    var webdriverio = require('webdriverio');

    function add(a, b) {
        var add;
        var mul;

        describe('This is from add.js file', function() {
            this.timeout(50000);
            var driver = {};

            before(function() {
                driver = webdriverio.remote({
                    desiredCapabilities: {
                        browserName: 'chrome',
                        chromeOptions: {
                            args: ['--start-maximized']
                        }
                    }
                });
                return driver.init();
            });
            it('Example1', function() {
                return driver.url("https://www.google.co.in/").getText("//a[text()='Gmail']").then(function(text) {
                    add = (text);
                });
            });
            it('Example2', function() {
                return driver.getText("//a[text()='Images']").then(function(text) {
                    mul = (text);
                });
            });

        });
        console.log({
            add,
            mul
        }); //{ add: undefined, mul: undefined }
        return {
            add,
            mul
        };
    }

    module.exports = add;

main.js

    var webdriverio = require('webdriverio');
    var add = require("./add.js");

    var d = add("G", "I");

    describe('This is from Main.js file', function() {
        this.timeout(50000);
        var driver = {};

        before(function() {
            driver = webdriverio.remote({
                desiredCapabilities: {
                    browserName: 'chrome',
                    chromeOptions: {
                        args: ['--start-maximized']
                    }
                }
            });
            return driver.init();
        });
        it('Example1', function() {
            return driver.url("https://www.google.co.in/").getText("//a[text()=" + d.add + "]").then(function(text) {
                add = (text);
            });
        });
        it('Example2', function() {
            return driver.getText("//a[text()=" + d.mul + "]").then(function(text) {
                mul = (text);
            });
        });

    });

    console.log(d); //prints { add: undefined, mul: undefined }

当我尝试将 return {add, mul} 从 add.js 文件转到 main.js 时,不会返回值,而是显示 {add: undefined, mul: undefined}

我正在使用 Webdriverio - Mocha 框架

【问题讨论】:

  • 我持怀疑态度。如果您的导入不起作用,则在尝试调用 add 时会出现错误。您所说的输出表明该方法已被调用,但实现与您在此处的不同。
  • minimal reproducible example - 你所拥有的不是一个。
  • 我编写了与上面完全相同的示例,并且运行良好。也许您在实际示例中拥有的代码的其他部分正在干扰 add 函数。我建议在返回值之前添加一个console.log(a, b, add, mul) 以进行仔细检查(以防您没有使用更高级的调试工具)。
  • “编辑:当添加函数有更多的步骤要执行时会发生这种情况” - 您需要提供minimal reproducible example。如果您只向我们展示一些不会遇到任何问题的工作代码,我们将无能为力!
  • 你的代码,应该正常返回值

标签: javascript node.js mocha.js webdriver-io


【解决方案1】:

看起来add.js 正在运行异步代码。尝试让 add.js 返回一个 Promise,使用 Promise.all 来确保 addmul 的数量,以及 awaitmain.js 中的填充:

function add(a, b) {
  return new Promise((resolveAdd, rejectAdd) => {
    describe('This is from add.js file', function() {
      this.timeout(50000);
      var driver = {};

      before(function() {
        driver = webdriverio.remote({
          desiredCapabilities: {
            browserName: 'chrome',
            chromeOptions: {
              args: ['--start-maximized']
            }
          }
        });
        return driver.init();
      });
      const example1Promise = new Promise(resolveP1 => {
        it('Example1', function() {
          return driver.url("https://www.google.co.in/").getText("//a[text()='Gmail']").then(resolveP1);
        });
      });
      const example2Promise = new Promise(resolveP2 => {
        it('Example2', function() {
          return driver.getText("//a[text()='Images']").then(resolveP2);
        });
      });
      Promise.all(example1Promise, example2Promise).then(([add, mul]) => {
        resolveAdd({ add, mul });
      });
    });
  });
}

module.exports = add;

main.js:

(async () => {
  const d = await add("G", "I");
  console.log(d);
})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    相关资源
    最近更新 更多