Learn an Object

Top  Previous  Next

To illustrate learning an object, we return to the TwoDialogs sample.

First, let's learn the OK button using recording.  We have done this before in the TwoDialogs sample.

Steps:

(1) Run TwoDialogs sample AUT.  By default this will be located in C:\Program Files\SmarteSoft\SmarteStudio\Samples\TwoDialogs\TwoDialogs.exe

(2) Start SmarteStudio and create a new test and call it TwoDialogsRecording.

(3) Press the Record/Learn button in the toolbar of SmarteStudio.

(4) When the "Select an Application to Record" dialog is displayed, choose the TwoDialogs.exe application and in the library list, select only the top library on the list - "Auto."  Press the Select button.

(5) In the TwoDialogs AUT, use the mouse to press the OK button.  Dismiss the alert message box complaining about the empty name.

(6) Notice that two things will happen.  Firstly, the OK button will be surrounded with a red marker, indicating that the OK button has been learned.  Secondly, the action of clicking the OK button is recorded in the Recording Activity dialog.  That recording has a single entry.:

LearnNormalButton

(7) Press the Finish button (or press Ctrl+3) to end the recording.

(8) SmarteStudio will return to be the foreground application, and it will have selected the TwoDialogsRecording.js (or whatever name you gave the test when you created it).

(9) Notice that there is a single line or script that has been added to the script file:

SeS('OK').DoAction();

This line of script has two interesting parts.

The "SeS('OK') is the identity (not the locator or location) of the OK button.  This is the object that was learned during recording.

The "DoAction()" is the instruction to the running script to take the action associated with a button.  A normal button has only a single possible action - to be pressed.

The Record/Learn process has taken both steps for you, and joined them together.

 

Now, let's use (normal) object learning to learn the same OK button and to call a method for the object.

Steps:

(1) Run TwoDialogs sample AUT.  By default this will be located in C:\Program Files\SmarteSoft\SmarteStudio\Samples\TwoDialogs\TwoDialogs.exe

(2) Start SmarteStudio and create a new test and call it TwoDialogsLearn.

(3) Press the Record/Learn button in the toolbar.

(4) When the "Select an Application to Record" dialog is displayed, choose the TwoDialogs.exe application.  Leave the library selection in its default state - we will not be using it this time.  Press Select.  Wait for the Recording Activity dialog to appear in the lower-right corner of the screen.

(5) Hover the mouse over the OK button of the TwoDialogs AUT but do not press the button.

(6) With the mouse positioned over the OK button, press Ctrl+2 ( the "Learn" command).  You will see the OK button surrounded with a red highlight.  You will also see that the Recording Activity dialog has been updated with a Learn event.

RAdialogForLearnOK

(7) Press the Finish button or Ctrl+3 to end the recording session.  You will now see that SmarteStudio has "learned" about the OK button, and the Object Tree in the upper left-hand pane of the SmarteStudio has a new entry called "OK" (shown here expanded).  The list of items contained under the OK button entry in the Object Tree is the set of methods and properties available for the OK object.  Methods are listed with purple icons, read properties are listed with blue icons, and write properties are listed with blue and purple icons.  Notice that the DoAction property is listed and recall in the previous section when we recorded pressing the button, the DoAction method was chosen for the button-press action.

ObjectTreeWithOKExpanded

(8) While we are looking at this OK object, let's make a few observations about it.  These observations will be useful for your later dealings with SmarteStudio and will make the script more informational and relevant as you delve into SmarteStudio.  First, look down at the Properties box that appears under the Object Tree in the bottom left corner of the SmarteStudio screen.  The screenshot below has some of the tree nodes expanded.

PropertiesOfOKButton

First, notice that the OK button has four (4) "locators" defined.  When you have SmarteStudio "learn" an object. it must collect data about that object so that it can relocate it even if the application has moved on the screen, and even if the application is in a different state of execution.  In order to accomplish this, SmarteStudio looks for all useful ways to uniquely identify the object.  As bad, or perhaps worse, than not being able to find an object would be to find the wrong object on the AUT.  Every time SmarteStudio is required to locate this object, it will first try to use the first locator.  If it fails to positively and uniquely match with that locator, it will try the second, and so on.  SmarteStudio will not give up and declare failure until it has failed to identify with all available locators.

 

Second, notice the ID entry in the Object section of the pane.  This is the name of the object from SmarteStudio's perspective.  All SmarteStudio names are available through the SeS() function call.  Therefore, if we want to refer to the "OK" object, we will use SeS("OK") to refer to it.  Once we have correctly identified the object, all valid methods and properties can be accessed by using that object as the basis.

 

Thirdly, notice in the main editor window of the SmarteStudio, that no code has been added.  When you identified the OK button, all SmarteStudio did was add the new object to the Object Tree.  It did not write any code in the javascript file.

(9) In the automated (recorded) section above, you saw that when you pressed the OK button on the dialog, SmarteStudio recorded a function like this:

SeS("OK").DoAction();

This time, you will use the established name of the OK button object, but do something a little more interesting than its default action to demonstrate how to use SmarteStudio.

(10) Move the cursor into the editor part of the SmarteStudio and make sure you are editing the file called TwoDialogsLearn.js.  At the moment, this file still looks something like this:

//########## Script Steps ##############

 

function Test()

{

 

}

 

g_load_libraries=["Generic"];

Between the open and close brace, add the following command:

SeS("OK").DoClick();

Hit the Play button and watch what happens.

The click will register as a command to the object and it will perform the action on the object.

 

While we have the context of this situation, let's complicate it just a little more to illustrate the intricacy as well as the flexibility of SmarteStudio and SeS.

There is a method whose names looks interesting: DoLButtonDown().

If we were to invoke DoLButtonDown() on the "OK" object, we would expect this would be the same as DoClick().

However, go back to the AUT for a moment.  Using the mouse, press the left mouse button over the OK button but don't take your finger off the left mouse button.

What happens is that the button takes its pressed state in appearance, but the button is not clicked.

The reason for this is that the DoClick() (or DoAction()) events cause the mouse button top be clicked as well as released.

Therefore, we would need to have a pair of events:

SeS("OK").DoLButtonDown();

SeS("OK").DoLButtonUp();

in order to make the "click" happen.

Try this in the test script you have created by adding those two lines of code in place of the DoClick() line.

It doesn't work!

Let's play a little with this problem.

When you press the Play button, leave the mouse alone.  Just press the left mouse button on the SmarteStudio Play button and take your hand away from the mouse.

The script does not press the OK button in the TwoDialogs AUT.

Now, press the Play button on the SmarteStudio and quickly move the mouse to hover over the OK button in the TwoDialogs AUT.

Now it works!

 

What's going on here is that the DoLButtonDown() and DoLButtonUp() methods are pressing the mouse irrespective of where the mouse cursor is positioned.

The other functions, DoClick and DoAction are methods that are applied to the button and so they are applied to the button.

Before we can expect DoLButtonDown() and DoLButtonUp() methods to work, we have to first the mouse cursor to the button.

function Test()

{

SeS("OK").DoMouseMove(25, 15);

SeS("OK").DoLButtonDown();

SeS("OK").DoLButtonUp();

}

will accomplish that.

Notice that SmarteStudio will actually move the mouse to the coordinates (25, 15) within the OK button.  Also notice that if you move the mouse while the test is playing, you will make the test fail.

 

As a last experiment in this arena, try moving the mouse outside the boundaries of the OK button object before calling the DoLButtonDown() function.

function Test()

{

SeS("OK").DoMouseMove(250, 150);

SeS("OK").DoLButtonDown();

SeS("OK").DoLButtonUp();

}

Once again, the script will fail.