Capturing response values from multiple dropdowns in Loadrunner - regex

I have to capture the values of options in the <select name="fromPort"> and <select name="toPort"> separately for correlation using loadrunner.
I have written the regular expression as
<select name="fromPort" class="form-inline">(.+?)</select>
for capturing from first but it shows that there is no match.
<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>
When I try with notepad++, there is a checkbox to select when there are multiple lines of response to be captured. When I check it, the same regular expression is working.enter code here

My answer is less elegant, considering you have multiple similar tags involved on the from/to side of the house. I am going to suggest backing up, capturing with a LB condition of either \"fromPort" class=\"form-inline\"\n" or \"toPort" class=\"form-inline\"\n" to an end tag of "\n"
You will wind up with two string variables, such as "Correlated_from" and "Correlated_to" which contain the option values. Next is conversion to an array structure.
There are likely a dozen ways that you can take the string apart, but I would recommend a function that breaks on the newline character, jumps the first "\t\t" Character in the substring, saving out the value between that and the next "<" character. As noted previously, there are likely a dozen ways to break up this string with a defined pattern to get to the substring which interests you.
By adhering to the standard LoadRunner pseudo array structure, this opens up the use of the standard LoadRunner functions for assignment of a variable from a pseudo array - The lr_paramarr_* function set.

You can try this
Well what i have done is.
Extracted all the options from the select matching select tag using this
(?:<select name="(fromPort|toPort)" class="form-inline">\s+)([\w\W]+?)(?=\s+<\/select>)
Than split each option as single element into an array.
Than extracted value from each option using this regex.
(?<=\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);

I extract the select using LB="select id="xxxx"" and RB="</select"
Then use something like the following to get either all options (if I want to randomize which one is submitted the script, or I pick the already selected one, if I want to submit what the default selection is. Whenever I need to use complex regular expressions I usually hardcode a test case into a temporary test action to test the logic out before hitting the actual application.
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
}

Related

Django: Multiselect Unexpected behaviour

Here is my Multiselect
<div class="form-group">
<label>Multiple select using select 2</label>
<select class="js-example-basic-multiple w-100" id='mls' name="resources" multiple="multiple">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
<option value="AM">America</option>
<option value="CA">Canada</option>
<option value="RU">Russia</option>
</select>
</div>
Whenever I try to post despite selecting multiple values I still get only one .
Here is stacktrace.
Variable Value
csrfmiddlewaretoken
'aI5tuSxOtxzGOpMDKR4RcH685yWUFpqkgTeBrYVbQ8kN9ODxnPOytllMTAb11Bib'
acc_id
'1'
resources
'AM'
I tried with getlist as well still getting single value we all can see single values are passing in request itself.
Not sure what might I am doing wrong here .
It seems you are send single value from front.
i tested it:
<form method="GET">
<div class="form-group">
<label>Multiple select using select 2</label>
<select class="js-example-basic-multiple w-100" id='mls' name="resources" multiple="multiple">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
<option value="AM">America</option>
<option value="CA">Canada</option>
<option value="RU">Russia</option>
</select>
</div>
<button type="submit">Show me markers</button>
</form>
i see in request:
"GET /test/testmarkers/?resources=WY&resources=AM HTTP/1.1" 200 1867
And i am sure, you are use not Form.send(). How are you send it?

Matching a string pattern which starts and ends with a string in imacros

