-2

I am using Loadrunner and need to come up with a regex that will pick up a particular word from the HTTP response coming in and save it in a parameter.

The response looks like this:

\t\t\t\t\t<option value="Rose taupe" id="WD26" class="WD26">Rose taupe</option>\n
\t\t\t\t\t<option value="Myrtle" id="WD20" class="WD20">Myrtle</option>\n
\t\t\t\t\t<option value="Deep carmine pink" id="WD142" class="WD142">Deep carmine pink</option>\n
\t\t\t\t\t<option value="Wild Strawberry" id="WD66" class="WD66">Wild Strawberry</option>\n
\t\t\t\t\t<option value="Cream" id="WD72" selected="selected" class="WD72">Cream</option>\n
\t\t\t\t\t<option value="Tangerine yellow" id="WD94" class="WD94">Tangerine yellow</option>\n

I want to pick up the color that is selected in a drop down menu in the front end, in the response this color line has selected="selected" in it. This is however random for each instance hence the regex has to pickup the color name from the line which contains selected="selected".

I then have to use it in my script as follows:

web_reg_save_param_regexp ("ParamName=SelectedColor",
"RegExp=-regular expression here-",
"Ordinal=All",
LAST);

Thank you for your help!

AnirudhB
  • 17
  • 6
  • I would think an HTML parser would be far more suited to this task than a RegEx. – Eric J. Oct 12 '15 at 03:02
  • Thank you, but I'm unsure if you can use that in LR. – AnirudhB Oct 12 '15 at 03:06
  • This is one possible alternative: ` – Jerry Jeremiah Oct 12 '15 at 03:09
  • Thanks for that Jerry, the attributes don't really change order so I will try your solution now. Cheers! – AnirudhB Oct 12 '15 at 20:58
  • Seems to be your doing "My Load Test Correlation Challenge " created by Stuart Moncrieff. From the website: "Please don’t share your solutions, ask for help, or help others. This is meant to be a challenge." So you appear to be violating one of the primary rules in this website. I have looked at this challenge and it's really good to increase someone's knowledge. – sivaramaraju Oct 15 '15 at 04:04
  • Well yeah, I wouldn't deny that. I'm a beginner who's started using LR a month ago and never really used code, have been trying this for 2 weeks and finally asked. I'm doing the rest of it by myself, this is the only place where I really got stuck and gave in. – AnirudhB Oct 15 '15 at 20:31

2 Answers2

0

The RegExp /<\s*option\s+(?=.*selected\s*=).*value\s*=\s*(?:"([^"]*)"|'([^']*)')/g will match the option tag with the selected attribute. The order of attributes is arbitrary. If attribute quotes may be ' then you need to handle the second match group, see example.

var s = '\t\t\t\t\t<option value="Rose taupe" id="WD26" class="WD26">Rose taupe</option>\n' +
        '\t\t\t\t\t<option value="Myrtle" id="WD20" class="WD20">Myrtle</option>\n' +
        '\t\t\t\t\t<option value="Deep carmine pink" id="WD142" class="WD142">Deep carmine pink</option>\n' +
        '\t\t\t\t\t<option value="Wild Strawberry" id="WD66" class="WD66">Wild Strawberry</option>\n' +

        '\t\t\t\t\t<option value="Cream" id="WD72" selected="selected" class="WD72">Cream</option>\n' +

        '\t\t\t\t\t<option id="WD72" value=\'Cream\' selected="selected" class="WD72">Cream</option>\n' +

        '\t\t\t\t\t<option value="Tangerine yellow" id="WD94" class="WD94">Tangerine yellow</option>\n';

var re = /<\s*option\s+(?=.*selected\s*=).*value\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
var m;

while (m = re.exec(s)) {
    console.log("Selected color: " + (m[1]||m[2]) + ", match " + m[0]);
}
Dmitry Sokolov
  • 3,118
  • 1
  • 30
  • 35
0

Below code will work.

web_reg_save_param_ex(
        "ParamName=SelectedItem",
        "LB/IC/DIG=id=\"^^^^\" selected=\"selected\"",
        "RB=</option>",
        "Ordinal=1",
        SEARCH_FILTERS,
        "Scope=ALL",
        LAST);
NaveenKumar Namachivayam
  • 1,163
  • 3
  • 25
  • 39