Ask a Question

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

Novari-QA
Frequent Contributor

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.

 

13 REPLIES 13
tristaanogre
Esteemed Contributor


@Novari-QA wrote:

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.

 


 

It depends upon how you are doing the modules.export and the requires calls.  I have a "toy" project that I use to explore creating custom JavaScript classes and have them called across code units and, when implementing such a thing in TestComplete, there were some "hoops" I needed to jump through to get it to work properly.

 

Could you share the entire code units, complete with the exports calls and such?


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
Novari-QA
Frequent Contributor

Certainly
Foo function works, where abc function do not.


image.pngimage.pngimage.png

Novari-QA
Frequent Contributor

a

tristaanogre
Esteemed Contributor

The import functions feature of TestComplete does not do the necessary tasks for JavaScript.  In test, you need to add 

 

module.exports = Test

at the end of the code unit that has the class you want to export.  You can then instantiate and use that class in any unit that has 

 

var Test = requires("Test")

in the beginning.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
cancel
Showing results for 
Search instead for 
Did you mean: