0

We've been trying to automate a webservice using selenium eclipse and http components.

In one of the queries we have we are passing two parameters "Param1" and "Param2" which will look like this in the endpoint url: http://xxx.xx.xx.xxxx:8080/sample/?Param1=value1&Param2=value2

We have an existing framework where we get the parameters from an excel sheet data table so users are free to change Param1, Param2, value1 or value2 and then pass the values as part of the whole url for execution

We won't have any problems when handling only one parameter/value as we would have something like this:

String url = "http://xxx.xx.xx.xxxx:8080/sample/"+Param1+"="+value1;

where both Param1 and value1 will be retrieved from our data table.

My question is, what will be the best approach in setting up the string url when parameters can be one or two. So that when I pass two parameter values they will automatically be joined by "&" (?Param1=value1&Param2=value2) but when I'm only passing one it will stop at this (?Param1=value1).

Sorry if this is a dumb question, I'm just starting to learn automation and would like to get the best approach for this. I was thinking this can be done by simple if else statement or having Param2 include the & character but still would like to get advise

Forged
  • 171
  • 10
  • first of all - google for url encoding – Iłya Bursov May 15 '17 at 05:33
  • 1
    https://docs.oracle.com/javase/7/docs/api/java/net/URI.html – Iłya Bursov May 15 '17 at 05:34
  • I believe this type of question has been answered here, there can be two ways:one is by using an if() inside the loop of params-Array and another by using the "NameValuePair". You can look at these for ideas - 1. http://stackoverflow.com/questions/26177749/how-can-i-append-a-query-parameter-to-an-existing-url, 2. http://stackoverflow.com/questions/2959316/how-to-add-parameters-to-a-http-get-request-in-android – Jehoshuah May 15 '17 at 06:14
  • Thanks a lot! I was able to do it using NameValuePair – Forged May 15 '17 at 09:12

0 Answers0