从Loadrunner中的多个下拉列表中捕获响应值

时间:2018-12-10 06:50:52

标签: regex performance-testing loadrunner

我必须使用loadrunner分别捕获<select name="fromPort"><select name="toPort">中的选项值以进行关联。

我将正则表达式写为

<select name="fromPort" class="form-inline">(.+?)</select>

从头开始捕获,但表明没有匹配项。

<form action="reserve.php" method="post">
        <select name="fromPort" class="form-inline">
            <option value="Paris">Paris</option>
            <option value="Philadelphia">Philadelphia</option>
            <option value="Boston">Boston</option>
            <option value="Portland">Portland</option>
            <option value="San Diego">San Diego</option>
            <option value="Mexico City">Mexico City</option>
            <option value="São Paolo">São Paolo</option>
        </select>
        <p>
        <h2>Choose your destination city:</h2>
        <select name="toPort" class="form-inline">
            <option value="Buenos Aires">Buenos Aires</option>
            <option value="Rome">Rome</option>
            <option value="London">London</option>
            <option value="Berlin">Berlin</option>
            <option value="New York">New York</option>
            <option value="Dublin">Dublin</option>
            <option value="Cairo">Cairo</option>
        </select>
        <p></p>
        <div class="container">
            <input type="submit" class="btn btn-primary" value="Find Flights"/>
        </div>
    </form>

当我尝试使用notepad ++时,有一个复选框可以选择何时捕获多行响应。当我检查它时,相同的正则表达式正在工作。enter code here

3 个答案:

答案 0 :(得分:0)

您可以尝试

好吧,我要做的是

  • 使用此方法从选择匹配的选择标签中提取所有选项

    (?:<select name="(fromPort|toPort)" class="form-inline">\s+)([\w\W]+?)(?=\s+<\/select>)

  • 然后将每个选项作为单个元素拆分为一个数组。

  • 比使用此正则表达式从每个选项中提取的值还要多。

    (?<=\s*?<option value=")(.*?)(?=">.*?<\/option>)

let str =`<form action="reserve.php" method="post">
        <select name="fromPort" class="form-inline">
            <option value="Paris">Paris</option>
            <option value="Philadelphia">Philadelphia</option>
            <option value="Boston">Boston</option>
            <option value="Portland">Portland</option>
            <option value="San Diego">San Diego</option>
            <option value="Mexico City">Mexico City</option>
            <option value="São Paolo">São Paolo</option>
        </select>
        <p>
        <h2>Choose your destination city:</h2>
        <select name="toPort" class="form-inline">
            <option value="Buenos Aires">Buenos Aires</option>
            <option value="Rome">Rome</option>
            <option value="London">London</option>
            <option value="Berlin">Berlin</option>
            <option value="New York">New York</option>
            <option value="Dublin">Dublin</option>
            <option value="Cairo">Cairo</option>
        </select>
        <p></p>
        <div class="container">
            <input type="submit" class="btn btn-primary" value="Find Flights"/>
        </div>
    </form>`
    
let selectAllOption = str.match(/(?<=<select name="(fromPort|toPort)" class="form-inline">\s+)[\w\W]+?(?=\s+<\/select>)/g);

let splitedOptions = selectAllOption.map(e=>e.split('\n'))
let finalValues = splitedOptions.map(e=>{
  return e.map(ele=>{
    return ele.match(/(?<=\s*?<option value=")(.*?)(?=">.*?<\/option>)/g)
  })
})
console.log(finalValues);

答案 1 :(得分:0)

考虑到您在房屋的另一侧涉及多个类似的标签,我的回答不太优雅。我建议备份,以\“ fromPort” class = \“ form-inline \” \ n“或\” toPort“ class = \” form-inline \“ \ n”的LB条件捕获到结束标签“ \ n”

您将得到两个包含选项值的字符串变量,例如“ Correlated_from”和“ Correlated_to”。接下来是转换为数组结构。

大概有十二种方法可以将字符串分开,但我建议使用一个在换行符上断开的函数,将子字符串中的第一个“ \ t \ t”字符跳转,以节省字符串之间的值和下一个“ <”字符。如前所述,可能有十二种方法可以使用定义的模式分解此字符串,以找到您感兴趣的子字符串。

通过遵循标准的LoadRunner伪数组结构,这为使用标准LoadRunner函数分配伪数组中的变量-lr_paramarr_ *函数集提供了便利。

答案 2 :(得分:0)

我使用 LB="select id="xxxx"" 和 RB=" 提取选择

