Showing posts with label MonkeyTalk. Show all posts
Showing posts with label MonkeyTalk. Show all posts

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
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

Datadriven Script (MonkeyTalk)


This basically requires three files all placed in the root folder of your MonkeyTalk Project.

1.  MonkeyTalk code script. This is the one where you all the automation code.
2.  A driver script. This script is the one which we will run to execute the test. This script just calls the main MonkeyTalk code script and passes a csv (comma separated value) file with it. Think of this script as a main method in a java class file which is invoked first and others are invoked as the thread of main method progresses
3. CSV file. This file contains all the arguments that you want to pass for each variable in the driven script. The order of the

Main MonkeyTalk code script (or the driven script) in MonkeyTalk form:-

Vars * Define user pass
Input username Tap
Input username EnterText ${user}
Input password EnterText ${pass}
Button LOGIN Verify LOGIN %thinktime=1000
Button LOGIN Tap
Label * Verify "Welcome, ${user}!" %thinktime=3000
Button LOGOUT VerifyNot LOGIN
Button LOGOUT Tap %thinktime=5000

Driver Script in MonkeyTalk form:-

Script <name_of_driven_script>.mt RunWith <name_of_csv_file>.csv

CSV file:-

user pass
username1, password1
username2, password2
username3, password3

Notice that the first line (row) in the CSV file is the variable name and the contents of second row are the values in the variable.

Iterations: The number of rows in the csv file is the number of times the script is iterated. So in this case the script is run 3 times. In the first iteration the csv file passes the value username1 to the user variable and password1 to pass variable and so on.

Variable initiation: It does not matter whether the declared variable in the driven script is initialized or not. Even if the declared variable is initialized with some value that value will be wiped of that variable during the first iteration and the new value as mentioned in the script will be assigned to the variable. This is based on the assumption that the declared variable is mentioned in the script.

Do I need mention all the variables in the csv file even if I don’t want to change their value in each iteration? No. Mention only that variable that you want to iterate in each iteration.

Leaving a blank value for a var in csv: Leaving a value blank for a variable in csv file leaves the variable empty (technically, null). Even if it is the second iteration the variable does not contain the value provided to it in the first iteration. This means that the var is wiped off even when there are not values to be assigned to it the second iteration.

Note: Just make sure that you hit the play or run button when you are using browsing the driver script and not the driven script.


Javascript form of the Monkey Script:-

load("libs/<monkey_project_name>.js");
<monkey_project_name>.<driven_script_name>.prototype.run = function(user, pass) {
                this.app.input("username").tap();
                this.app.input("username").enterText(user);
                this.app.input("password").enterText(pass);
                this.app.button("LOGIN").verify("LOGIN", {thinktime:"1000"});
                this.app.button("LOGIN").tap();
                this.app.label().verify("Welcome, " + user + "!", {thinktime:"3000"});
                this.app.button("LOGOUT").verifyNot("LOGIN");
                this.app.button("LOGOUT").tap({thinktime:"5000"});
};

Simple Login (MonkeyTalk)


MonkeyTalk form:-

Input username EnterText test
Input password EnterText test
Button LOGIN tap %thinktime=3000
Button LOGOUT tap %thinktime=3000
Input username tap
Input username EnterText test1
Input password EnterText test1
Button LOGIN tap %thinktime=3000
Button LOGOUT tap %thinktime=3000
Input username tap
Input username EnterText test2
Input password EnterText test
Button LOGIN tap %thinktime=3000
Button LOGOUT tap %thinktime=3000

Javascript form:-

load("libs/<monkey_project_name>.js");
<monkey_project_name>. <script_name>.prototype.run = function() {
                this.app.input("username").enterText("test");
                this.app.input("password").enterText("test");
                this.app.button("LOGIN").tap({thinktime:"3000"});
                this.app.button("LOGOUT").tap({thinktime:"3000"});
                this.app.input("username").tap();
                this.app.input("username").enterText("test1");
                this.app.input("password").enterText("test1");
                this.app.button("LOGIN").tap({thinktime:"3000"});
                this.app.button("LOGOUT").tap({thinktime:"3000"});
                this.app.input("username").tap();
                this.app.input("username").enterText("test2");
                this.app.input("password").enterText("test");
                this.app.button("LOGIN").tap({thinktime:"3000"});
                this.app.button("LOGOUT").tap({thinktime:"3000"});
};

Tuesday, 24 July 2012

Steps to Install Agent in MonkeyTalk


Monkey Talk:
Monkey Talk can be downloading from the below address:


Pre-requisite for installing Monkey Talk Agent:

For installing the Monkey Talk Android Agent, first we need to convert Android project into AspectJ.
For doing so, we need to configure AJDT in Eclipse.

For Eclipse setup and configuration with Android SDK, Cleck here.. 

Steps to configure AJDT in Eclipse:
Eclipse> Help> Install New Software > Click on Add
In ‘Add Repository’ popup:
 Name: AJDT
Click Ok. It will configure the AJDT with Eclipse.

 

 Installing the Monkey Talk Android Agent:

Open your Android Project in Eclipse and follow these instructions.

1. Convert your Android project to AspectJ



       2MonkeyTalk-agent.jar ‘monkeytalk-agent-1.0.8.beta4c’ can be found in the "agents" folder in the Monkey Talk package you downloaded earlier. The exact name of the jar might vary depending on the version, but it should always start with "Monkey Talk-agent".
 3. Create a "libs" folder in your Android project, if you don't already have one.
 4. Copy the .jar file (monkeytalk-agent-1.0.8.beta4c) into the libs folder.
 5. Right click on MonkeyTalk-agent.jar > AspectJ Tools > Add to Aspectpath.
6. Update your AndroidManifest.xml to include the following two permissions:
a)      android.permission.INTERNET
b)      android.permission.GET_TASKS
 7. Update the project properties (right-click on the project > Properties > Java Build Path), select the ‘Order and Export’ tab, and check the checkbox next to the AspectJ Runtime Library to export it:


 Note: In case if checking only ‘AspectJ Runtime Library’ does not work then check all the uncheck checkboxes.


MonkeyTalk Script Guidelines:
MonkeyTalk works on Record-Play concept, and for automation it requires simple Java Script knowledge. For the guidelines Click here

Script examples:
1. Simple Login script Click here
2. Data driven script Click here
3. Passing variable script Click here
4. Conditional statement script Click here