0

I am new to Playwright. I am writing API tests in TypeScript where my API response is like below:

{
  "id" : "abc123",
  "appCode" : "09000007",
  "applicationReference" : "ABCDEF",
  "datetimeSubmitted" : "2023-03-09T17:56:28.912876Z",
  "firstName" : "ABC",
  "surname" : "DEF",
  "lastAddress" : {
    "street" : "9779 Pat Loaf",
    "property": "2"
  }
}

How do I validate the JSONn schema in the response JSON using TypeScript in Playwright?

For example, I want to test if the response JSON contains:

id, appCode, applicationReference, datetimeSubmitted, firstname, surname, lastAddress.street, lastAddress.property

Is there any way to achieve such type of test in Playwright or any 3rd party plugin?

Any help is much appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sweety
  • 117
  • 6
  • 18
  • Sure, there's a way. Have you tried implementing anything? You might start with `waitForRepsonse` if you're not sure where to begin. – ggorlen Mar 18 '23 at 22:38
  • @ggorlen Can you please elaborate on waitForRepsonse please? How do I use it to validate the schema? – Sweety Mar 20 '23 at 11:41
  • It's used to capture the response. Once you capture the response, validation is easy using normal assertions, `expect(body.id).toBe("abc123")` etc. – ggorlen Mar 20 '23 at 15:39

2 Answers2

0

Have you tried using JSON Schema? The goal of the DL is help to annotate and validate JSON documents. There are many implementations of the specification, this might be useful for what you're trying to do.

Rafael Amaral
  • 36
  • 1
  • 4
0

Maybe a late answer, but you can use "chai-json-schema" npm package and import it into your test file to validate the schema:

const { chai } = require('chai')
import { expect as expectChai } from 'chai'
chai.use(require('chai-json-schema'))

inside your test block:

expectChai(response).to.be.jsonSchema(uploadResponseSchema);