0

I'm writing an automation test. I've got an endpoint URL 'https://deckofcardsapi.com/api/deck/new/shuffle/?deck_count=1' when I go onto this endpoint there is text that is printed.

Is there any way in my test I can validate some of the data within the text that is printed. For example, validate when I go to the endpoint remaining is 52.

{  
   "deck_id":"qzpre4zxokj7",
   "shuffled":true,
   "remaining":52,
   "success":true
}
Pankwood
  • 1,799
  • 5
  • 24
  • 43
L.Dockster
  • 169
  • 1
  • 3
  • 12
  • 1
    Why are you using selenium to test a non-html endpoint? Wouldn't using `HttpClient` or `HttpResponse` be more appropriate? – gunr2171 Jul 31 '18 at 19:41
  • this looks like a JSON response. Use a JSONDeserializer to convert it into an object, then check `remaining == 52`. [example](https://stackoverflow.com/a/7895168/1132334) – Cee McSharpface Jul 31 '18 at 19:41
  • That is likely a "REST" endpoint. When you do a GET from that endpoint, it is returning with "JSON" (JavaScript Object Notation). What you are seeing is an object returned in JSON format. The object has 4 properties (deck_id, shuffled, remaining, and success). The value of "remaining" is 52. Find a JSON parser and you should be able to see this – Flydog57 Jul 31 '18 at 19:42
  • @Flydog57 How would i do this? – L.Dockster Jul 31 '18 at 19:43
  • @Pankwood: your edit makes everything easy to see. However, although your edit is equivalent to what Dockster is seeing, it's not really "what he's seeing" – Flydog57 Jul 31 '18 at 19:44
  • The internet is blessed with very good search tools. I've never used Selenium before (though, coincidentally, I'm going to some training later this week). But, if I pop this ("selenium json parser") into one of those search tools, I get responses – Flydog57 Jul 31 '18 at 19:46
  • @Flydog57 fixed. – Pankwood Jul 31 '18 at 19:47
  • What tool did you use? - for the next person who looks at this question... – Flydog57 Jul 31 '18 at 20:09

2 Answers2

1

You can use the JSON parser to extract the remaining value.

NuGet Package :

Newtonsoft.Json

I assumed you are getting the below text in your response.So, please use the below code to extract the remaining value

{  
   "deck_id":"qzpre4zxokj7",
   "shuffled":true,
   "remaining":52,
   "success":true
}

Code:

var expectedValue =52;
var apiResponse = <<Stroe the API Text Response>>;

var jsonObject = JObject.Parse(apiResponse);
var remaining = jsonObject["remaining"].ToString();//It will retrun the value as 52
var actualValue = Int64.Parse(remaining);

Assert.AreEqual(expectedValue, actualValue);//Validate the remaing Value from the API Response
Subburaj
  • 2,294
  • 3
  • 20
  • 37
0

That is likely a "REST" endpoint. When you do a GET from that endpoint, it is returning with "JSON" (JavaScript Object Notation). What you are seeing is an object returned in JSON format. The object has 4 properties (deck_id, shuffled, remaining, and success). The value of "remaining" is 52. Find a JSON parser and you should be able to see this.

If you search the internet, you should find Selenium tools that can parse JSON (sorry, I'm not an expert in that technology).

Flydog57
  • 6,851
  • 2
  • 17
  • 18