Conditional statements (MonkeyTalk)
IF statement (Javascript form):
For using
conditions like the IF we need to modify the javascript form of the code and
add new code to the existing javascript. While this makes the script more
powerful, one cannot return to the old regular MonkeyTalk script form. In order
to export the script click on the javascript tab at the bottom of the editor
window in MonkeyTalk IDE and then click on Export button at the top.
We will
illustrate the use of If statement by using two scripts, driver and driven. The
driver script will pass the username and password to the driven script twice
(two iterations). In one of the iteration the password is invalid and therefore
the page will throw error. The driven script verifies if that error exists and
then executes certain part of the code else it executes some other part of the
code.
Driver Script
Script ae.js
Run username1 p1
Script ae.js
Run username1 pass2
Driven Script
load("libs/<monkeytalk_project_name>.js");
<monkeytalk_project_name>.<driven_script_name>.prototype.run
= function(user, pass) {
this.app.input("username").enterText(user);
this.app.input("password").enterText(pass);
this.app.button("LOGIN").tap();
var a =
this.app.label("login_err").get("a");
/*
* The get() function
returns a string which
* is displayed on the
label having id as "login_err"
* This element is always
displayed on the screen.
* When the login page
throws no error the string is void
* i.e. it is equal to
""
* This returned string is
stored in variable 'a'
*/
if(a ==
"Sorry! Password must be 4 or more characters.")
/*
* The if statement, tests if the string
captured in a
* is equal to the same error
message.
*
* If value returned is TRUE then
* new set of username and
passwords are entered followed
* by user clicking the
"forms" tab and then the "login" tab
*
* If value returned is FALSE then
the user is logged in
* and all we need to do is logut
the user. Followed by
* user clicking the
"hierarchy" tab and then the "login" tab
*/
{
this.app.input("username").tap();
this.app.input("username").enterText("new
username");
this.app.input("password").enterText("new
password");
this.app.button("LOGIN").tap();
this.app.button("LOGOUT").tap({thinktime:"3000"});
this.app.tabBar().select("forms",
{thinktime:"3000"});
this.app.tabBar().select("login",
{thinktime:"3000"});
}
else
{
this.app.button("LOGOUT").tap({thinktime:"3000"});
this.app.tabBar().select("hierarchy",
{thinktime:"3000"});
this.app.tabBar().select("login",
{thinktime:"3000"});
}
};
Variable declaration:-
Variables
can be initialized at the time of declaration:
Vars *
Define usr=john pw=smith
Input
username EnterText ${usr}
Input
password EnterText ${pw}
The above
script will enter john as username and smith as password.
However, if
the above script is being driven by a driver script (as mentioned in example
2), which passes arguments then the values provided as arguments in the driver
script trump the ones defined in the above script. Example:
Script
driven.mt Run albert einstein
Also,
mentioned in example 2 is that if a driver script only passes one argument then
it is replaces the values stored in the first declared variable and the second
one remains as it is.
Script
driven.mt Run albert.
Related Articles:-
- Eclipse configuration with Android SDK (Click here…)
- Monkey Talk: Installation of Agent (Click here…)
- Writing script in Monkey Talk (Click here...)
- Simple Login Script (Click here…)
- Data driven Script (Click here…)
- Passing variable values through driver script (Click here...)
- Steps to install Android Apps to SD Card (Click here…)
- Steps to install Android App in Emulator (Click here…)
Comments
Post a Comment