KEMBAR78
Increase automation to rest | PPTX
Increase Automation to
REST
Understanding Web Services
and how to test them
Shiva and Fiona
Key
Take
Aways:
❏ What Web Services are and why we
use them
❏ How to test a Web Service in
multiple ways
❏ Increased familiarity with test
automation
Web Services
● Interface to provide access to
functionality
● Uses HTTP/HTTPS/SMTP for
communication
● Communicates using
JSON/XML/CSV/Plain Text etc.
When and Why
Same functionality multiple UIs:
● Customise cross platform
● Multiple apps
● Different vendors
Being able to implement one
service and use that functionality
across all these different displays
Let see an example!
...things get clearer when you can see them :)
An Example
Booking a ticket with Orange Bus via
different agencies
RedBus UI TravelYari UI Orange Bus UI
Book
a seat
Book
a seat
Get bus
details
Orange Bus Web Service
Another Example
Website serving request:
Weather Underground
Underlying HTTP request:
curl -X GET
http://api.wunderground.com/api/e8
96598d74613088/conditions/q/zmw:
00000.1.43128.json
REpresentational State
Transfer (REST)
● Uses HTTP(S) for CRUD
operations
● Uniform Resource Identifiers
(URIs)
● New alternative to SOAP
Statelessness
Client
Load Balancer
Node 1 Node 2
What about APIs?
● Part of the web service which
receives requests/sends
responses
● Has one or more endpoints
● Documented contract
Requests and Responses
...lets see some examples...
Request
Request:
URI Method Headers DATA
Response
Response
HeadersResponse Code Data
Requests
Common request types:
● GET
● POST
● PUT
● DELETE
Response Codes
Outcome of a request:
● 1xx - Informational
● 2xx - Success
● 3xx - Redirection
● 4xx - Client Error
● 5xx - Server Error
QAs and Web Services
...based on our experiences testing web services out in the wild...
How testing helps
● Business issues first then the
presentation issues
● Early feedback
● Debug in different layers
● Save time retesting
● More confident app works
● Release more frequently
● Lots more….
The role of QAs
Demo - try it yourself!
Manual in browser test
https://petal-spirit.hyperdev.space/
Postman
Sample Postman Request:
curl
Sample Curl Request:
curl -X GET https://petal-spirit.hyperdev.space/dreams -H "Content-Type: application/json" -i
> curl _
Alternatives?
Cocoa Rest Client
IntelliJ REST
Plugin
Let’s automate it! - Java
Use Library: UniRest (Java)
Sample Test Scenario
public class TestDreams {
@Test
public void testGetDreams() throws UnirestException {
System.out.println("Testing Get Dreams.....");
String url = "https://petal-spirit.hyperdev.space/dreams";
HttpResponse<JsonNode> jsonResponse = Unirest.get(url)
.header("Content-Type",
"application/json").asJson();
Assert.assertEquals(200, jsonResponse.getStatus());
Assert.assertNotNull(jsonResponse.getBody());
}
}
Let’s automate it! - Python
Use Package: Requests (Python)
Sample Test Scenario
import unittest
import requests
class TestDreams(unittest.TestCase):
def setUp(self):
self.url = 'http://petal-spirit.hyperdev.space/dreams'
def test_should_verify_get_dreams(self):
print("Verifying Get Dreams....")
headers = {"Content-Type": "application/json"}
res = requests.get(url=self.url, headers=headers, verify=False)
self.assertEqual(200, res.status_code, "Response code doesn't match")
self.assertTrue(res.json())
Closing notes
...time flies when you’re automating tests...
Suggestions
● Automate as you go
● Cross role pairing
● Integrate with build pipeline
● If the service is broken no need
to test UI
● Spread the word! :)
Key
Take
Aways:
❏ What Web Services are and why we
use them
❏ How to test a Web Service in
multiple ways
❏ Increased familiarity with test
automation
Questions

