load runner advanced scripting techniques

8
LoadRunner Advanced Scripting Techniques Making use of function calls to simplify Scripts by Nicholas P. M. Godfrey Introduction Let's suppose that you have ten scripts to write and each one starts with a complex login screen, for example. The usual approach to this would be to record your scripts one at a time and correlate and parametize each one independently. And if, at a later date, the recorded login functionality changes, each script would then have to be modified accordingly. There is however another approach: Record one script and program the login screen until it works. Then, build a function from this working code and call this function from within each script that needs to login to the application under test. This document shows how this can be achieved quickly and easily with a minimum of coding knowledge. Just follow the example below. Suppose you want to record a web searching script where each different type of user logs into your site and performs a different search depending on their particular business rules. So for each type of user you have a separate script but they all share the same initialization function (init()) where they login. Standard Vugen script The various components of your standard recorded script look like this: vuser_init.c (the common login screen) vuser_init() {

Upload: purandhar636539

Post on 12-Mar-2015

628 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Load Runner Advanced Scripting Techniques

LoadRunner Advanced Scripting TechniquesMaking use of function calls to simplify Scripts

by Nicholas P. M. Godfrey 

Introduction Let's suppose that you have ten scripts to write and each one starts with a complex login screen, for example. The usual approach to this would be to record your scripts one at a time and correlate and parametize each one independently. And if, at a later date, the recorded login functionality changes, each script would then have to be modified accordingly. There is however another approach: Record one script and program the login screen until it works. Then, build a function from this working code and call this function from within each script that needs to login to the application under test. This document shows how this can be achieved quickly and easily with a minimum of coding knowledge. Just follow the example below. Suppose you want to record a web searching script where each different type of user logs into your site and performs a different search depending on their particular business rules. So for each type of user you have a separate script but they all share the same initialization function (init()) where they login.  Standard Vugen script The various components of your standard recorded script look like this: vuser_init.c (the common login screen) vuser_init(){ 

web_add_cookie("FPS=dl; DOMAIN=www.webwob.com"); 

web_add_cookie("B=bjh9s8t2jc687&b=3&s=5n; DOMAIN=www.webwob.com"); 

web_add_cookie("Q=q1=AACAAAAAAAAAfA--&q2=RYtEeg--; DOMAIN=www.webwob.com"); 

web_add_cookie("SO=v=0.4&t=1166799707; DOMAIN=www.webwob.com"); 

