LoadRunner C Programming _ 1

Source: Internet
Author: User

For more information, see. LoadRunner's script language is actually no different from C. Many of his internal mechanisms are implemented by C.

It is just a "Class C"

So I will analyze it from several aspects.

1: defining constant variables is the same as C

2: The parameters of the LR function are a little different from those of C. In LR, the variables of C are different from those of LR.
No C variable can be directly called by LR functions.
Lr_eval_string should be used.

3: Which loop statements are the same as C statements?


4: The definitions of some functions are different from those of C. Although the names are the same, the parameters are different.

5: the input and output are also somewhat different.


So the key point to be broken through is to clarify the direct relationship between parameters and variables. Familiar with many LR functions, others are knowledge of C language.


LR has its own management variables, also known as parameters. Such parameters can only be defined by Reg, lr_save _, or by file.


1. parameter values and values

Lr_save_string ("Hello World", "Param ");
Lr_eval_string ("{Param }");


2. Variables to parameters
Int X;
X = 10;
Lr_save_string (x, "Param ");
Lr_eval_string ("{Param }");


3. Variable read Parameters
Char X [100];
X = "{Param }";
Lr_save_string ("Hello World", "Param ");
Lr_eval_string (X );

LoadRunner does not provide a function for arithmetic operations on parameters. Therefore, the LR parameter must be:

1) An integer converted to C

2) use the C function to calculate the final return of a C string

3) Save the returned string as a parameter.


View plaincopy to clipboardprint?
// 1. Integer converted to C:

I = atoi (lr_eval_string ("{pnum_in }"));

// 2. Use the C function to calculate the final returned string of C:

Sprintf (cbuf, "% d", I + 1 );

// 3. Save the returned string as a parameter:

Lr_save_string (cbuf, "pnum_out ");
// 1. Integer converted to C:

I = atoi (lr_eval_string ("{pnum_in }"));

// 2. Use the C function to calculate the final returned string of C:

Sprintf (cbuf, "% d", I + 1 );

// 3. Save the returned string as a parameter:

Lr_save_string (cbuf, "pnum_out ");



Lr_eval_string ()

Function: returns the current value of a parameter in the script,

Return Value Type: Char

Generally, the parameter value is output when the script is debugged. The usage is as follows:
Lr_output_message ("The parameter1's value is % s", lr_eval_string ("{parameter1}"), where parameter1 is a previously defined parameter

Lr_log_message (lr_eval_string ("{parameter1 }"))


In LR, C variables and LR parameters are different.
No C variable can be directly called by LR functions.
Lr_eval_string should be used.
For example, {newparam} (parameterized variable in LR) can be referenced directly.
However, if:

Action ()
{
Char A [10];
Strcpy (a, "{newparam }");
Lr_message ();
Return 0;
}

This is incorrect.
Lr_message (a); an error is returned.
But you can write it as lr_message (lr_eval_string (.
Because the value has been taken out.

***********/
Variable ()


Char * IP = lr_get_vuser_ip (); // obtain the IP address of the current user and save it in the IP variable.
Char * gname = lr_get_host_name (); // obtain the machine name of the current user and save it in the gname variable.

If (IP)
// Runtime is a defined parameter, which can be called directly below
Lr_vuser_status_message ("IP Address: % s, parameter: % s", IP, lr_eval_string ("{runtime }"));
Else
Lr_vuser_status_message ("IP spoofing not started ...... ");


Lr_save_string (gname, "gn"); // put the variable IP in the "gn" parameter, which can be called directly by {GN} below.



Web_submit_data ("statusreporter ",
"Name = title", "value = {runtime}", Enditem,
"Name = content", "value = {GN}", Enditem,
Last );



Lr_output_message ("current IP Address: % s", ip );

Lr_think_time (2); // pause for two seconds for easy observation.

Return0;
}

Initially, I want to save the number obtained by web_reg_save_param () in the array and lr_eval_string () to display each element in the array. The Code is as follows:
Web_reg_save_param ("test", "lB =", "RB =", "ord = All", last );
Web_url ();
Count = lr_eval_string ("{test_count }");
For (I = 1; I <= count, I ++)
Sprintf (STR, lr_eval_string ("{test _ % d}"), I );
However, the data obtained every time is always
STR = test_1 test_2... are not elements in the array.
In fact, the difference between the internal and external parameters in LR is not clear.
The array obtained in web_reg_save_param () is an internal function of LR. In its internal function, variables are used, that is, (test _ % d, I ), the element value cannot be obtained directly. This must be passed through variables (external variables.
The above code is changed:
Web_reg_save_param ("test", "lB =", "RB =", "ord = All", last );
Web_url ();
Count = atoi (lr_eval_string ("{test_count }"));
For (I = 1; I <= count, I ++)
{
Sprinf (TMP, "{test _ % d}", I );
Sprintf (STR, lr_eval_string (TMP ));
}
This can be achieved.


Lr_save_string


The lr_save_string function assigns the specified null-terminated string to a parameter. This function is useful in correlating queries. To determine the value of the parameter, use the lr_eval_string function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.