|
| 1 | +--- |
| 2 | +id: api-lifecycle |
| 3 | +title: API Lifecycle |
| 4 | +sidebar_label: API Lifecycle |
| 5 | +--- |
| 6 | + |
| 7 | +# API Lifecycle |
| 8 | + |
| 9 | +This documentation provides a step-by-step guide on how to interact with Sauce Visual API. By following these steps, you'll be able to create builds, upload images, create snapshots, and finish builds. This guide is intended for users who wish to connect directly to the API or implement a binding on their own, including a link to our GraphQL API documentation for further reference. |
| 10 | + |
| 11 | +## What You'll Need |
| 12 | + |
| 13 | +- A Sauce Labs account ([Log in](https://accounts.saucelabs.com/am/XUI/#login/) or sign up for a [free trial license](https://saucelabs.com/sign-up)) |
| 14 | +- Familiarity with GraphQL queries and mutations |
| 15 | +- Tools like Postman or cURL for testing API calls |
| 16 | + |
| 17 | +## Lifecycle Steps |
| 18 | + |
| 19 | +### 1. Create a Build |
| 20 | + |
| 21 | +To start, you need to create a build. This initializes the process and prepares the environment for subsequent steps. |
| 22 | + |
| 23 | +**GraphQL Mutation:** |
| 24 | + |
| 25 | +```graphql |
| 26 | +mutation { |
| 27 | + createBuild(input: { name: "Your Build Name", branch: "Branch name", project: "Project name" }) { |
| 28 | + id |
| 29 | + name |
| 30 | + status |
| 31 | + url |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +- `branch`: Branch name to associate the build with. |
| 37 | +- `project`: Label/project to associate the build with. |
| 38 | + |
| 39 | +**Expected Response:** |
| 40 | + |
| 41 | +```json |
| 42 | +{ |
| 43 | + "data": { |
| 44 | + "createBuild": { |
| 45 | + "id": "build-id-here", |
| 46 | + "name": "Your Build Name", |
| 47 | + "status": "RUNNING", |
| 48 | + "url": "https://app.saucelabs.com/visual/builds/build-id-here" |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +- `status`: As a newly created build without any snapshots, this will be `RUNNING`. |
| 55 | +- `url`: The URL that can be used any time to check the build on Sauce Labs. |
| 56 | + |
| 57 | +### 2. Upload Image |
| 58 | + |
| 59 | +Next, upload an image to the build. This is a two step process. |
| 60 | + |
| 61 | +First, obtain a signed URL for uploading your image by using the `createSnapshotUpload` mutation. |
| 62 | + |
| 63 | +**GraphQL Mutation:** |
| 64 | + |
| 65 | +```graphql |
| 66 | +mutation { |
| 67 | + createSnapshotUpload(input: {buildId: "build-id-here"}) { |
| 68 | + id |
| 69 | + uploadUrl |
| 70 | + domUploadUrl |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +- `buildId`: The ID of the build created in the previous step. |
| 76 | + |
| 77 | +**Expected Response:** |
| 78 | + |
| 79 | +```json |
| 80 | +{ |
| 81 | + "data": { |
| 82 | + "createSnapshotUpload": { |
| 83 | + "id": "upload-id-here", |
| 84 | + "uploadUrl": "image-upload-url-here", |
| 85 | + "domUploadUrl": "dom-upload-url-here" |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +- `id`: Upload ID to use in the subsequent steps. |
| 92 | +- `uploadUrl`: The URL to upload the image in the next step. |
| 93 | +- `domUploadUrl`: The URL to upload the DOM to (if desired and available). Explained in the optional step below. |
| 94 | + |
| 95 | +Next, send a `PUT` request to `uploadUrl` with image file in the body of the request. Only **PNG** files are supported. |
| 96 | + |
| 97 | +**cURL Request:** |
| 98 | + |
| 99 | +```sh |
| 100 | +curl --request PUT \ |
| 101 | + --url 'upload-url-here' \ |
| 102 | + --header 'Content-MD5: base64-encoded-md5-hash' \ |
| 103 | + --header 'Content-Type: image/png' \ |
| 104 | + --data '@my-screenshot.png' |
| 105 | +``` |
| 106 | + |
| 107 | +- `Content-MD5` header: Base64 encoded MD5 hash of the file (`my-screenshot.png`). |
| 108 | +- `Content-Type` header: Must be set to `image/png`. Not other extensions are supported. |
| 109 | + |
| 110 | +Optional: Upload DOM |
| 111 | + |
| 112 | +If desired (and available), DOM can be also uploaded to `domUploadUrl` obtained from `createSnapshotUpload` mutation. |
| 113 | + |
| 114 | +**cURL Request:** |
| 115 | + |
| 116 | +```sh |
| 117 | +curl --request PUT \ |
| 118 | + --url 'dom-upload-url-here' \ |
| 119 | + --header 'Content-MD5: base64-encoded-md5-hash' \ |
| 120 | + --header 'Content-Type: text/html' \ |
| 121 | + --data '@my-dom.html' |
| 122 | +``` |
| 123 | + |
| 124 | +- `Content-MD5` header: Base64 encoded MD5 hash of the file (`my-dom.html`). |
| 125 | +- `Content-Type` header: Must be set to `text/html`. Not other extensions are supported. |
| 126 | + |
| 127 | +### 3. Create Snapshot |
| 128 | + |
| 129 | +After uploading your image, add your snapshot along with its metadata to your build. |
| 130 | + |
| 131 | +**GraphQL Mutation:** |
| 132 | + |
| 133 | +```graphql |
| 134 | +mutation { |
| 135 | + createSnapshot( |
| 136 | + input: { |
| 137 | + buildUuid: "build-id-here", |
| 138 | + uploadId: "upload-id-here", |
| 139 | + name: "Your snapshot name", |
| 140 | + operatingSystem: OS, |
| 141 | + operatingSystemVersion: "os-version", |
| 142 | + browser: BROWSER, |
| 143 | + browserVersion: "browser-version" |
| 144 | + } |
| 145 | + ) { |
| 146 | + id |
| 147 | + uploadId |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | +- `buildUuid`: Build ID that was used in previous steps. |
| 152 | +- `uploadId`: Upload ID acquired with `createSnapshotUpload` in the previous step. |
| 153 | +- `operatingSystem`: The operating system used to take the snapshot. Strongly advised to be filled in. Available options: `ANDROID`, `IOS`, `LINUX`, `MACOS`, `WINDOWS`. |
| 154 | +- `operatingSystemVersion`: The operating system version. e.g. "14.5" for `MACOS` or "11" for `WINDOWS`. |
| 155 | +- `browser`: The browser used to take the snapshot. Strongly advised to be filled in (if available). Available options: `CHROME`, `EDGE`, `FIREFOX`, `PLAYWRIGHT_WEBKIT`, `SAFARI`. |
| 156 | +- `browserVersion`: The browser version. e.g. "120.0.6099.318", "128.0.2". |
| 157 | + |
| 158 | +**Expected Response:** |
| 159 | + |
| 160 | +```json |
| 161 | +{ |
| 162 | + "data": { |
| 163 | + "createSnapshot": { |
| 164 | + "id": "snapshot-id-here", |
| 165 | + "uploadId": "upload-id-here" |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +- `id`: The ID of the snapshot that has been created. |
| 172 | + |
| 173 | +### 4. Finish Build |
| 174 | + |
| 175 | +Finally, finish the build to mark it as complete. Keep in mind that this is a necessary step and the build cannot be reused after it's finished. |
| 176 | + |
| 177 | +It's also worth noting that unfinished builds will be automatically closed and set to `ERRORED` state after a certain period of time (default: 3 hours). |
| 178 | + |
| 179 | +**GraphQL Mutation:** |
| 180 | + |
| 181 | +```graphql |
| 182 | +mutation { |
| 183 | + finishBuild(input: {uuid: "build-id-here"}) { |
| 184 | + id |
| 185 | + status |
| 186 | + url |
| 187 | + } |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +**Expected Response:** |
| 192 | + |
| 193 | +```json |
| 194 | +{ |
| 195 | + "data": { |
| 196 | + "finishBuild": { |
| 197 | + "id": "build-id-here", |
| 198 | + "status": "UNAPPROVED", |
| 199 | + "url": "https://app.saucelabs.com/visual/builds/build-id-here" |
| 200 | + } |
| 201 | + } |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +## Additional Information |
| 206 | + |
| 207 | +For more detailed information about the available queries and mutations, refer to our [GraphQL API documentation](https://api.us-west-1.saucelabs.com/v1/visual/graphql). |
0 commit comments