-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding script for json schema validation test
- Loading branch information
mishal.alexander
committed
Jan 5, 2024
1 parent
72777c7
commit 7baa49c
Showing
6 changed files
with
119 additions
and
6 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
cypress/e2e/api-tests/simpleJSONSchemaValidationTest.cy.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}) | ||
}) | ||
}) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
<!doctype html> | ||
<html lang="en"><head><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="viewport" content="width=device-width, initial-scale=1"/><title>Mochawesome Report</title><link rel="stylesheet" href="assets\app.css"/></head><body data-raw="{"stats":{"suites":2,"tests":2,"passes":2,"pending":0,"failures":0,"testsRegistered":2,"passPercent":100,"pendingPercent":0,"other":0,"hasOther":false,"skipped":0,"hasSkipped":false,"start":"2024-01-05T13:32:41.686Z","end":"2024-01-05T13:32:55.208Z","duration":13522},"results":[{"uuid":"94233c28-e0b5-47e7-9a7d-f50d5949e0e8","title":"","fullFile":"cypress\\e2e\\tests\\registerTest.cy.js","file":"cypress\\e2e\\tests\\registerTest.cy.js","beforeHooks":[],"afterHooks":[],"tests":[],"suites":[{"uuid":"8a6a836a-9cf2-4c7c-92c1-0cf1544605db","title":"Cypress Test Automation Suite","fullFile":"cypress\\e2e\\tests\\registerTest.cy.js","file":"cypress\\e2e\\tests\\registerTest.cy.js","beforeHooks":[],"afterHooks":[],"tests":[{"title":"register a new user","fullTitle":"Cypress Test Automation Suite register a new user","timedOut":null,"duration":4866,"state":"passed","speed":"fast","pass":true,"fail":false,"pending":false,"context":null,"code":"registerObject.openUrl();\nregisterObject.enterFirstName(_registerData.default.firstname);\nregisterObject.enterLastName(_registerData.default.lastName);\nregisterObject.enterEmail(_registerData.default.email);\nregisterObject.enterTelephone(_registerData.default.telephone);\nregisterObject.enterPasswordAndConfirmPassword(_registerData.default.password);\nregisterObject.checkPrivacyPolicy();\nregisterObject.clickOnContinue();","err":{},"uuid":"c9437348-2277-4c7d-9a0b-b3433330f3d7","parentUUID":"8a6a836a-9cf2-4c7c-92c1-0cf1544605db","isHook":false,"skipped":false}],"suites":[],"passes":["c9437348-2277-4c7d-9a0b-b3433330f3d7"],"failures":[],"pending":[],"skipped":[],"duration":4866,"root":false,"rootEmpty":false,"_timeout":2000}],"passes":[],"failures":[],"pending":[],"skipped":[],"duration":0,"root":true,"rootEmpty":true,"_timeout":2000},{"uuid":"f7712ec1-3a02-46b2-9af0-888e80164115","title":"","fullFile":"cypress\\e2e\\tests\\addToCartTest.cy.js","file":"cypress\\e2e\\tests\\addToCartTest.cy.js","beforeHooks":[],"afterHooks":[],"tests":[],"suites":[{"uuid":"574fe714-8aa5-4138-9e8d-c5cfeca17e24","title":" test automation","fullFile":"cypress\\e2e\\tests\\addToCartTest.cy.js","file":"cypress\\e2e\\tests\\addToCartTest.cy.js","beforeHooks":[],"afterHooks":[],"tests":[{"title":"Add To Cart flow","fullTitle":" test automation Add To Cart flow","timedOut":null,"duration":6751,"state":"passed","speed":"medium","pass":true,"fail":false,"pending":false,"context":null,"code":"homePageObj.searchProduct(_testData.default.product.productName);\nhomePageObj.addToCart();\nhomePageObj.verifySucessMessage().should('contain', _testData.default.message.successMessage).and('contain', _testData.default.product.productName);","err":{},"uuid":"4309ac62-e043-4869-8c83-6ec0b314be85","parentUUID":"574fe714-8aa5-4138-9e8d-c5cfeca17e24","isHook":false,"skipped":false}],"suites":[],"passes":["4309ac62-e043-4869-8c83-6ec0b314be85"],"failures":[],"pending":[],"skipped":[],"duration":6751,"root":false,"rootEmpty":false,"_timeout":2000}],"passes":[],"failures":[],"pending":[],"skipped":[],"duration":0,"root":true,"rootEmpty":true,"_timeout":2000}],"meta":{"mocha":{"version":"7.0.1"},"mochawesome":{"options":{"quiet":false,"reportFilename":"mochawesome","saveHtml":false,"saveJson":true,"consoleReporter":"spec","useInlineDiffs":false,"code":true},"version":"7.1.3"},"marge":{"options":{"overwrite":false,"html":false,"json":true,"reportDir":"cypress\\reports\\html\\.jsons"},"version":"6.2.0"}}}" data-config="{"reportFilename":"index.html","reportDir":"C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html","reportTitle":"cypress-e2e-framework","reportPageTitle":"Mochawesome Report","inline":false,"inlineAssets":false,"cdn":false,"charts":false,"enableCharts":false,"code":true,"enableCode":true,"autoOpen":false,"overwrite":true,"timestamp":false,"ts":false,"showPassed":true,"showFailed":true,"showPending":true,"showSkipped":false,"showHooks":"failed","saveJson":false,"saveHtml":true,"dev":false,"assetsDir":"C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html\\assets","htmlFile":"C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html\\index.html"}"><div id="report"></div><script src="assets\app.js"></script></body></html> | ||
<html lang="en"><head><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="viewport" content="width=device-width, initial-scale=1"/><title>Mochawesome Report</title><link rel="stylesheet" href="assets\app.css"/></head><body data-raw="{"stats":{"suites":1,"tests":1,"passes":1,"pending":0,"failures":0,"testsRegistered":1,"passPercent":100,"pendingPercent":0,"other":0,"hasOther":false,"skipped":0,"hasSkipped":false,"start":"2024-01-05T13:52:04.811Z","end":"2024-01-05T13:52:05.533Z","duration":722},"results":[{"uuid":"6426cb80-2e3f-4c4e-96a2-6c766f63ba09","title":"","fullFile":"cypress\\e2e\\api-tests\\simpleJSONSchemaValidationTest.cy.js","file":"cypress\\e2e\\api-tests\\simpleJSONSchemaValidationTest.cy.js","beforeHooks":[],"afterHooks":[],"tests":[],"suites":[{"uuid":"fc132e6a-2a8c-4470-a9b0-553881e3be8a","title":"simple JSON schema validation test","fullFile":"cypress\\e2e\\api-tests\\simpleJSONSchemaValidationTest.cy.js","file":"cypress\\e2e\\api-tests\\simpleJSONSchemaValidationTest.cy.js","beforeHooks":[],"afterHooks":[],"tests":[{"title":"Schema validation against response","fullTitle":"simple JSON schema validation test Schema validation against response","timedOut":null,"duration":690,"state":"passed","speed":"fast","pass":true,"fail":false,"pending":false,"context":null,"code":"cy.request({\n method: 'GET',\n url: 'https://fakestoreapi.com/products'\n}).then(response => {\n const schema = {\n \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n \"title\": \"Generated schema for Root\",\n \"type\": \"array\",\n \"items\": {\n \"type\": \"object\",\n \"properties\": {\n \"id\": {\n \"type\": \"number\"\n },\n \"title\": {\n \"type\": \"string\"\n },\n \"price\": {\n \"type\": \"number\"\n },\n \"description\": {\n \"type\": \"string\"\n },\n \"category\": {\n \"type\": \"string\"\n },\n \"image\": {\n \"type\": \"string\"\n },\n \"rating\": {\n \"type\": \"object\",\n \"properties\": {\n \"rate\": {\n \"type\": \"number\"\n },\n \"count\": {\n \"type\": \"number\"\n }\n },\n \"required\": [\"rate\", \"count\"]\n }\n },\n \"required\": [\"id\", \"title\", \"price\", \"description\", \"category\", \"image\", \"rating\"]\n }\n };\n const validateSchema = ajv.compile(schema);\n const isSchemaValid = validateSchema(response.body);\n expect(isSchemaValid).to.be.true;\n});","err":{},"uuid":"33118bc3-f72a-4af4-afbc-4e46484c51e7","parentUUID":"fc132e6a-2a8c-4470-a9b0-553881e3be8a","isHook":false,"skipped":false}],"suites":[],"passes":["33118bc3-f72a-4af4-afbc-4e46484c51e7"],"failures":[],"pending":[],"skipped":[],"duration":690,"root":false,"rootEmpty":false,"_timeout":2000}],"passes":[],"failures":[],"pending":[],"skipped":[],"duration":0,"root":true,"rootEmpty":true,"_timeout":2000}],"meta":{"mocha":{"version":"7.0.1"},"mochawesome":{"options":{"quiet":false,"reportFilename":"mochawesome","saveHtml":false,"saveJson":true,"consoleReporter":"spec","useInlineDiffs":false,"code":true},"version":"7.1.3"},"marge":{"options":{"overwrite":false,"html":false,"json":true,"reportDir":"cypress\\reports\\html\\.jsons"},"version":"6.2.0"}}}" data-config="{"reportFilename":"index.html","reportDir":"C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html","reportTitle":"cypress-e2e-framework","reportPageTitle":"Mochawesome Report","inline":false,"inlineAssets":false,"cdn":false,"charts":false,"enableCharts":false,"code":true,"enableCode":true,"autoOpen":false,"overwrite":true,"timestamp":false,"ts":false,"showPassed":true,"showFailed":true,"showPending":true,"showSkipped":false,"showHooks":"failed","saveJson":false,"saveHtml":true,"dev":false,"assetsDir":"C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html\\assets","htmlFile":"C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html\\index.html"}"><div id="report"></div><script src="assets\app.js"></script></body></html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters