Ask a Question

Global variable trouble!

SOLVED
royd
Regular Contributor

Global variable trouble!

Hello everyone

 

I am having serious trouble with Global vaiebles! In the past, I did use Global variable successfully. Only difference this time is, I have 3 Global variable coming from UnitA to UnitB and 3 Variables coming from UnitB to UnitC (one new from UnitB). I have wasted much time last week, can not figure out the problem!

 

I am using TestComplete 12 with JScript. Here is my setup:

 

UnitA(with UnitB reference)                    UnitB(with UnitC reference)                     UnitC

 

and . . .

 

UnitA:

 

var fName
var securityC
var getURL


function atHomePatient(){

  Browsers.Item("iexplore", "", Browsers.pX86).Run("https://<url>");

. . . 

//select department
Aliases.browser.pageHIS.aspNet.sDept.ClickItem(patient.sDept);

//set admission date
Aliases.browser.pageHIS.aspNet.aDate.SetText(patient.aDate);

//set patient name
Aliases.browser.pageHIS.aspNet.pName.SetText(patient.pName); //i.e: John Smith
fName = patient.pName.split(" ")[0];
Log.Message("Split first name is: " + fName);

//set DOB
Aliases.browser.pageHIS.aspNet.pDOB.SetText(patient.pDOB);  

. . .

}

UnitB:

 

function patientSignOn(){

Log.Message("Patient's Name is: " + fName);
Log.Message("Security code is: " + securityC);
Log.Message("Signup url is: " + getURL);

. . .

}

Here is the structure of my project:

units.png

 

All three reported 'undefined' in UnitB!

 

I would really appreciate any help!

 

Thanks.

 

Dave

13 REPLIES 13
shankar_r
Community Hero

There are two ways,

 

1) Using //USEUNIT in your UnitB and UnitC .

2) Using your global variable prefixed with their unit name Unit.fName

 

If you are not into any of these 2 ways then you will get undefined


Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”

Extension Available

royd
Regular Contributor

Hi Shankar

 

Good to see you again.

 

JScriptdoes does not allow the use of circular references see here

 

Just tried your second suggestion and got the following error:

 

Microsoft JScript runtime error.

'AA_Create_Patient' is undefined.

 

L19.png

 

Any idea?

 

Thanks.

 

Dave

tristaanogre
Esteemed Contributor

@royd, correct, it does not allow circular references, but you still need to have a //USEUNIT statement at the top of your unit to tell it to include variables and methods from another script unit.  Without that, you'll get exactly the result you got.  If you have B referencing A and then A referencing B again, that's not allowed.  But so long as it's just a single line, shouldn't be a problem.

 

In other words, your UnitB should look like this:

 

//USEUNIT AA_Create_Patient;

function patientSignOn(){

Log.Message("Patient's Name is: " + AA_Create_Patient.fName);
Log.Message("Security code is: " + AA_Create_Patient.securityC);
Log.Message("Signup url is: " + AA_Create_Patient.getURL);

. . .

}

 


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
royd
Regular Contributor

Hi Robert, 

 

Thanks for your response. Just tried that, added 

//USEUNIT AA_Create_Patient

to  AB_Signup_Patient and got following error:

 

circular.png

 

 

UnitB code is:

//USEUNIT AC_3839_Login_English_Toggle_Spanish_Logout_Reload
//USEUNIT underscore
//USEUNIT AA_Create_Patient




function patientSignOn(){

  
  Log.Message("Patient's Name is: " + AA_Create_Patient.fName);
  Log.Message("Security code is: " + AA_Create_Patient.securityC);
  Log.Message("Signup url is: " + AA_Create_Patient.getURL);

...

}

 

Dave

tristaanogre
Esteemed Contributor

Right..., because you have AB_SignUp_Patient as a //USEUNIT in AA_Create_Patient.  That's a circular reference and is not allowed.  Remove the //USEUNIT from AA_Create_Patient and try again.  You cannot use unit B within Unit A.  Likewise, you cannot use UNIT C within Unit B.  There must be a one-way direction in the uses clause.  

 

In brief...

 

A -> No USEUNIT references to Unit B or C

B -> //USEUNIT UnitA and no USEUNIT Reference to Unit C

C -> //USEUNIT UnitB

 

 


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
royd
Regular Contributor

Hi Robert,

 

Sorry for the delay in replying (Stand up meeting).

 

Once I removed the //USEUNIT AB_Signup_Patient from UnitA, got an error, 'AB_Signup_Patient' is undefined.

 

ab.png

 

Dave

tristaanogre
Esteemed Contributor

That's to be expect... you are trying to use B unit code in unit A... which is not allowed because you're not allowed circular references...

What this comes down to, honestly, is that you're going to need to re-engineer some stuff so that you keep your code modularized in specific units so you don't end up with circular references.  Unfortunately, that's you're only recourse here... in order to call the code you want to call, you need USEUNIT clauses... but you can't do that because doing so puts you in the situation of having circular references... the only solution is to re-engineer.


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

like @tristaanogre said, sounds like a redesign is needed here. but a couple ideas:

1. you could change the project language to javascript and use require('file_name') to link the files together. circular references are allowed in javascript.

2. you could use a script extension if the code is stable--see here for a recent testcomplete academy on script extensions presented by @tristaanogre--it's a bit of setup but makes for very portable/accessible functions

You can create a Unit which will have all global variables.

You can refer that unit every other unit so you will avoid circular references.

 

This is just a workaround of your issue.


Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”

Extension Available

cancel
Showing results for 
Search instead for 
Did you mean: