1

I'm trying to call my function from it() but I always get return value of null. I thought my function assigns the value to the return variable back to the caller.

file: "helperDropDownBx.js"

module.exports = function() {
  function myFunctionTest() {
    var reTxt = null;
    try {
      browser.wait(EC.visibilityOf(id_dropDownValue), 30000);
      id_dropDownValue.getAttribute("value").then(function(text) {
        retTxt = text;
      });
    } catch(err) {
      throw new Error(err.message);
    }
    return retTxt;
  }
        
  return{
    myFunctionTest : myFunctionTest
  }
}

file: "TestHelpers.js"

const myHelper = require("../pages/helpers/helperDropDownBx.js");
describe("[Test Helpers]", function(){
    var myHelperObj = new myHelper();

    it('testing Helpers', function() {        
        try{
            //attempt#1, but not working
            var retVal = myHelperObj.myFunctionTest();
            retVal.then(function (value){
               console.log(value);
            )};

            //attempt#2, but not working
             myHelperObj.myFunctionTest().then(function(value){
               console.log(value);
            )};
       }catch(err){
           throw new Error(err.message);
       }
    });
});

both of my attempts above, always return null

Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
Phil
  • 339
  • 2
  • 13
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Robin Zigmond Apr 29 '21 at 18:24
  • your export function is not a constructor function.youshould not initialize it with `new` keyword. just `var myHelperObj = myHelper();` – Yosef Tukachinsky Apr 29 '21 at 18:43
  • @YosefTukachinsky i remove the new keyword, but does not fix the issue – Phil Apr 29 '21 at 19:08
  • @RobinZigmond it seems I use the same suggestion under section "If you're using promises, this answer is for you." but it does not work for me – Phil Apr 29 '21 at 19:09
  • 1. You are way over complicating everything. 2. You do typical mistake of writing hundred of lines and then trying to figure out which one is wrong. Lets take one thing at the time. What you trying to achieve? get `value` attribute of `id_dropDownValue`? – Sergey Pleshakov Apr 29 '21 at 21:20
  • yes, i'm trying to get the selected value of id_dropDownValue. I'm able to get value inside the function myFunctionTest() because when I print it to the screen, it shows the value, but when it returns to the caller, it returns back as null with error `"Cannot read property 'then' of null"` – Phil Apr 29 '21 at 21:36

1 Answers1

1

file: "helperDropDownBx.js"

module.exports = {
  myFunctionTest: async function () {
    // this is a bad practice to start a function with wait
    // all wait should be handled after you complete an action and not before
    await browser.wait(EC.visibilityOf(id_dropDownValue), 30000); 
    return id_dropDownValue.getAttribute("value")
  }
}

file: "TestHelpers.js"

const myHelper = require("../pages/helpers/helperDropDownBx.js");
describe("[Test Helpers]", function(){

  it('testing Helpers', async function() {        
    var value = await myHelper.myFunctionTest();
    console.log(value)
  });
});

if you still curious what you needed to do with .then() to make it work, then something like this

module.exports = {
  myFunctionTest: function() {
    return browser.wait(EC.visibilityOf(id_dropDownValue), 30000)
      .then(() => {
        return id_dropDownValue.getAttribute("value")
          .then(function(text) {
            return text;
          })
      })
  }
}

Looks like hell to me :)

Sergey Pleshakov
  • 7,964
  • 2
  • 17
  • 40
  • 39 lines became 16. But this is based on assumption that you tested everything beyond that - locator is correct, you defined right condition to wait for, and you're sure you need to get `value` attribute, not anything else – Sergey Pleshakov Apr 29 '21 at 21:36
  • Not sure why this @quote does not work for me, but thanks Sergey for your help. I will try out your code. So the reason I use "await" in myFunctionTest() because I want the page with dropdown box to load everything first because it's navigating from the main dashboard page. I thought about using await and async too but i thought ".then" would work also instead it works only half the portion of the code. I'm not sure what else i need to return from the function, but I hope that is all, just the selected value from dropdown – Phil Apr 29 '21 at 22:14
  • @Phil I updated the answer with an example of what `.then` syntax would be – Sergey Pleshakov Apr 30 '21 at 13:57
  • oh my goodness. you're right. the ".then" looks so confusing. thanks for looking into this. – Phil May 03 '21 at 19:06