Suppose This is my text
SET abc "<select name="ctl00$ContentPlaceHolder1$ddlyear" onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$ddlyear\',\'\')', 0)" id="ctl00_ContentPlaceHolder1_ddlyear" class="page-heading" style="background-color: rgb(255, 244, 244); width: 95px; outline: 1px solid blue;"> <option selected="selected" value="Select Year">Select Year</option> <option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
</select>"
And I just want to extract years from the above text, so I matched the staring string as <option value= and ending string as </option> and get the string between these two pattern
SET text EVAL("var s=\"{{abc}}\"; s.match(\"(?![<option value=])(.*)(?=</option>)\");")
But it gives the entire text again
And if I do
SET text EVAL("var s=\"{{abc}}\"; s.match(\"(?![<option value=])(.*)(?=</option>)\");s[0]")
This gives me "<"
Any suggestion where I'm going wrong?
Thanks
I think you can try a simpler way like this:
TAG XPATH="id('ctl00_ContentPlaceHolder1_ddlyear')" EXTRACT=TXTALL
SET numbers EVAL("'{{!EXTRACT}}'.replace('Select Year[OPTION]', '');")
PROMPT {{numbers}}
I can't test an imacros version, but your plain regex is wrongfully using square brackets and negative lookahead.
Here is a regex that matches what you are looking for (all digits before an </option>) :
([0-9]*)(?=</option>)
Demo here :
https://regex101.com/r/jA3Al9/2
Edit : as I said I can't test it but I guess you are looking for :
SET text EVAL("var s=\"{{abc}}\"; s.match(\"([0-9]*)(?=</option>)\");s[0]")
From #Shugar's answer added few lines to get the answer
TAG XPATH="id('ctl00_ContentPlaceHolder1_ddlyear')" EXTRACT=TXTALL
SET numbers EVAL("'{{!EXTRACT}}'.replace('Select Year[OPTION]', '');")
SET numb1 EVAL("'{{numbers}}'.replace(/[OPTION]/g,'');")
SET numb2 EVAL("'{{numb1}}'.split('[]')")
PROMPT {{numb2}}

how to ignore a word to replace using regEx?

<select class="form-control" name="work_status">
<option value="">Working With</option>
<option value="Private Company">Private Company</option>
<option value="Government/ Public sector">Government/Public Sector</option>
<option value="Government/ Public sector">Defense/Civil Services</option>
<option value="Government/ Public sector">Business/Self Employed</option>
<option value="Government/ Public sector">Not Working</option>
</select>
Here I want to remove all <option value="Ignore this">. This means I want to replace <option value=""> with "" and <option value="Private Company"> with "". I.e. I want to ignore word inside value attribute. How can I do this?
You must specify a language but for the regex part you can use this : value="(.+)"
Regex101
Anyway you dont need Regex. You can do this with javascript. Let say you use jQuery:
$('.form-control option').each(function(){
$(this).attr('value',"");
});

Trying to fill a form with Scrapy FormRequest, unexpected results

