Tuesday, 7 August 2012

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
<monkeytalk_project_name>.<driven_script_name>.prototype.run = function(user, pass) {
                    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").enterText("new username");
                            this.app.input("password").enterText("new password");
                                                this.app.tabBar().select("forms", {thinktime:"3000"});
                                                this.app.tabBar().select("login", {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


  1. 1.How can I get the log file as the output....how can i show the pass \ fail case in the monkey talk

    2.can I run the script in the batch mode.

  2. Hi,
    When I try to run js file in MT i received this error "16:15:59.975: Script Driver.js Run
    16:15:59.976: Completed Script Playback - ERROR sun.org.mozilla.javascript.internal.WrappedException: Wrapped java.lang.RuntimeException: script error: /Applications/monkeytalk/MonkeyTalkIDE/MonkeyTalkIDE.app/Contents/MacOS/workspace/CRB/Driver.js
    javax.script.ScriptException: sun.org.mozilla.javascript.internal.WrappedException: Wrapped java.lang.RuntimeException: script error: /Applications/monkeytalk/MonkeyTalkIDE/MonkeyTalkIDE.app/Contents/MacOS/workspace/CRB/libs/CRB.js
    javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: missing name after . operator (libs/CRB.js#250) in libs/CRB.js at line number 250 (MonkeyTalkJS.js#1) in MonkeyTalkJS.js at line number 1 (MonkeyTalkJS.js#1) in MonkeyTalkJS.js at line number 1"