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.
Hi,
Can you post here your code so that others can see exact implementation and be able to share their ideas?
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.
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 "
}
Thanks
I am able to call the function of another unit but not able to use the variable/object declared in another unit.
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();
}
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;
}
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 !");
}
}
Thanks for your suggestions, Vinicius, Alex, Rudolf!
@Preetha, did you find an answer to your question?
Please accept that reply as a solution.
User | Count |
---|---|
36 | |
14 | |
10 | |
8 | |
6 |
Subject | Author | Latest Post |
---|---|---|