然后使用类似下面的东西来获取所有选项(如果我想随机化提交脚本的哪个,或者我选择已经选择的一个,如果我想提交默认选择是什么。每当我需要使用复杂的正则表达式 我通常将一个测试用例硬编码到一个临时测试操作中,以便在运行实际应用程序之前测试逻辑。

TestExtract()
{
    int matchCt, ord;
    
    char *arrayMemberValue; 
    
    char * bufferToSearch =
        "<option value=\"8183011816-1:USD\"";
    
    char * bufferToSearch2 =
        "  <option value=\"8171245851-1:USD\">xxxxxx5851 -  DDA 008171245851</option>"
        "  <option value=\"863006969-1:USD\">xxxxxx6969 - DDA 000863006969</option>"
        "   <option value=\"863006977-1:USD\">xxxxxx6977 - DDA 000863006977</option> ";
    
    
    char * bufferToSearch3 =
        "  <option value=\"125\">AIRLINE PILOT</option>  "
        "  <option value=\"008\" selected=\"selected\">SELF EMPLOYED</option>"
        "  <option value=\"232\">RECEPTIONIST</option>";
    
    
    char * bufferToSearch4 =
        "<select id=\"occupation\" name=\"occupation\" onchange='occupationChange();' class='form-control required' data-validate='dropdown'> "
        "    <option value=\"125\">AIRLINE PILOT</option>  "
        "    <option value=\"008\" selected=\"selected\">SELF EMPLOYED</option>"
        "    <option value=\"232\">RECEPTIONIST</option>"
        "</select>"
        "<select name=\"_address_state_pr\" id=\"_address_ state_p\" class=\"form-control\">"
        "   <option value=\"Adjuntas\" selected>Adjuntas</option>"
        "   <option value=\"Aguada\"> Aguada</option>"
        "   <option value=\"Aguadilla\">\tAguadilla\t</option>"
        "   <option value=\"Aguas Buenas\">\tAguas Buenas\t</option>"
        "</select>";
    
     lr_output_message ("Test Regexp Extract");
     
     lr_save_param_regexp (
        bufferToSearch,
               strlen(bufferToSearch),
               "RegExp=option value=\"(.*?)\"",               
               "ResultParam=fromAccount",
               LAST );
    
    lr_output_message ("Value of fromAccount in buffer1 = %s", lr_eval_string("{fromAccount}"));
     
     lr_save_param_regexp (
        bufferToSearch2,
        strlen(bufferToSearch2),
        "RegExp=option value=\"(.*?)\"",
        "Ordinal=All",
        "ResultParam=reMatchesParam",
        LAST );

    matchCt = lr_paramarr_len("reMatchesParam");
    
    lr_message("%d match(es) found in buffer2.", matchCt);

    for (ord=1; ord <= matchCt; ord++) 
    {
        arrayMemberValue = lr_paramarr_idx("reMatchesParam", ord);
        lr_message("Member %d value: %s", ord, arrayMemberValue);
    }
    
    
    // find selected option - use a non capturing group to skip the greedy RegExp for prior Option values
   
    
    // https://regex101.com/
    // PCRE (?:value=".*").*value=(.*?) selected
    
     lr_save_param_regexp (
        bufferToSearch3,
        strlen(bufferToSearch3),
        "RegExp=(?:value=\".*\").*value=\"(.*?)\" selected",
        "Ordinal=1",
        "ResultParam=reSelectedOption",
        LAST );

    lr_output_message ("Value of selected option in buffer3 = %s", lr_eval_string("{reSelectedOption}"));
    
    // normally will use web_reg_save_param_ex with LB and RB to extract the specific select statement 
    
    lr_save_param_regexp (
        bufferToSearch4,
        strlen(bufferToSearch4),
        "RegExp=select id=\"occupation\"(.*?)</select>",
        "Ordinal=1",
        "ResultParam=reOptionGroupOccupation",
        LAST );
    
     lr_output_message ("Value of option group occupation in buffer4 is = %s", lr_eval_string("{reOptionGroupOccupation}"));
     
      lr_save_param_regexp (
        lr_eval_string("{reOptionGroupOccupation}"),
        strlen( lr_eval_string("{reOptionGroupOccupation}")),
        "RegExp=(?:value=\".*\").*value=\"(.*?)\" selected",
        "Ordinal=1",
        "ResultParam=reSelectedOption",
        LAST );

    lr_output_message ("Value of selected occupation option in buffer4 = %s", lr_eval_string("{reSelectedOption}"));
    
    return -1;  // iterate no further
}