cancel
Showing results for 
Search instead for 
Did you mean: 

Placing NameMapping data into vars

SOLVED
New Contributor

Placing NameMapping data into vars

Hey smartBear community!

 

I was wondering if it's possible to place a specific object's Alias inside a variable once and re-use that variable throughout several scripts.

 

For example, if I'm referencing the same object in three different scripts which run back to back, would it be possible to place it in a variable before running that "set" of scripts and simply do something like the following:

 

Place this somewhere initially,

header = Aliases...---------------------...label.

 

Then reference that variable in the scripts without redeclaring that same variable again in each script,

Script1: header.Click();

Script2: header.HoverMouse();

Script3 header.VisibleOnScreen();

 

In other words, I'm trying to create a bank of variables which I can freely choose upon within several scripts without having to type in the "Aliases..........." part over and over again, being that it becomes repetitive.

 

Thanks for the help!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Leader

Re: Placing NameMapping data into vars

Hi @automTest 

 

I have used a similar approach previously and can say that I won't recommend it.  For example, I was storing a combobox in a variable because I was applying lots of operations on the same object.  Inevitably though, somewhere through the test run, the handle to the object would be destroyed and you would get an error about not being able to find the object despite it obviously being on the page.  You don't even need a page load for the mapping to change in some cases.  This means that each time you want to work with the object - before Click(), HoverMouse() and VisibleOnScreen() - in your example, you first have to check that it exists AND remap it if it doesn't.  If you keep it local to your script,  you may still need to do the same, but you will find you don't need to refresh the mapping info as often, which means less of a performance hit, which will slow down your tests.  Taking longer to type your code is better than taking longer to run your scripts.  You will make up the difference after a few test runs.

 

What I could suggest to make the typing easier.  You can assign the aliases object to a variable inside your script.  You don't need to store it in a global variable, but you don't need to type as much either:

 

var myLabel = Aliases.MyApp.Panel1.Panel0.Panel2.Table.Cell1.Label;
myLabel.Click();
myLabel.HoverMouse();
myLabel.VisibleOnScreen();

You only had to use that long Aliases... syntax once in the entire script.  Slightly more typing than a global variable, lots less failed tests because of object recognition issues totally unrelated to what you are actually trying to test


-------------------------------------------------
Standard syntax disclaimers apply
Regards,
8 REPLIES 8
Super Contributor

Re: Placing NameMapping data into vars

The issue is that of scope. I suppose you could make them project variables, and populate them at test start or something.

Community Hero

Re: Placing NameMapping data into vars

NameMapping references an object. So, if you create a variable that stores an object, you can assign an object to that variable.  HOWEVER....  the state of the object at the time of storage is important.  You can assign the object but if it is not present in the application at the time you assign it, then what you'll get is a stub object or null object.

The Aliases reference triggers an internal engine of TestComplete that searches for and identifies the object.  So, you can't quite just assign the object.  Each time you reference an Alias object, you need to make sure that it exists.  And assigning it once when it could subsequently be destroyed and recreated will lose the "handle" on the object.

Now... I'm reading between the lines a bit.  It sounds like your Aliases..... is a very long string of objects.  It COULD be that there is some means by which that string can be shortened.

For example, let's say you have an object that looks like this.

Aliases.MyApp.Panel1.Panel0.Panel2.Table.Cell1.Label.

 

That may reflect EXACTLY the hierarchy of the object in your application.  In fact, you my see, in the Mapped Object section

 

NameMapping.Sys.MyApp.Panel1.Panel0.Panel2.Table.Cell1.Label.

 

Well, the "beauty" of Aliases is that you can exclude unnecessary members of the tree.  The NameMapping.Sys doesn't change but, by right clicking on an Aliased object and selecting "Exclude", you can shorten your string to only the desired segments.

Aliases.MyApp.Panel1.Label COULD be all you need.  To do this, all I'd do is exclude everything between Panel1 and Label... then you wouldn't have as much to type.


