-2

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 }

When I'm trying to return {add, mul} from add.js file to main.js the values are not returned instead it displays {add: undefined, mul: undefined}.

I'm using Webdriverio - Mocha framework

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 4
    Im skeptical. If your import wasnt working you'd get an error when trying to call `add`. The output you say you have implies the method was called, but the implementation was different to what you have here. – Jamiec Mar 27 '18 at 07:30
  • Do you have anything else in your `add.js`? Perhaps something else overwriting the `add` assignment? – CertainPerformance Mar 27 '18 at 07:30
  • Yes Exactly, The Problem is I'm unable to get the Returned value of that function – Banu Prashanth Mar 27 '18 at 07:31
  • @CertainPerformance I have few require packages & files in it but it's not overwriting the add assignment – Banu Prashanth Mar 27 '18 at 07:33
  • 1
    [mcve] - What you have isnt one. – Jamiec Mar 27 '18 at 07:35
  • Why did you tag your question with [asynchronous]? Nothing in the code you posted is async. – Bergi Mar 27 '18 at 07:39
  • @Bergi When i googled about my Issue I saw many posts which said it's because of asynchronous code so I tagged it – Banu Prashanth Mar 27 '18 at 07:44
  • 2
    I coded the exact same example above and it's working fine. Maybe other parts of the code that you have in your actual example are interfering with the `add` functions. I recommend adding a `console.log(a, b, add, mul)` before its return value to double check (in case you're not using more advanced debugging tools). – Behrooz Mar 27 '18 at 07:54
  • 1
    "Edit: It happens when add function has more number of steps to execute" — You need to provide a [mcve]. We can't help if you only show us some working code which doesn't suffer from whatever problem you have! – Quentin Mar 27 '18 at 07:58
  • 1
    your code, should returns the values normally – Slimane amiar Mar 27 '18 at 08:02
  • I've checked your example. All working fine. Could you please provide more information/pieces of code? – Vasyl Moskalov Mar 27 '18 at 08:02
  • `add` and `mul` do some async work in your real code, don't they? – Jamiec Mar 27 '18 at 08:20
  • @Quentin Please check the code now – Banu Prashanth Mar 27 '18 at 09:16
  • Duplicate: https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Quentin Mar 27 '18 at 09:21
  • Well your code couldnt have really been further from what you posted originally. Now you know why we ask for a [mcve]. Lesson learned. – Jamiec Mar 27 '18 at 11:39

1 Answers1

0

Looks like add.js is running asynchronous code. Try having add.js return a Promise instead, using Promise.all to ensure the population of both add and mul, and awaiting it in main.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);
})();
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320