Increase automation to rest

  • 1.
    Increase Automation to REST UnderstandingWeb Services and how to test them Shiva and Fiona
  • 2.
    Key Take Aways: ❏ What WebServices are and why we use them ❏ How to test a Web Service in multiple ways ❏ Increased familiarity with test automation
  • 3.
    Web Services ● Interfaceto provide access to functionality ● Uses HTTP/HTTPS/SMTP for communication ● Communicates using JSON/XML/CSV/Plain Text etc.
  • 4.
    When and Why Samefunctionality multiple UIs: ● Customise cross platform ● Multiple apps ● Different vendors Being able to implement one service and use that functionality across all these different displays
  • 5.
    Let see anexample! ...things get clearer when you can see them :)
  • 6.
    An Example Booking aticket with Orange Bus via different agencies RedBus UI TravelYari UI Orange Bus UI Book a seat Book a seat Get bus details Orange Bus Web Service
  • 7.
    Another Example Website servingrequest: Weather Underground Underlying HTTP request: curl -X GET http://api.wunderground.com/api/e8 96598d74613088/conditions/q/zmw: 00000.1.43128.json
  • 8.
    REpresentational State Transfer (REST) ●Uses HTTP(S) for CRUD operations ● Uniform Resource Identifiers (URIs) ● New alternative to SOAP
  • 9.
  • 10.
    What about APIs? ●Part of the web service which receives requests/sends responses ● Has one or more endpoints ● Documented contract
  • 11.
    Requests and Responses ...letssee some examples...
  • 12.
  • 13.
  • 14.
    Requests Common request types: ●GET ● POST ● PUT ● DELETE
  • 15.
    Response Codes Outcome ofa request: ● 1xx - Informational ● 2xx - Success ● 3xx - Redirection ● 4xx - Client Error ● 5xx - Server Error
  • 22.
    QAs and WebServices ...based on our experiences testing web services out in the wild...
  • 23.
    How testing helps ●Business issues first then the presentation issues ● Early feedback ● Debug in different layers ● Save time retesting ● More confident app works ● Release more frequently ● Lots more….
  • 24.
  • 25.
    Demo - tryit yourself!
  • 26.
    Manual in browsertest https://petal-spirit.hyperdev.space/
  • 27.
  • 28.
    curl Sample Curl Request: curl-X GET https://petal-spirit.hyperdev.space/dreams -H "Content-Type: application/json" -i > curl _
  • 29.
  • 30.
    Let’s automate it!- Java Use Library: UniRest (Java) Sample Test Scenario public class TestDreams { @Test public void testGetDreams() throws UnirestException { System.out.println("Testing Get Dreams....."); String url = "https://petal-spirit.hyperdev.space/dreams"; HttpResponse<JsonNode> jsonResponse = Unirest.get(url) .header("Content-Type", "application/json").asJson(); Assert.assertEquals(200, jsonResponse.getStatus()); Assert.assertNotNull(jsonResponse.getBody()); } }
  • 31.
    Let’s automate it!- Python Use Package: Requests (Python) Sample Test Scenario import unittest import requests class TestDreams(unittest.TestCase): def setUp(self): self.url = 'http://petal-spirit.hyperdev.space/dreams' def test_should_verify_get_dreams(self): print("Verifying Get Dreams....") headers = {"Content-Type": "application/json"} res = requests.get(url=self.url, headers=headers, verify=False) self.assertEqual(200, res.status_code, "Response code doesn't match") self.assertTrue(res.json())
  • 32.
    Closing notes ...time flieswhen you’re automating tests...
  • 33.
    Suggestions ● Automate asyou go ● Cross role pairing ● Integrate with build pipeline ● If the service is broken no need to test UI ● Spread the word! :)
  • 34.
    Key Take Aways: ❏ What WebServices are and why we use them ❏ How to test a Web Service in multiple ways ❏ Increased familiarity with test automation
  • 35.

