From 7baa49cec7ece3c9374f80de1c5c9c259cae291c Mon Sep 17 00:00:00 2001 From: "mishal.alexander" Date: Fri, 5 Jan 2024 19:23:25 +0530 Subject: [PATCH] adding script for json schema validation test --- .../simpleJSONSchemaValidationTest.cy.js | 69 +++++++++++++++++++ .../{tests => ui-tests}/addToCartTest.cy.js | 0 .../{tests => ui-tests}/registerTest.cy.js | 0 cypress/reports/html/index.html | 2 +- package-lock.json | 47 +++++++++++++ package.json | 7 +- 6 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 cypress/e2e/api-tests/simpleJSONSchemaValidationTest.cy.js rename cypress/e2e/{tests => ui-tests}/addToCartTest.cy.js (100%) rename cypress/e2e/{tests => ui-tests}/registerTest.cy.js (100%) diff --git a/cypress/e2e/api-tests/simpleJSONSchemaValidationTest.cy.js b/cypress/e2e/api-tests/simpleJSONSchemaValidationTest.cy.js new file mode 100644 index 0000000..c9fd485 --- /dev/null +++ b/cypress/e2e/api-tests/simpleJSONSchemaValidationTest.cy.js @@ -0,0 +1,69 @@ +// install ajv library and instantiate it +const Ajv = require('ajv'); +const ajv = new Ajv() +// ajv.compile() can validate whether the json is as specified in the json schema +describe('simple JSON schema validation test', () => { + it('Schema validation against response', () => { + cy.request({ + method: 'GET', + url: 'https://fakestoreapi.com/products' + }) + .then((response) => { + const schema = { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Generated schema for Root", + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "number" + }, + "title": { + "type": "string" + }, + "price": { + "type": "number" + }, + "description": { + "type": "string" + }, + "category": { + "type": "string" + }, + "image": { + "type": "string" + }, + "rating": { + "type": "object", + "properties": { + "rate": { + "type": "number" + }, + "count": { + "type": "number" + } + }, + "required": [ + "rate", + "count" + ] + } + }, + "required": [ + "id", + "title", + "price", + "description", + "category", + "image", + "rating" + ] + } + } + const validateSchema = ajv.compile(schema) + const isSchemaValid = validateSchema(response.body) + expect(isSchemaValid).to.be.true; + }) + }) +}) \ No newline at end of file diff --git a/cypress/e2e/tests/addToCartTest.cy.js b/cypress/e2e/ui-tests/addToCartTest.cy.js similarity index 100% rename from cypress/e2e/tests/addToCartTest.cy.js rename to cypress/e2e/ui-tests/addToCartTest.cy.js diff --git a/cypress/e2e/tests/registerTest.cy.js b/cypress/e2e/ui-tests/registerTest.cy.js similarity index 100% rename from cypress/e2e/tests/registerTest.cy.js rename to cypress/e2e/ui-tests/registerTest.cy.js diff --git a/cypress/reports/html/index.html b/cypress/reports/html/index.html index 20c05c8..7f3b0ea 100644 --- a/cypress/reports/html/index.html +++ b/cypress/reports/html/index.html @@ -1,2 +1,2 @@ -Mochawesome Report
\ No newline at end of file +Mochawesome Report
\ No newline at end of file diff --git a/package-lock.json b/package-lock.json index a481bb6..98dcb5d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "devDependencies": { + "ajv": "^8.12.0", "cypress": "^13.6.2", "cypress-mochawesome-reporter": "^3.8.0", "npm": "^10.2.5" @@ -116,6 +117,22 @@ "node": ">=8" } }, + "node_modules/ajv": { + "version": "8.12.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.12.0.tgz", + "integrity": "sha512-sRu1kpcO9yLtYxBKvqfTeh9KzZEwO3STyX1HT+4CaDzC6HpTGYhIhPIzj9XuKU7KYDwnaeh5hcOwjy1QuJzBPA==", + "dev": true, + "dependencies": { + "fast-deep-equal": "^3.1.1", + "json-schema-traverse": "^1.0.0", + "require-from-string": "^2.0.2", + "uri-js": "^4.2.2" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/epoberezkin" + } + }, "node_modules/ansi-colors": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.3.tgz", @@ -987,6 +1004,12 @@ "node >=0.6.0" ] }, + "node_modules/fast-deep-equal": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", + "dev": true + }, "node_modules/fd-slicer": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", @@ -1567,6 +1590,12 @@ "integrity": "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA==", "dev": true }, + "node_modules/json-schema-traverse": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", + "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==", + "dev": true + }, "node_modules/json-stringify-safe": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz", @@ -5559,6 +5588,15 @@ "node": ">=0.10.0" } }, + "node_modules/require-from-string": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", + "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/require-main-filename": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/require-main-filename/-/require-main-filename-2.0.0.tgz", @@ -5968,6 +6006,15 @@ "node": ">=8" } }, + "node_modules/uri-js": { + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", + "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", + "dev": true, + "dependencies": { + "punycode": "^2.1.0" + } + }, "node_modules/url-parse": { "version": "1.5.10", "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.10.tgz", diff --git a/package.json b/package.json index 5fc1105..97c1d4f 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,8 @@ "description": "Sample framework created using Cypress", "main": "index.js", "scripts": { - "test:addToCart": "npx cypress run --spec cypress/e2e/tests/addToCartTest.cy.js", - "test:registerTest": "npx cypress run --spec cypress/e2e/tests/registerTest.cy.js", - "test:simpleApiTest": "npx cypress run --spec cypress/e2e/api-tests/simpleApiTest.cy.js", - "test:bearertokentest": "npx cypress run --spec cypress/e2e/api-tests/simpleBearerTokenTests.cy.js", "test:apiTests": "npx cypress run --spec cypress/e2e/api-tests/", - "test:uiTests": "npx cypress run --spec cypress/e2e/tests/", + "test:uiTests": "npx cypress run --spec cypress/e2e/ui-tests/", "test:all": "npx cypress run" }, "author": "Mishal Alexander", @@ -23,6 +19,7 @@ }, "homepage": "https://github.com/mishalalex/cypress-e2e-framework/#readme", "devDependencies": { + "ajv": "^8.12.0", "cypress": "^13.6.2", "cypress-mochawesome-reporter": "^3.8.0", "npm": "^10.2.5"