web_url("www.webwob.com", "URL=http://www.webwob.com/", "Resource=0", "RecContentType=text/html",

Page 2: Load Runner Advanced Scripting Techniques

"Referer=", "Snapshot=t1.inf", "Mode=HTTP", LAST);

 web_concurrent_start(NULL);

 web_url("y3.gif",

"URL=http://us.i1.webwob.com/us.webwob.com/i/ww/beta/y3.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://www.webwob.com/", "Snapshot=t2.inf", LAST);

 return 0;

} Action.c (variable searching activities) Action(){ 

web_add_cookie("D=_ylc=X3IDMwRmcANyL3N4&_ylg=X3oDMTQwczUxMzEy; DOMAIN=www.webwob.com"); 

lr_think_time(15); 

web_url("p.gif", "URL=http://www.webwob.com/p.gif?

t=1166801046&_ylp=A0LEfYGW.ItFtV8AN131cSkA&, "Resource=1", "RecContentType=image/gif", "Referer=http://www.webwob.com/", "Snapshot=t31.inf", LAST);

 web_add_cookie("B=bjh9s8t2jc687&b=3&s=5n; DOMAIN=search.webwob.com");

 web_add_cookie("Q=q1=AACAAAAAAAAAfA--&q2=RYtEeg--;

DOMAIN=search.webwob.com"); 

web_add_cookie("SO=v=0.4&t=1166799707; DOMAIN=search.webwob.com"); 

web_url("search",

Page 3: Load Runner Advanced Scripting Techniques

"URL=http://www.webwob.com/r/sx/*-http://search.webwob.com/search?p=Nikon+F-301+1985&fr=yfp-t-501&toggle=1&cop=mss&ei=UTF-8&vc=&fp_ip=UK",

"Resource=0", "RecContentType=text/html", "Referer=http://www.webwob.com/", "Snapshot=t32.inf", "Mode=HTTP", LAST);

return 0;} globals.h (This is used next. See below.) #ifndef _GLOBALS_H #define _GLOBALS_H //--------------------------------------------------------------------// Include Files#include "lrun.h"#include "web_api.h"#include "lrw_custom_body.h" //--------------------------------------------------------------------// Global Variables #endif // _GLOBALS_H  Modified Vugen script, using a function call Make sure that the login section fully works and then modify the above structure. This is done by introducing a new file and referencing it in the globals.h file, after which it will be possible to call a login() function from within the vuser_init.c section. First, cut and paste the code from with the vuser_init.c file into a new text file, called mylogin.h, creating a login function at the same time: mylogin.h login(){ 

web_add_cookie("FPS=dl; DOMAIN=www.webwob.com"); 

web_add_cookie("B=bjh9s8t2jc687&b=3&s=5n; DOMAIN=www.webwob.com");

Page 4: Load Runner Advanced Scripting Techniques

 web_add_cookie("Q=q1=AACAAAAAAAAAfA--&q2=RYtEeg--;

DOMAIN=www.webwob.com"); 

web_add_cookie("SO=v=0.4&t=1166799707; DOMAIN=www.webwob.com"); 

web_url("www.webwob.com", "URL=http://www.webwob.com/", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t1.inf", "Mode=HTTP", LAST);

 web_concurrent_start(NULL);

 web_url("y3.gif",

"URL=http://us.i1.webwob.com/us.webwob.com/i/ww/beta/y3.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://www.webwob.com/", "Snapshot=t2.inf", LAST);

 return 0;

} Save this file somewhere in your file structure. Let's say it is under c:\LRScripts. Next, edit the vuser_init.c file so that it looks like this: vuser_init.c vuser_init(){

login(); 

return 0;} And edit the globals.h file thus: globals.h (Note the additional line.) #ifndef _GLOBALS_H

Page 5: Load Runner Advanced Scripting Techniques

#define _GLOBALS_H //--------------------------------------------------------------------// Include Files#include "lrun.h"#include "web_api.h"#include "lrw_custom_body.h"#include "c:\\LRScripts\mylogin.h" //--------------------------------------------------------------------// Global Variables #endif // _GLOBALS_H Using the new structured Vugen script If you compile and run your script, everything should work as before but you now have a separate login function, contained within mylogin.h which is called from within vuser_init(). For the next user type, record a new script and save it. Then delete the contents of the new vuser_init() and replace it with this: vuser_init(){

login(); 

return 0;} The new script will now login correctly with no extra correlation or parameterization work required. Note that any parameter lists used within the login() function will need incorporating into the new script but there is a better way to handle such data. Read below. Passing data into your new function Typically you will want your users to login with their own unique username and password. This is best accomplished by passing data directly into your new function through the function call. Data files will still be needed by the calling script but the new login function will not refer to these directly, which is better from a maintenance perspective. To do this, define your function as follows: login(char* username, char* password), so mylogin.h looks like this:  mylogin.h

Page 6: Load Runner Advanced Scripting Techniques

 login(char* username, char* password){ 

web_add_cookie("FPS=dl; DOMAIN=www.webwob.com"); 

web_add_cookie("B=bjh9s8t2jc687&b=3&s=5n; DOMAIN=www.webwob.com"); 

web_add_cookie("Q=q1=AACAAAAAAAAAfA--&q2=RYtEeg--; DOMAIN=www.webwob.com"); 

web_add_cookie("SO=v=0.4&t=1166799707; DOMAIN=www.webwob.com"); 

web_url("www.webwob.com", "URL=http://www.webwob.com/", "Resource=0", "RecContentType=text/html", "Referer=", "Snapshot=t1.inf", "Mode=HTTP", LAST);

 web_concurrent_start(NULL);

 web_url("y3.gif",

"URL=http://us.i1.webwob.com/us.webwob.com/i/ww/beta/y3.gif", "Resource=1", "RecContentType=image/gif", "Referer=http://www.webwob.com/", "Snapshot=t2.inf", LAST);

 return 0;

} Inside mylogin() you can refer to username and password just like any other variable and vuser_init() passes the data into the function like this: login("user_id1", "Password1");