|
| 1 | +## Appium interceptor commands |
| 2 | + |
| 3 | +Create the appium session by passing `appium:intercept : true` option in the desired capability. Once the session is successfully created, tests can manage the api mocking using below commands. |
| 4 | + |
| 5 | +### Mock Configuration |
| 6 | +Mock configuration is a json object that defines the specification for filtering and applying various updates to the api request and below is the structure for the config object. |
| 7 | + |
| 8 | +```javascript |
| 9 | +{ |
| 10 | + url: string; |
| 11 | + method?: string; |
| 12 | + updateUrl?: [{ regexp: string, value: string}]; |
| 13 | + headers?: object | { add: object : string, remove: string[]}; |
| 14 | + requestBody?: string; |
| 15 | + updateRequestBody?: [{regexp: string, value: string }] | [{jsonPath: string, value: string}]; |
| 16 | + statusCode?: number; |
| 17 | + responseHeaders?: object | { add: object : string, remove: string[]}; |
| 18 | + responseBody?: string; |
| 19 | + updateResponseBody?: [{regexp: string, value: string }] | [{jsonPath: string, value: string}]; |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +| Name | Type | Required | Description | Example | |
| 24 | +| ------ | ------ | -------- | --------------------------------------------------------- | ------- | |
| 25 | +| url | string | yes | Regular Expression or Glob pattern matcher to filter the request for applying the mock | Sample url : `https://www.reqres.in/api/users?page=1` <br> Regex example: `/api/users?.*/g` <br> Glob pattern: `**/api/users*?*` | |
| 26 | +| method | string | no | Method to matching the request for applying the mock | `GET` / `POST` / `PUT` / `PATCH` | |
| 27 | +| updateUrl |string | no | Regular Expression patter and replaceable value to update the outgoing url | Sample url : `https://www.reqres.in/api/users?page=1` <br> When passing `{ updateUrl : {regexp: "/page=(\\d)+/g", value: "page=5"} }` <br> Then outgoing url will be replaced to `https://www.reqres.in/api/users?page=2` | |
| 28 | +| headers | object | no | Map of key value pairs to be added or removed from the request header | 1. Passing a plain object map will add all the key: value pairs to the request headers `{headers : {"Content-Type" : "application/json", "Authorization": "Bearer sometoken"} }` <br> 2. If you want to add and remove header values simulaneously then you can pass the header as `{headers : { add : {"Content-Type" : "application/json"}, remove: ["Authorization", "X-Content-Type"] } }` | |
| 29 | +| requestBody | string | no | This will replace the original payload (post body) send to api the from the application and updates it with new body | When passing `{"url" : "/api/login/g" , "requestBody": "{\"email\": \"invalid@email.com\", \"password\": \"wrongpassword\"}"}` will send the given payload for all login api calls made from the application | |
| 30 | +| updateRequestBody | string | no | This is similar to requestBody but instead of fully replacing the request payload, you can replace any value in the payload using Regular expression or jsonpath | Consider you application sending `{\"username\": \"someusername\", \"email\": \"someemail@email.com\", \"password\": \"somepassword\", \"isAdmin\" : \"false\"}` as a payload for a register user api request and you want to update the email and username, then you can pass <br> `{"updateRequestBody": [{ "jsonPath": "$.email", "newemail@email.com" }, { "jsonPath": "$.username", "new_username" }]}` and it will update the email and username field before sending the request to the server| |
| 31 | +| statusCode | number | no | Updates the response status code with the given value | To simulate any unexpected error you can send some of the below statusCode <br> 1. `500` - Internal server error <br> 2. `400` - Bad request <br> 3. `401` - Unauthorized | |
| 32 | +| responseHeaders | object | no | Map of key value pairs to be added or removed from the response header | Same syntax as `headers` key. But this will update the response header | |
| 33 | +| responseBody | object | no | This will replace the original response data returned by the api server and updates it with new data | Passing the config as `{"url" : "/api/login/g" , "responseBody": "{\"error\": \"User account locked\"}", `statusCode`: 400 }` will simulate a error scenario when logged in with any user credentilas | |
| 34 | +| updateResponseBody | string | no | This is similar to responseBody but instead of fully mocking the server response, you can replace any value in the response using Regular expression or jsonpath | Consider you application returns user data as `{\"username\": \"someusername\", \"email\": \"someemail@email.com\", \"isAdmin\" : \"false\"}` as a response for get user api request and you want to update the values for email and IsAdmin fiels, then you can pass <br> `{"updateRequestBody": [{ "jsonPath": "$.email", "newemail@email.com" }, { "jsonPath": "$.isAdmin", "true" }]}` and it will update the email and isAdmin field before sending the response back to the application| |
| 35 | + |
| 36 | + |
| 37 | +## Commands: |
| 38 | + |
| 39 | +### interceptor: addMock |
| 40 | + |
| 41 | +Add a new mock specification for intercepting and updating the request. The command will returns a unique id for each mock which can be used in future to delete the mock at any point in the test. |
| 42 | + |
| 43 | +#### Example: |
| 44 | + |
| 45 | +```javascript |
| 46 | + const authorizationMock = await driver.execute("interceptor: addMock", [{ |
| 47 | + config: { |
| 48 | + url "*reqres.in*", |
| 49 | + headers: { |
| 50 | + "Authorization" : "Bearer bearertoken" |
| 51 | + } |
| 52 | + } |
| 53 | + }]); |
| 54 | + |
| 55 | + const userListGetMock = await driver.execute("interceptor: addMock", [{ |
| 56 | + config: { |
| 57 | + url "**reqres.in/api/users", |
| 58 | + method: "GET", |
| 59 | + responseBody: JSON.stringify({ |
| 60 | + page: 2, |
| 61 | + count: 2, |
| 62 | + data: [ |
| 63 | + { |
| 64 | + "first_name" : "User", |
| 65 | + "last_name" : "1" |
| 66 | + }, |
| 67 | + { |
| 68 | + "first_name" : "User", |
| 69 | + "last_name" : "2" |
| 70 | + } |
| 71 | + ] |
| 72 | + }) |
| 73 | + } |
| 74 | + }]); |
| 75 | +``` |
| 76 | + |
| 77 | +`authorizationMock` will be executed for all api calls made to `reqres.in` domain and `userListGetMock` will be applied for `https://www.reqres.in/api/users` with `GET` http method. |
| 78 | + |
| 79 | +### interceptor: removeMock |
| 80 | +Given a mockId return during addMock command, will remove the mock configuration from the proxy sever. |
| 81 | + |
| 82 | +#### Example: |
| 83 | + |
| 84 | +```javascript |
| 85 | + const authorizationMock = await driver.execute("interceptor: addMock", [{ |
| 86 | + config: { |
| 87 | + url "*reqres.in*", |
| 88 | + headers: { |
| 89 | + "Authorization" : "Bearer bearertoken" |
| 90 | + } |
| 91 | + } |
| 92 | + }]); |
| 93 | + |
| 94 | + //peform user action |
| 95 | + //perform validation |
| 96 | + .. |
| 97 | + .. |
| 98 | + |
| 99 | + await driver.execute("interceptor: removeMock", [{ |
| 100 | + id: authorizationMock |
| 101 | + }]); |
| 102 | + |
| 103 | + // authorizationMock will not be active after this point and the test will proceed with normal flow |
| 104 | +``` |
| 105 | + |
0 commit comments