I'm trying to fill the form that is at www.wetseal.com/Stores that allows selecting the state to show stores from.
<form action="http://www.wetseal.com/Stores?dwcont=C73689620" method="post" id="dwfrm_storelocator_state">
<fieldset>
<div class="form-row required ">
<label for="dwfrm_storelocator_address_states_stateUSCA">
<span>State</span>
<span class="required-indicator">*</span>
</label>
<select id="dwfrm_storelocator_address_states_stateUSCA" class="input-select required" name="dwfrm_storelocator_address_states_stateUSCA">
<option value="">Select...</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="PR">Puerto Rico</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
</select>
</div>
<button type="submit" name="dwfrm_storelocator_findbystate" value="Search">
Search
</button>
</fieldset>
</form>
Looking with Chrome I can see the request being made and the form params:
That said, I have a very simple spider that, looking at the docs, sends a FormRequest to that URL to fill the form (In this case I'm testing for Arizona shops - AZ):
class WetSealStoreSpider(Spider):
name = "wetseal_store_spider"
allowed_domains = ["wetseal.com"]
start_urls = [
"http://www.wetseal.com/Stores"
]
def parse(self, response):
yield FormRequest.from_response(response,
formname='dwfrm_storelocator_state',
formdata={'dwfrm_storelocator_address_states_stateUSCA': 'AZ',
'dwfrm_storelocator_findbystate': 'Search'},
callback=self.parse1)
def parse1(self, response):
print response.status
print response.body
When it gets to make the FormRequest, looking at the response, everything seems OK:
But in the callback method, I see this in the response:
It looked like a GET request was made at the end, and the url is all wrong:
'http://www.wetseal.com/Search?q=&dwfrm_storelocator_findbystate=Search&dwfrm_storelocator_address_states_stateUSCA=AZ'
Any idea what I'm doing wrong?
Thanks!
You're using formname but the form doesn't have a name.
Try using formxpath='id("dwfrm_storelocator_state")' instead.
try this
states = response.xpath(
".//select[#id='dwfrm_storelocator_address_states_stateUSCA']//option[#value!='']/#value").extract()
url = self.get_text_from_node(response.xpath("//form[#id='dwfrm_storelocator_state']/#action"))
for state in states:
form_data = {'dwfrm_storelocator_address_states_stateUSCA': state,
"dwfrm_storelocator_findbystate": "Search"}
yield FormRequest(url,
formdata=form_data,
callback=self.your_Callback)

Transfer Value from drop down to a

I need some help ...
I need the location drop down box to determine the appropriate inbox the form should be sent to.
ex if I choose Houston from the drop down box it will send it to the PayrollUSA email.
Im currently using a radio button to make the selection but i would like to automate with the drop down.
Im pretty new to this but im sure theres a if statement that can tie them together...
im using .asp for this.
<input type="radio" name="payroll" value="PayrollUSA#mail.com" checked="checked">US Payroll
<input type="radio" name="payroll" value="PayrollCAN#mail.com">CAN Payroll
<input type="radio" name="payroll" value="PayrollUK#mail.com">UK Payroll
<input type="radio" name="payroll" value="PayrollHK#mail.com">HK Payroll
Drop down selection
<SELECT SIZE="1" NAME="Business_Unit" style="width: 205px;" class="answers">
<option selected >Select</option>
<OPTION>Calgary</OPTION>
<OPTION>Chicago</OPTION>
<OPTION>Hong Kong</OPTION>
<OPTION>Houston</OPTION>
<OPTION>London</OPTION>
<OPTION>Los Angeles</OPTION>
<OPTION>Montreal</OPTION>
<OPTION>New York</OPTION>
<OPTION>New York Corporate</OPTION>
<OPTION>Philadelphia</OPTION>
<OPTION>San Francisco</OPTION>
<OPTION>Toronto</OPTION>
<OPTION>Toronto Corporate</OPTION>
<OPTION>Vancouver</OPTION>
</SELECT>
If you need the option value to be held then try this:
<SELECT SIZE="1" NAME="Business_Unit" style="width: 205px;" class="answers">
<option value="">Select an Option</option>
<option <% if Business_Unit= "PayrollCAN#mail.com" then %> selected <% End if %> value="PayrollCAN#mail.com">Calgary</option>
<option <% if Business_Unit= "PayrollUSA#mail.com" then %> selected <% End if %> value="PayrollUSA#mail.com">Chicago</option>
</select>
etc. for each option and same value may be used for different option displays
I imaging this should just be html based. So your option menu should look like:
<SELECT SIZE="1" NAME="Business_Unit" style="width: 205px;" class="answers">
<option value="-1">Select an Option</option>
<OPTION value="PayrollCAN#mail.com">Calgary</OPTION>
<OPTION value="PayrollUSA#mail.com">Chicago</OPTION>
<OPTION value="PayrollHK#mail.com">Hong Kong</OPTION>
<OPTION value="PayrollUSA#mail.com">Houston</OPTION>
<OPTION value="PayrollUK#mail.com">London</OPTION>
<OPTION value="PayrollUSA#mail.com">Los Angeles</OPTION>
<OPTION value="PayrollCAN#mail.com">Montreal</OPTION>
<OPTION value="PayrollUSA#mail.com">New York</OPTION>
<OPTION value="PayrollUSA#mail.com">New York Corporate</OPTION>
<OPTION value="PayrollUSA#mail.com">Philadelphia</OPTION>
<OPTION value="PayrollUSA#mail.com">San Francisco</OPTION>
<OPTION value="PayrollCAN#mail.com">Toronto</OPTION>
<OPTION value="PayrollCAN#mail.com">Toronto Corporate</OPTION>
<OPTION value="PayrollCAN#mail.com">Vancouver</OPTION>
</SELECT>
You can use the same value multiple times if needed