Robert Martin
[Community Expert Group]
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
Extensions available
Community Hero

Re: Placing NameMapping data into vars

EDIT: This is not a recommended method

 

If you really don't want to type Aliases (which is short name of the object) then there is one possibility.

You can create a separate unit where you can declare all the objects globally like below then refer that in all other script units.

 

Objs unit

var txtUserName = Aliases.MainPage.LoginForm.txtUserName;
var txtPassword = Aliases.MainPage.LoginForm.txtPassword;

In other units just use like below and make you did USEUNIT for Objs unit

//USEUNIT Objs
function login(){
      Objs.txtUserName.Keys("Username");
      Objs.txtPassword.Keys("password");
}

You don't need to put Objs. but for the safer side, it is good to have.


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

Community Hero

Re: Placing NameMapping data into vars


@tristaanogre wrote:

NameMapping references an object. So, if you create a variable that stores an object, you can assign an object to that variable.  HOWEVER....  the state of the object at the time of storage is important.  You can assign the object but if it is not present in the application at the time you assign it, then what you'll get is a stub object or null object.

This sounds interesting, I tried to have my aliases as a global variable and used that variable in one of my function. I believe global will get set at the start of execution.

At the start, my object was not available after some actions object is getting displayed and I'm able to do the action, where I used the global variable in a function and everything works fine.  I'm curious about your reply Smiley Happy


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

Community Hero

Re: Placing NameMapping data into vars

@shankar_r :

 

Hi,

 

Robert is correct with his explanation.

The only missed detail is that objects referenced via Aliases use late binding and are evaluated when the target object is first interacted with.

This fact makes it possible to declare global variable that references some Alias object and use this variable later inside some function. But as soon as the reference to the Alias is resolved, the obtained object will be cached. So if the target object is recreated (for example, as a result of the page reload), then its cached reference will be invalidated (obj.Exists will become False) and it will be required to refersh it with the help of the .RefereshMappingInfo() method. (See its documentation for more details.)

This is the reason why personally I don't consider the idea of storing references to aliased objects in global variables to be a really good one. (Not mentioning the fact that global variable is risky in the meaning that its value can be unexpectedly altered at any time.)

 

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/
================================
Community Hero

Re: Placing NameMapping data into vars

Sounds good to me!


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

Community Manager

Re: Placing NameMapping data into vars

Guys, thanks for your great suggestions!

@automTest, do they help you resolve the issue? Please mark the best answer as a solution or post here the solution you decided to use. Thanks.

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

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Highlighted
Community Leader

Re: Placing NameMapping data into vars

Hi @automTest 

 

I have used a similar approach previously and can say that I won't recommend it.  For example, I was storing a combobox in a variable because I was applying lots of operations on the same object.  Inevitably though, somewhere through the test run, the handle to the object would be destroyed and you would get an error about not being able to find the object despite it obviously being on the page.  You don't even need a page load for the mapping to change in some cases.  This means that each time you want to work with the object - before Click(), HoverMouse() and VisibleOnScreen() - in your example, you first have to check that it exists AND remap it if it doesn't.  If you keep it local to your script,  you may still need to do the same, but you will find you don't need to refresh the mapping info as often, which means less of a performance hit, which will slow down your tests.  Taking longer to type your code is better than taking longer to run your scripts.  You will make up the difference after a few test runs.

 

What I could suggest to make the typing easier.  You can assign the aliases object to a variable inside your script.  You don't need to store it in a global variable, but you don't need to type as much either:

 

var myLabel = Aliases.MyApp.Panel1.Panel0.Panel2.Table.Cell1.Label;
myLabel.Click();
myLabel.HoverMouse();
myLabel.VisibleOnScreen();

You only had to use that long Aliases... syntax once in the entire script.  Slightly more typing than a global variable, lots less failed tests because of object recognition issues totally unrelated to what you are actually trying to test


-------------------------------------------------
Standard syntax disclaimers apply
Regards,
New Here?
Join us and watch the welcome video: