0

I am trying to get a better understanding on javacsript. And I am not sure why this code is not working. I am trying to create functions that will call another function. And return the results of the called function.

When I call the below, I get fully logged in and presented with the screen I desire. But jsDidLogin Always returns undefined. Is there a better way to implement my methods?

var jsDidLogin = beginLogin() console.log(jsDidLogin)

function waitUntilElementFound(element, time, callFunction) //Wait for the element to be found on the page
{
    if (document.querySelector(element) != null) {
        return callFunction();
    }
    else {
        if (!checkForFailedLogin()) {
            setTimeout(function () {
                waitUntilElementFound(element, time, callFunction);
            }, time);
        }
        else {
            return false;
        }
    }
}

function checkForFailedLogin() {
    if (document.querySelector("div[class='modal-body ng-scope'] h1") != null) {
        if(document.querySelector("div[class='modal-body ng-scope'] h1").innerHTML == "Login Error")
        {
          return true;
        }
    }
    else {
        return false;
    }
}

function initialTabSelect() //Load the bank page once login is completed
{
   document.querySelectorAll("li[class='Tab'] a")[0].click();
   return "Fully Logged In";
}
function initialDoNotAsk() {
    document.querySelectorAll("a[ng-click='modalCancel()']")[0].click();
    return waitUntilElementFound("li[class='Tab'] a", 1000, initialTabSelect);
}
function initialLogin() {
    var accountName = document.getElementById("username");
    var accountPassword = document.getElementById("password");
    var evt = document.createEvent("Events");

    evt.initEvent("change", true, true);
    accountName.value = "USERNAME";
    accountPassword.value = "PASSWORD";

    accountName.dispatchEvent(evt);
    accountPassword.dispatchEvent(evt);

    document.querySelectorAll("form[name='loginForm'] button.icon-login")[0].click();

    return waitUntilElementFound("a[ng-click='modalCancel()']", 2000, initialDoNotAsk);
}

function beginLogin() {
    return waitUntilElementFound("form[name='loginForm'] button.icon-login", 1000, initialLogin);
}

Changing to this alerts me when Fully Logged in, but if I change it to return status. I still get no returns.

My head is starting to hurt :(

    function waitUntilElementFound(element, time, callFunction, callBack) //Wait for the element to be found on the page
    {
        if (document.querySelector(element) != null) {
            callBack(callFunction());
        }
        else {
            if (!checkForFailedLogin()) {
                setTimeout(function () {
                    callBack(waitUntilElementFound(element, time, callFunction, function(status){alert(status);}));
                }, time);
            }
            else {
                return false;
            }
        }
    }

function checkForFailedLogin() {
        if (document.querySelector("div[class='modal-body ng-scope'] h1") != null) {
            if(document.querySelector("div[class='modal-body ng-scope'] h1").innerHTML == "Login Error")
            {
              return true;
            }
        }
        else {
            return false;
        }
    }

    function initialTabSelect() //Load the bank page once login is completed
    {
       document.querySelectorAll("li[class='Tab'] a")[0].click();
       return "Fully Logged In";
    }
    function initialDoNotAsk() {
        document.querySelectorAll("a[ng-click='modalCancel()']")[0].click();
        return waitUntilElementFound("li[class='Tab'] a", 1000, initialTabSelect, function(status){alert(status)};);
    }
    function initialLogin() {
        var accountName = document.getElementById("username");
        var accountPassword = document.getElementById("password");
        var evt = document.createEvent("Events");

        evt.initEvent("change", true, true);
        accountName.value = "USERNAME";
        accountPassword.value = "PASSWORD";

        accountName.dispatchEvent(evt);
        accountPassword.dispatchEvent(evt);

        document.querySelectorAll("form[name='loginForm'] button.icon-login")[0].click();

        return waitUntilElementFound("a[ng-click='modalCancel()']", 2000, initialDoNotAsk, function(status){alert(status)};);
    }

    function beginLogin() {
        return waitUntilElementFound("form[name='loginForm'] button.icon-login", 1000, initialLogin, function(status){alert(status)};);
    }
Zach Schulze
  • 304
  • 1
  • 4
  • 17
  • What's the problem? So far _"Is there a better way to implement my methods"_ is a bit too open ended. – evolutionxbox Apr 20 '17 at 14:49
  • 1
    Your best bet is to learn [how to use the debugger](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) in your browser and step through the code to see where it's failing. – Heretic Monkey Apr 20 '17 at 14:53
  • 1
    `"jsDidLogin always returns undefined"` - Well, there is at least one code path in the function you're calling which doesn't `return` anything. Seems reasonable that it's following that code path? What does your debugging reveal? – David Apr 20 '17 at 14:53
  • @David Okay, I see I missed my return on setTimeout. Thanks. I will test this out and update my post if need be. – Zach Schulze Apr 20 '17 at 15:01
  • @David Alright, I am lost on how to implement this last return. It always returns the setTimeOut process ID and not the results of the function – Zach Schulze Apr 20 '17 at 16:08
  • @ZachSchulze: The result of `setTimeOut()` *is* the identifier of that timeout. It can't return the value of an operation that hasn't happened yet. You're going to have to restructure this a bit, you seem to have painted yourself into a corner here. If you're scheduling operations to happen at a later time, then those operations are themselves going to have to invoke whatever result you want from them. – David Apr 20 '17 at 16:12
  • @David editted my code. But I think I am still having issues understanding. Do you think you might be able to show an example? – Zach Schulze Apr 20 '17 at 17:10
  • @ZachSchulze: This is all pretty broad. Perhaps instead of making minor (and unknown to me) edits to your existing code, take a step back and define what it is you're actually trying to accomplish. What high-level problem are you trying to solve? Given how you've painted yourself into a corner here, it seems likely that you've made some design mistakes prior to whatever you're doing now. – David Apr 20 '17 at 17:13

0 Answers0