Editor's Notes

  • #3 Shiva: What are WebServices; Importance of Web Services Learn HOw do we test a WebService using different tools and programming libraries Get familiarised with automating WebService.
  • #4 Fiona (add an example to the explanation) Using web services have been growing in popularity as a interfact for users to interact with systems and for systems to communicate with one another They are an way to access resources needed such as images, files, web pages, data etc. REST is not dependent on any protocol, but almost every RESTful service uses HTTP as its underlying protocol. Common formats for the messages they exchange are listed here but there are more and it depends on the individual requirements
  • #5 Shiva: Application A is written in 1 framework may be JAVA and SPRINGS, can use service available in other application
  • #7 Shiva
  • #8 Fiona another example of a web service is Weather Underground. They collect weather data from weather stations over the world and provide that data to people who wish to use it. Lets have a look at how a request for data about weather in Hyderabad is received in the browser… ...then lets see the same thing in the command line using the curl command
  • #9 Fiona Apologies for such an acronym heavy slide, please bear with me while I ensure we are all on the same page for these terms. REST is one architectural approach to web services which is growing in popularity. It uses moslty the HTTP(S) protocol for CRUD functionality: creation, reading, updating and deleting of resources. URIs are the id of some data which is being referred to in the requested SOAP (Simple Object Access Protocol) is a protocol with uses XML. Its an alternative to REST. SOAP was popular previously but systems built more recently would often be design in a RESTful way so we are seeing less and less SOAP now.
  • #10 Fiona A request cannot be dependent on a past request and a service treats each request independently. Assuming the system has enough load to require more than 1 node … which is pretty common … its important to consider how to handle the likely even where the first and second request go to different nodes or instances of the web service. A key part of a restful architecture is that each request from any client contains all the information necessary to service the request For instance a real world physical example, lets say we walk into a store, they are a huge company with stores all over the globe and thousands of staff members. We make an order with this company and they provide us with an id. If we want to check the status of this order we might not find outselves chatting to the exact staff member again, or they might have served another customer since then and forgotten us, it could be whichever staff member is free, or we might be in a different store. In order for the staff member to be able to responde to our status request we need to provide some information again along with the request. These staff members are like the nodes, they will all be looking at the same system to get your status, you walked into the same store or called the same number, but the nodes or staff themselves are individuals. So they need context on how to help you.
  • #11 Shiva Mention “Application Programming Interface” - API
  • #13 Shiva Separate slide for request and response. Consider using a screenshot of postman.
  • #14 Shiva Separate slide for request and response. Consider using a screenshot of postman.
  • #15 Fiona Get - request a resource, header might be used to specify some conditions similar to a query. Should only read data and shouldnt effect its state. Post - provides some resource (or data) which the service will handle, could override, update, append, act upon... Put - provides some resource which will be placed exactly at that URI location. If there is something already put there it will be overridden Delete - removes the resource at the URI location. Could be stopped by the server but ideally shouldn't say it succeeded unless that resource was deleted but technically could
  • #16 Fiona There are a lot of potential responses but they can be grouped into these 5 main types. Error codes starting with there initial numbers help you to know which type they are. Lets cover some of the most common/important ones in more detail
  • #17 Success! Your request was fulfilled.
  • #18 Success! This request caused something to be created/added/updated successfully. Most likely you posted some data which cased this response
  • #19 Error, Oops, something went wrong. Seems like there’s a problem with the request you made.
  • #20 Error, Oops, something went wrong. Seems that you arent permitted to do that request or arent correctly proving your right to perform that request
  • #21 Error, Oops something went wrong. Seems that the request is correct and you have the permission but the thing you asked for isnt there
  • #22 Error on the other side. Seems like the web service you are making a request to has some issues.
  • #24 Shiva
  • #25 Shiva: UNIT Testing: Talk about TDD: example of writing tests first and developing later
  • #27 Fiona
  • #28 Fiona
  • #29 Shiva
  • #30 Shiva
  • #31 Shiva
  • #32 Shiva
  • #34 Shiva: Automate as you GO: Automate in parallel any other activies(Manual) Cross Role : Code Review, Stop a Bug before being raised , Developers as well can Test , Pairing with BAs can encounter which is missing in the business flow.
  • #35 Fiona
  • #36 Both