Teach you step by step how to write LoadRunner script

Writing  LoadRunner  scripts requires familiarity with basic knowledge such as scripting languages, business scenarios, parameterization technology, assertions, and transactions.

During actual writing, reasonable configuration and adjustment can be made based on specific test requirements and actual conditions.

The basic steps

Create script

In the Controller module of LoadRunner, create a new test script, you can choose to record the script or write the script manually.

Add business scenario

Add business scenarios to the script, such as login, shopping cart, ordering, payment, etc., to simulate user behavior.

to parameterize

Use parameterization technology in scripts to replace some dynamic values ​​(such as username, password, order number, etc.) with parameters for easy modification at runtime.

Add assertion

Add assertions to the script and check the return results after each business scenario is executed to ensure the correctness of the program.

Add transaction

Add transactions to the script to measure the response time and throughput of each business scenario.

debug script

Use LoadRunner's Debug mode to debug the script and check whether there are logic errors or script syntax errors to ensure the correctness of the script.

Configure the number of Vusers

Configure the number of Vusers according to the number of users that need to be simulated, and set the independent running configuration of Vusers.

Run and analyze scripts

Use the Controller module of LoadRunner to start the test process, wait for the test to complete, and use

LoadRunner's Analysis module performs result analysis and report generation.

Scripting

Action()
{
    char *username, *password;

    lr_start_transaction("login");

    // 参数化用户名和密码
    username = lr_eval_string("{pUsername}");
    password = lr_eval_string("{pPassword}");

    // 打开主页
    web_url("homepage",
        "URL=http://www.example.com/",
        "Resource=0",
        "RecContentType=text/html",
        "Mode=HTML",
        LAST);

    // 单击登录链接
    web_link("login_link",
        "Text=Login",
        "Snapshot=t1.inf",
        LAST);

    // 提交凭证
    web_submit_form("login_form",
        "Snapshot=t2.inf",
        ITEMDATA,
        "User ID={pUsername}",
        "Password={pPassword}",
        "Submit=Login",
        LAST);

    // 添加日志语句和断言
    lr_output_message("Logged in with username='%s' and password='%s'", username, password);
    lr_end_transaction("login", LR_AUTO);

    // 处理错误
    if (strstr(lr_eval_string("{ResponseURL}"), "error")) {
        lr_fail_trans_with_error("Login failed: incorrect username or password");
    }

    return 0;
}

You need to use virtual users to simulate multiple users logging in at the same time.

Use parameterization to simulate different username and password combinations to test different credentials in each iteration.

Ensure normal user flow and log response times for each operation by adding log statements, assertions, and error handling.

Summarize

The script uses the {pUsername} and {pPassword} parameters to simulate different credentials and uses the lr_eval_string() function to obtain these values ​​from the parameter file. On each iteration, these parameters are automatically updated to allow testing of different credentials.

The script also uses the lr_output_message() function to add logging statements to record the username and password for each login, and the lr_fail_trans_with_error() function to handle error conditions, such as failed logins.

Finally, the script also includes the lr_start_transaction() and lr_end_transaction() functions at the beginning and end of the transaction to record the response time of each transaction.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you! 

Guess you like

Origin blog.csdn.net/2301_78276982/article/details/133080537