0

I have got the following response for one of my HTTP request in JMeter:

  <div class="container">
    <h2>Choose your departure city:</h2>
    <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>

Now I want to select the fromPort values and also the toPort values in two different variables and then pass it on to the next request. I build the regex expression but it captures all the values of fromPort and toPort in a single array. I need to extract these in two different arrays and then call them. So I want to know about the right regex for this ??

Satyajit
  • 152
  • 10
  • 1
    If you need to extract two different sets of values in two different arrays, you'll need two different patterns to be executed. You won't be able to do that with only one regular expression. – Anthony BONNIER Aug 10 '18 at 07:56
  • That's ok. I can use two different patterns but what is the pattern that needs to be used? – Satyajit Aug 10 '18 at 08:12
  • 1
    I tried my best to get the right regex but it seems very hard to achieve it or even impossible. Actually, in this other topic https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454, they said that it's really unadvised to use regex for parsing html. Here is the best regex I've obtained but it only finds the first match of the select : `(?<= – Anthony BONNIER Aug 13 '18 at 11:31

2 Answers2

1

I have tried with CSS/JQuery Extractor. Check the below snapshot for format and output. Below is for "from port" and second CSS selector for "TO port". Just change the CSS expression for 2nd extractor from select[name=fromPort] to select[name=toPort]. enter image description here

Output in View Results enter image description here Now, you have the array. I hope this helps.

Update:-

Use the below syntax for the CSS/JQuery Extractor.

enter image description here

Below is the required output. Since, we are using Match No. as 0..values will be random on each iteration.

enter image description here

sunny_teo
  • 1,961
  • 1
  • 7
  • 11
  • 1
    If you need one value from each to and from in a random manner and use it in the next request then check the solution provided by Rohit. I think. it will fit your requirement. – sunny_teo Aug 10 '18 at 11:05
  • I got the values in the array as per your suggestion but when I try to use the variable name it is inserting all the locations in the from and to field. As shown below it is inserting all values, how to avoid this and just send one value at a time which should be randomly picked. – Satyajit Aug 10 '18 at 11:08
  • I tried that approach too but its giving me error - Assertion error: false Assertion failure: true Assertion failure message: The markup in the document preceding the root element must be well-formed. See log file for further details. – Satyajit Aug 10 '18 at 11:14
  • 1
    Updated my answer. Kindly check, if it helps. – sunny_teo Aug 10 '18 at 12:01
  • Thanks, @sunny_teo for the help, it worked and I am able to find what I wanted, appreciate your help. – Satyajit Aug 12 '18 at 16:20
  • If it helps, kindly accept it as an answer and vote. – sunny_teo Aug 12 '18 at 16:30
0

The easiest way to extract this kind of values is to use Xpath Extractor. Add a xpath extractor as a child to the request. To extract fromPort you can use //select[@name='fromPort']/* as Xpath and match no 0 to get any random value from fromport options

To extract toPort you can use //select[@name='toPort']/* and match no 0 as shown below

enter image description here

Result : enter image description here use ${variableName} in your script

More information:

Jmeter- extract variables

Please let me know if it helps..

Rohit
  • 601
  • 4
  • 12
  • I tried using this approach but getting this error: Assertion error: false Assertion failure: true Assertion failure message: The markup in the document preceding the root element must be well-formed. See log file for further details. – Satyajit Aug 10 '18 at 11:11
  • I suspect that error is because of malformed XML.. Your XML should be well-formed before processing .. https://stackoverflow.com/questions/46355454/how-to-fix-error-the-markup-in-the-document-following-the-root-element-must-be could you please share entire XML file so that i can reproduce the issue and provide exact solution – Rohit Aug 10 '18 at 12:05