1

我有 LR 脚本,它在 Action 部分包含近 20 个类似的交易。

脚本片段在这里:

        lr_start_transaction("select random item in Methodology");

    // 1st transaction
        web_url("Load Testing Terminology", 
            "URL=http://***/Load_Testing_Terminology", 
            "Resource=0", 
            "RecContentType=text/html", 
            "Referer=http://***/Methodology", 
            "Snapshot=t21.inf", 
            "Mode=HTML", 
            LAST);

    // 2nd transaction
        web_url("Concepts", 
            "URL=http://***/Concepts", 
            "Resource=0", 
            "RecContentType=text/html", 
            "Referer=http://***/Methodology", 
            "Snapshot=t22.inf", 
            "Mode=HTML", 
            LAST);

        ...
// (and so on till last transaction in transactions list)

        lr_end_transaction("select random item in Methodology",LR_AUTO);

对于每个执行的脚本,我只需要从此列表中选择 1 个事务。

你能告诉我,我怎样才能实现这个随机选择?

4

2 回答 2

2

考虑到在下一个版本中链接的数量可能会更改为 30 或 40 或 50 或 6 个,你为什么要硬编码呢?你的脚本不应该足够灵活来收集页面上的 URL 列表,然后为你随机选择一个吗?

考虑使用标准关联方法将 URL 列表收集到一个数组中,然后选择其中一个数组元素用于继续下一页。

于 2013-09-09T13:45:06.920 回答
2

现在我需要从我的页面 Methodology 上的有用链接中获取 1 个随机链接。条件是随机交易的名称应该从该交易的相对 URL 中获取,如下所示:

"random_link_relative_URL" =  "/index.php/What_are_the_Goals_of_Performance_Testing?"

本案例的代码如下:

Action()
    {
lr_start_transaction("open index page");

web_url("***", 
        "URL=http://{HOST}/", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t19.inf", 
        "Mode=HTML", 
        LAST);
lr_end_transaction("open index page",LR_AUTO);

/* in link_name we are collecting elements of links array on Methodology page*/
/* in my case usefull links have the structure like this: 
    <p><a href="/index.php/Load_Testing_Terminology" title="Load Testing Terminology">Load Testing Terminology</a> 
and useless links have different HTML-code structure,
therefore we need to use LB and RB to limit useful HTML snippets like this:
/index.php/What_are_the_Goals_of_Performance_Testing */

web_reg_save_param("link_relative_URL", "LB/ic=<p><a href=\"", "RB=\" title", "ORD=all", LAST );

/* the array containing 21 items of link_relative_URL is created when the transaction Methodology is performed */
lr_start_transaction("Methodology");

web_url("Methodology", 
        "URL=http://{HOST}/index.php/Methodology", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://***/index.php", 
        "Snapshot=t20.inf", 
        "Mode=HTML",
        LAST);

lr_end_transaction("Methodology",LR_AUTO);


/* using `lr_paramarr_random` function we can choise 1 random link from array of link_relative_URL and save this random link from array to parameter colled random_link_relative_URL */
lr_save_string(lr_paramarr_random("link_relative_URL"),"random_link_relative_URL");

/* let's verify that {random_link_relative_URL} was saved */
`lr_output_message("random link is %s",lr_eval_string("{random_link_relative_URL}"));`

/* let's save the relative URL `random_link_relative_URL` to variable colled  `random_link_name` */
sprintf(random_link_name, lr_eval_string("{random_link_relative_URL}"));

/* start transaction random_link_name */
 lr_start_transaction(random_link_name);

/* using this sintax we will see the name of random link in requst's status */
        web_url(lr_eval_string("{random_link_relative_URL}"), 
        "URL=http://{HOST}{random_link_relative_URL}", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://{HOST}/index.php/Methodology", 
        "Mode=HTML", 
        LAST);

/* close transaction */
lr_end_transaction(random_link_name, LR_AUTO);

    return 0;
}

在第一步,我们将在 Methodology 页面上看到有用的链接数组,在第二个 - 选择的随机链接上,在第三个 - 具有此随机链接的名称和 URL 的交易。


第二种方式。

Action()
{
    lr_start_transaction("open index page");

    web_url("***", 
        "URL=http://{HOST}/", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=", 
        "Snapshot=t19.inf", 
        "Mode=HTML", 
        LAST);
    lr_end_transaction("open index page",LR_AUTO);


/* saves pages names as link_name on Methodology web page using limitations in HTML tag */
web_reg_save_param("link_name", "LB/ic=<a href=\"/index.php/", "RB=\" title", "ORD=ALL", LAST );

lr_start_transaction("Methodology");

    web_url("Methodology", 
        "URL=http://{HOST}/index.php/Methodology", 
        "Resource=0", 
        "RecContentType=text/html", 
        "Referer=http://***/index.php", 
        "Snapshot=t20.inf", 
        "Mode=HTML", 
        LAST);

lr_end_transaction("Methodology",LR_AUTO);

/* select random link from links array on the page Methodology */
random_tr = lr_paramarr_random("link_name"); 

    return 0;
}

解决这个任务的方法可能是下一个:

在 Action 部分只写 1 个带有 2 个参数的请求,{name_of_random_link}并且{URL_of_random_link}

web_url("{name_of_random_link}", 
"URL={URL_of_random_link}", 
"Resource=0", 
"RecContentType=text/html", 
"Referer=http://***/Methodology", 
"Mode=HTML", 
LAST);

该列表包含 20 个相关参数的 20 个字符串,{name_of_random_link}{URL_of_random_link}放置在参数列表文件中。 {name_of_random_link}是从这个列表中随机选择的,{URL_of_random_link}被选为“与 URL_of_random_link 相同的行”。

于 2013-09-09T11:16:57.653 回答