Forum Discussion

Novari-QA's avatar
Novari-QA
Frequent Contributor
7 years ago

Object functions returning Object, Javascript doesn't understand the type.

I have an object that holds multiple sub objects. These objects can be functions or objects. See example below.

var menu = {
  Parent: {
    Child: function() { 
      Aliases.browser.pageDashboard_PhysOffice.menuMain.HoverMouse(); 
      return Aliases.browser.pageDashboard_PhysOffice.menuMain_Main; 
    }
}


When trying to get the object above, javascript doesn't understand what I am trying to return. It doesn't crash, the code just won't work when trying to call methods based on the returned object. See below

function foo()
{
  menu.Parent.Child.Click();
}


However the code below will work:

var menu = {
  Parent: {
    Child: Aliases.browser.pageDashboard_PhysOffice.menuMain_Main; 
}

function foo() {
Aliases.browser.pageDashboard_PhysOffice.menuMain.HoverMouse();
menu.Parent.Child.Click();
}



Why is it that the function returning the value won't work, but the an object that equals the value will work.

 

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    By "won't work" what happens? Are there error messages?  What behavior do you observe.

     

    One thing that comes to mind is that "Child" in the menu.Parent object is not a property... it is a method because you have it declared as a function.  So, try the following:

    function foo(){
       var myChild = menu.Parent.Child();
       myChild.Click();
    }
  • cunderw's avatar
    cunderw
    Community Hero

    This is because you have Child defined as a function, but you're calling it like a property.

     

     

    menu.Parent.Child.Click();  
    // change that to
    menu.Parent.Child().Click();

     EDIT: Looks like tristaanogre already said that. lol

    • Novari-QA's avatar
      Novari-QA
      Frequent Contributor

      Correct that seems to have done it.

      My next question, is why isn't other classes able to see these functions? The Code below works. However when exporting the class to another, it doesn't know what UserPrefs is

      var Header = undefined;
      
      var Main = {
        UserPrefs: function() { 
          Header.menuMain.HoverMouse(); 
          return Header.menuMain_UserPrefs; 
        }
      }
      
      function foo() {
        Header = Aliases.browsers.pageDashboard_PhysOffice;
        Main.UserPrefs().Click();
      }


      Navigation.cs

       

       

      var Menu = require("Menu");
      
      function foo()
      {
        Menu.Header = Aliases.browser.pageDashboard_PhysOffice; 
        Menu.Main.UserPrefs().Click();
      }


      We have a header at the top of almost every page, I am trying to make a common class which all pages can reference when trying to navigate.

      Menu.Parent.Child.Click(); is what I am trying to create. Then I would create a single class which handles Navigation.

       

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        Out of scope... Just wondering...

        What is the reason for you to implement your test code the way that is not obvious even to you?

        Why not to revert to old good set of functions within script units?
        I think that I am missing something, but the only advantage of this approach is that 'developers do it this way'...

        But will it be easy for you and those who will use and support your code later to understand and fix it? To fix it in a tight time frame when you have a manager staying behind your chair and wondering: "Well, we deployed a new build 15 minutes ago... What is the result of your tests run? Did tests find anything? Can you tell me something that may prevent further deploy to production?"
        Another point: how easy will it be to port your code to another scripting language? Within the course of tests automation, you will create generic functions that will do generic things that are common to any web project (e.g. scroll the object into view, wait until the object is populated with data, download file, process login dialog, etc.) If you are not going to spend all your days with the current company for this given project, is there any good reason to put yourself in a position when you will need to create generic code for any new script language a-new instead of just adjusting its syntax?

        And one more point: neither intellisense nor the navigational combo on top of the code editor window in TestComplete support display of internal properties and functions for the classes. This means that you will have to memorize or somehow else document what functionality exists in your test code. Will this simplify and speed-up new code creation?

         

        And sorry for not been helpful on the topic...

  • Novari-QA's avatar
    Novari-QA
    Frequent Contributor

    Well it is working now, I am not sure why. But i am not complaining.  If anyone thinks this method is silly please comment