Home > database >  How can i replace the dynamic content in url?
How can i replace the dynamic content in url?

Time:09-28

i have a API url http://test:1000/v5/accounts/101/transactions?limit=10&offset=10. in my automation script, my test case sometimes have either limit or offset or both with values changing in consecutive calls. If url has offset, i need to update the url with offset with next value as "&offset=20" and so on in next calls too. how can i do it dynamically inside the java script? any idea please.

CodePudding user response:

If I understand correctly what is you asking for, you can do the following:

  1. Retrieve from the URL offset value (in case there is such there)
  2. Update the offset value
  3. Update the URL with the new offset value.
    Simple Java code to do this can look as following:
String url = "http://test:1000/v5/accounts/101/transactions?limit=10&offset=10";
String offset = url.split("offset")[1];
if(offset != null && !offset.trim().isEmpty()){
    offset = offset.substring(offset.indexOf("=")   1);
    int updatedOffsetValue = Integer.parseInt(offset)   10;
    url = url.substring(0,url.lastIndexOf("=") 1)   String.valueOf(updatedOffsetValue);
}
  • Related