cancel
Showing results for 
Search instead for 
Did you mean: 

how can we pass an object defined in one unit to another

SOLVED
Occasional Contributor

how can we pass an object defined in one unit to another

how can we pass an object defined in one unit to another in java csript.

 

I have imported the unitA(where the object is declared) in Unit B where i need to use the object.

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: how can we pass an object defined in one unit to another

Hi,

 

You need to return required value from the function like it was advised by @Vinicius.

Like this:

//OpenFB unit

function openurl()
{
  var browser;
  var page;

  Browsers.Item(btChrome).Run("https://www.facebook.com/");
  browser = Sys.browser();
  page = browser.Page("*facebook.com*");
 // page.NativeWebObject.Find("name","websubmit","BUTTON").Click();  this code is working when executing from unit OpenFB

 

  return page; // <== added line
}

 

//Unit B

 

//USEUNIT OpenFB
function mandatory_check()
{

    var page;

 

    page = openurl();   // <== OpenFB. is not required because of //USEUNIT OpenFB directive
    page.NativeWebObject.Find("name", "websubmit", "BUTTON").Click();
 }

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================

View solution in original post

7 REPLIES 7
Community Hero

Re: how can we pass an object defined in one unit to another

Hi,

 

Can you post here your code so that others can see exact implementation and be able to share their ideas?

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Occasional Contributor

Re: how can we pass an object defined in one unit to another

Maybe you can go with something like:

 

Unit 1:

function returnObj(){

return Sys.Process('app').Caption('text');

}

 

And inside the unit 2 you can call the function.

 

 

Occasional Contributor

Re: how can we pass an object defined in one unit to another

Hi Alex

My code is as below

 

//OpenFB unit

function openurl()
{
  var browser;
  var page;

  Browsers.Item(btChrome).Run("https://www.facebook.com/");
  browser = Sys.browser();
  page = browser.Page("*facebook.com*");
 // page.NativeWebObject.Find("name","websubmit","BUTTON").Click();  this code is working when executing from unit OpenFB
}

 

//Unit B

 

//USEUNIT OpenFB
function mandatory_check()
{
    OpenFB.openurl();   //  --this line is working it is calling the above function
   OpenFB.page.NativeWebObject.Find("name","websubmit","BUTTON").Click();  // giving error says "Cannot read property 'NativeWebObject' of undefined  "
 }

Highlighted
Occasional Contributor

Re: how can we pass an object defined in one unit to another

Thanks

I am able to call the function of another unit but not able to use  the variable/object declared in another unit.

Community Hero

Re: how can we pass an object defined in one unit to another

Hi,

 

You need to return required value from the function like it was advised by @Vinicius.

Like this:

//OpenFB unit

function openurl()
{
  var browser;
  var page;

  Browsers.Item(btChrome).Run("https://www.facebook.com/");
  browser = Sys.browser();
  page = browser.Page("*facebook.com*");
 // page.NativeWebObject.Find("name","websubmit","BUTTON").Click();  this code is working when executing from unit OpenFB

 

  return page; // <== added line
}

 

//Unit B

 

//USEUNIT OpenFB
function mandatory_check()
{

    var page;

 

    page = openurl();   // <== OpenFB. is not required because of //USEUNIT OpenFB directive
    page.NativeWebObject.Find("name", "websubmit", "BUTTON").Click();
 }

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================

View solution in original post

Community Leader

Re: how can we pass an object defined in one unit to another

Edit: Hey, wouldn't you know it.  I just spotted some syntax errors to fix

 

Hi @Preetha

 

Something you need to be aware of that often gets users coming from OO environments.  You can't pass objects by reference in quite the same way.  See https://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference

 

Your code seems to indicate you are trying to use the openurl() function from OpenFB unit as an object with page being one of the properties of the object.  This won't work, since "page" is not exposed if you are looking at it from an OO outlook.  This shouldn't be too much of a surprise.  This is a scripting language rather than an OO language after all.

 

There are two potential ways of working round this I can think of off the top of my head:

 

1. Return the "page" object as @AlexKaras suggests

2. Return the "page" as a property of the pseudo object:

 

 

//OpenFB unit
function FBPage()
{
//This bit makes it behave like an object:
if (!(this instanceof FBPage))
  {
    return new FPPage();
  }
Browsers.Item(btChrome).Run("https://www.facebook.com/");
var browser = Sys.browser();
this.page = browser.Page("*facebook.com");
//the same thing, but as a function. Usefull if you need to do more than jsut get the page
this.GetPage = function()
{
Log.Message("Getting the Page");
return this.page;
}
//More on this later
this.Fetch = funtion()
{
return this;
} }

//USEUNIT OpenFB
function mandatory_check()
{
FBPage().page.NativeWebObject.Find("name","websubmit","BUTTON").Click();
//TC IDE can't pick up the .page - If you want TC IDE to see the page property, you are looking at script extentions
//The same thing:
FBPage().GetPage().NativeWebObject.Find("name","websubmit","BUTTON").Click();

//Both these methods create a new "Instance" of the object though, meaning it will rerun and refind stuff
//which can take some time. This is where fetch comes in
var openFBObj = FBPage().Fetch();
openFBObj.page.NativeWebObject.Find("name","websubmit","BUTTON").Click();
openFBObj.GetPage().NativeWebObject.Find("name","websubmit","BUTTON").Click();
//Once again, don't depend on TC to pick up these "properties" and "methods"
}


 

I can't help but notice though that this function is extremely narrowly focussed.  You may want to consider making openurl a function with parameters and move it into a more generic unit e.g.

 

//Unit PageNavigation
function OpenPage(browser,url,pagename)
{
 if(!(this instanceof OpenPage))
{
return new OpenPage(browser,url,pagename);
}
 Browsers.Item(browser).Run(url);
  var browser = Sys.browser();
  var page = browser.Page(pagename);
this.GetPage = function()
{
return page;
}
this.Fetch=function()
{
return this;
}
this.Click = function()
{
try
{
page.NativeWebObject.Find("name","websubmit","BUTTON").Click();
return true;
}
catch(e)
{
Log.Error(e);
return false;
}
} }


//Unit B
//USEUNIT PageNavigation
function mandatory_check()
{
if(OpenPage(btChrome,"https://facebook.com/","*facebook.com").Click())
{
Log.Message("It worked !");
}
else
{
Log.Error("It Failed !");
}
}

 


-------------------------------------------------
Standard syntax disclaimers apply
Regards,
Community Manager

Re: how can we pass an object defined in one unit to another

Thanks for your suggestions, Vinicius, Alex, Rudolf!

 

@Preetha, did you find an answer to your question?

Please accept that reply as a solution.

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
New Here?
Join us and watch the welcome video:
Final Interview in the Series
API_testing_mistake#7
Top Kudoed Authors