Conditional statements for MonkeyTalk



IF Condition (In JavaScript form):
For using statements like the IF we required to update the code for JavaScript form and add new code in the available JavaScript. This build the script more smart, user cannot come back to the old usual MonkeyTalk script form. If you want to export the script click on the JavaScript tab from the bottom of the editor window from MonkeyTalk IDE and then just click on Export button at the top of this window.
We will exemplify the utilize of If condition by using these two scripts, driven and driver. The driver script will pass the user_name and pasword to the driven script two times (2 iterations). In first iteration the password if invalid and then the page will throw exception. The driven script confirms if that error available and then it will execute only the certain part of the code part else it will executes also the another part of the code.
Driver Script
Script ae.js Run user_name1 pass1
Script ae.js Run user_name1 pass2

Driven Script
load("libs/<monkeytalk_mproject_name >.js");
<monkeytalk_mproject_name>.<driven_mscript_name>.prototype.run = function(user, pass) {
                    this.app.input("user_name").enterText(user);
                    this.app.input("pass_word").enterText(pass);
                    this.app.button("LOGIN").tap();
                    var p = this.app.label("login_err").get("p");
                    /*
                     > The get() function always returns a string which
                         Displays on the applied label having id as "login_er"
                    > This element will always display on the screen.
                    > If the login page will not throws an error then string will be void
                    > i.e. it will equal to ""
                    > This returned string is stored in variable 'p'
                     */
                                if(p == "Sorry! Password must be of 6 characters or more than 6       
                                                character")
                                                /*
         The if statement, tests that required string full fil his
          condition.
                                                *  If returned value is TRUE then
                                                 * New set of user_name and pass_words are entered
         On user clicking the "forms" tab then login from the
         "login" tab
                                                 * If value returned is FALSE then the user is logged in
                                                 * And all we need to do if logout the client. Followed by
                                                 *  Client clicking the "hierarchy" tab and then the "login" tab
                                                 */ 
                                {
                                                this.app.input("user_name").tap();
                            this.app.input("user_name").enterText("new user_name");
                            this.app.input("password").enterText("new password");
                            this.app.button("LOGIN").tap();
                            this.app.button("LOGOUT").tap({thinktime:"4000"});
                                                this.app.tabBar().select("forms", {thinktime:"4000"});
                                                this.app.tabBar().select("login", {thinktime:"4000"});
                        }
                        else
                        {
                                this.app.button("LOGOUT").tap({thinktime:"4000"});
                                                this.app.tabBar().select("hierarchy", {thinktime:"4000"});
                                                this.app.tabBar().select("login", {thinktime:"4000"});
                        }
};

Declaration of Variables:-
Variables can be assigned at the time of initialization:

Vars * Define user=Marry pwd=Elizabeth
Input username EnterText ${user}
Input password EnterText ${pwd}

The above mentioned script will set the Marry as username and Elizabeth as password.

Though, if the above mentioned script will be driven by a driver script (as explained in example 2), which passes the parameters then the values provided as parameters in the script of driver, trump the ones explained in the above script. Example:

Script driven.mt Run John Decorato


No comments:

Post a Comment

Popular Posts