1

In Postman I have did in the below way and want to do the same in Rest Assured framework. I want to parse and save the "tltkt" value in Rest Assured framework. How can I do that in Rest Assured?

GET call :https://prod.streaming/com/account/signin/

Postman tests:** Load the HTML response to $

const $ = cheerio.load(pm.response.text())
console.log($("title").text()); // get title

console.log($('script#app-config').text().trim());
let appConfig = JSON.parse($('script#app-config').text().trim());

console.log(appConfig.tltkt);
pm.collectionVariables.set("saved_tl_tkt", appConfig.tl_tkt);

console.log(pm.collectionVariables.get("saved_tl_tkt"), ":from pm");

Response in HTML:

main id="main-container"
script id="app-config" type="application/json"

{"tltkt":"QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ" , "imageHost": https:\/\/prod-wwwimage-us.com, "regionBaseUrl:""};
Wilson
  • 73
  • 8
  • My suggestion is getting Response as String then extract by regex. – lucas-nguyen-17 Mar 06 '22 at 11:39
  • I tried tthe below code in Rest Assured framework  String bodyTxt = response.htmlPath().getString("html.body.main.script") and it worked which gave the {{"tltkt":"QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ" , "imageHost": https:\/\/prod-wwwimage-us.com, "regionBaseUrl:""} . But I am only looking for tltkt and its value. Please need inputs how can get it with regular expression? – Wilson Mar 07 '22 at 05:47

1 Answers1

3

I assume that you've already had {"tltkt":"QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ" , "imageHost": https:\/\/prod-wwwimage-us.com, "regionBaseUrl:""}

Because it's not valid json, so I use regex to extract value from this string.

String bodyTxt = ...;
Pattern pattern = Pattern.compile("\"tltkt\":\"(.+?)\"");
Matcher matches = pattern.matcher(bodyTxt);
String tltkt = "";
if(matches.find()) {
    tltkt = matches.group(1);
}
System.out.println(tltkt);
//QVdMcHpmWitoWENSSU8zN0FtYzNmWlJVdFFrQkoxOUVJTE5iOHQvTXZ
lucas-nguyen-17
  • 5,516
  • 2
  • 9
  • 20