Skip to content

Commit

Permalink
adding script for json schema validation test
Browse files Browse the repository at this point in the history
  • Loading branch information
mishal.alexander committed Jan 5, 2024
1 parent 72777c7 commit 7baa49c
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 6 deletions.
69 changes: 69 additions & 0 deletions cypress/e2e/api-tests/simpleJSONSchemaValidationTest.cy.js
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.
2 changes: 1 addition & 1 deletion cypress/reports/html/index.html
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="{&quot;stats&quot;:{&quot;suites&quot;:2,&quot;tests&quot;:2,&quot;passes&quot;:2,&quot;pending&quot;:0,&quot;failures&quot;:0,&quot;testsRegistered&quot;:2,&quot;passPercent&quot;:100,&quot;pendingPercent&quot;:0,&quot;other&quot;:0,&quot;hasOther&quot;:false,&quot;skipped&quot;:0,&quot;hasSkipped&quot;:false,&quot;start&quot;:&quot;2024-01-05T13:32:41.686Z&quot;,&quot;end&quot;:&quot;2024-01-05T13:32:55.208Z&quot;,&quot;duration&quot;:13522},&quot;results&quot;:[{&quot;uuid&quot;:&quot;94233c28-e0b5-47e7-9a7d-f50d5949e0e8&quot;,&quot;title&quot;:&quot;&quot;,&quot;fullFile&quot;:&quot;cypress\\e2e\\tests\\registerTest.cy.js&quot;,&quot;file&quot;:&quot;cypress\\e2e\\tests\\registerTest.cy.js&quot;,&quot;beforeHooks&quot;:[],&quot;afterHooks&quot;:[],&quot;tests&quot;:[],&quot;suites&quot;:[{&quot;uuid&quot;:&quot;8a6a836a-9cf2-4c7c-92c1-0cf1544605db&quot;,&quot;title&quot;:&quot;Cypress Test Automation Suite&quot;,&quot;fullFile&quot;:&quot;cypress\\e2e\\tests\\registerTest.cy.js&quot;,&quot;file&quot;:&quot;cypress\\e2e\\tests\\registerTest.cy.js&quot;,&quot;beforeHooks&quot;:[],&quot;afterHooks&quot;:[],&quot;tests&quot;:[{&quot;title&quot;:&quot;register a new user&quot;,&quot;fullTitle&quot;:&quot;Cypress Test Automation Suite register a new user&quot;,&quot;timedOut&quot;:null,&quot;duration&quot;:4866,&quot;state&quot;:&quot;passed&quot;,&quot;speed&quot;:&quot;fast&quot;,&quot;pass&quot;:true,&quot;fail&quot;:false,&quot;pending&quot;:false,&quot;context&quot;:null,&quot;code&quot;:&quot;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();&quot;,&quot;err&quot;:{},&quot;uuid&quot;:&quot;c9437348-2277-4c7d-9a0b-b3433330f3d7&quot;,&quot;parentUUID&quot;:&quot;8a6a836a-9cf2-4c7c-92c1-0cf1544605db&quot;,&quot;isHook&quot;:false,&quot;skipped&quot;:false}],&quot;suites&quot;:[],&quot;passes&quot;:[&quot;c9437348-2277-4c7d-9a0b-b3433330f3d7&quot;],&quot;failures&quot;:[],&quot;pending&quot;:[],&quot;skipped&quot;:[],&quot;duration&quot;:4866,&quot;root&quot;:false,&quot;rootEmpty&quot;:false,&quot;_timeout&quot;:2000}],&quot;passes&quot;:[],&quot;failures&quot;:[],&quot;pending&quot;:[],&quot;skipped&quot;:[],&quot;duration&quot;:0,&quot;root&quot;:true,&quot;rootEmpty&quot;:true,&quot;_timeout&quot;:2000},{&quot;uuid&quot;:&quot;f7712ec1-3a02-46b2-9af0-888e80164115&quot;,&quot;title&quot;:&quot;&quot;,&quot;fullFile&quot;:&quot;cypress\\e2e\\tests\\addToCartTest.cy.js&quot;,&quot;file&quot;:&quot;cypress\\e2e\\tests\\addToCartTest.cy.js&quot;,&quot;beforeHooks&quot;:[],&quot;afterHooks&quot;:[],&quot;tests&quot;:[],&quot;suites&quot;:[{&quot;uuid&quot;:&quot;574fe714-8aa5-4138-9e8d-c5cfeca17e24&quot;,&quot;title&quot;:&quot; test automation&quot;,&quot;fullFile&quot;:&quot;cypress\\e2e\\tests\\addToCartTest.cy.js&quot;,&quot;file&quot;:&quot;cypress\\e2e\\tests\\addToCartTest.cy.js&quot;,&quot;beforeHooks&quot;:[],&quot;afterHooks&quot;:[],&quot;tests&quot;:[{&quot;title&quot;:&quot;Add To Cart flow&quot;,&quot;fullTitle&quot;:&quot; test automation Add To Cart flow&quot;,&quot;timedOut&quot;:null,&quot;duration&quot;:6751,&quot;state&quot;:&quot;passed&quot;,&quot;speed&quot;:&quot;medium&quot;,&quot;pass&quot;:true,&quot;fail&quot;:false,&quot;pending&quot;:false,&quot;context&quot;:null,&quot;code&quot;:&quot;homePageObj.searchProduct(_testData.default.product.productName);\nhomePageObj.addToCart();\nhomePageObj.verifySucessMessage().should(&#x27;contain&#x27;, _testData.default.message.successMessage).and(&#x27;contain&#x27;, _testData.default.product.productName);&quot;,&quot;err&quot;:{},&quot;uuid&quot;:&quot;4309ac62-e043-4869-8c83-6ec0b314be85&quot;,&quot;parentUUID&quot;:&quot;574fe714-8aa5-4138-9e8d-c5cfeca17e24&quot;,&quot;isHook&quot;:false,&quot;skipped&quot;:false}],&quot;suites&quot;:[],&quot;passes&quot;:[&quot;4309ac62-e043-4869-8c83-6ec0b314be85&quot;],&quot;failures&quot;:[],&quot;pending&quot;:[],&quot;skipped&quot;:[],&quot;duration&quot;:6751,&quot;root&quot;:false,&quot;rootEmpty&quot;:false,&quot;_timeout&quot;:2000}],&quot;passes&quot;:[],&quot;failures&quot;:[],&quot;pending&quot;:[],&quot;skipped&quot;:[],&quot;duration&quot;:0,&quot;root&quot;:true,&quot;rootEmpty&quot;:true,&quot;_timeout&quot;:2000}],&quot;meta&quot;:{&quot;mocha&quot;:{&quot;version&quot;:&quot;7.0.1&quot;},&quot;mochawesome&quot;:{&quot;options&quot;:{&quot;quiet&quot;:false,&quot;reportFilename&quot;:&quot;mochawesome&quot;,&quot;saveHtml&quot;:false,&quot;saveJson&quot;:true,&quot;consoleReporter&quot;:&quot;spec&quot;,&quot;useInlineDiffs&quot;:false,&quot;code&quot;:true},&quot;version&quot;:&quot;7.1.3&quot;},&quot;marge&quot;:{&quot;options&quot;:{&quot;overwrite&quot;:false,&quot;html&quot;:false,&quot;json&quot;:true,&quot;reportDir&quot;:&quot;cypress\\reports\\html\\.jsons&quot;},&quot;version&quot;:&quot;6.2.0&quot;}}}" data-config="{&quot;reportFilename&quot;:&quot;index.html&quot;,&quot;reportDir&quot;:&quot;C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html&quot;,&quot;reportTitle&quot;:&quot;cypress-e2e-framework&quot;,&quot;reportPageTitle&quot;:&quot;Mochawesome Report&quot;,&quot;inline&quot;:false,&quot;inlineAssets&quot;:false,&quot;cdn&quot;:false,&quot;charts&quot;:false,&quot;enableCharts&quot;:false,&quot;code&quot;:true,&quot;enableCode&quot;:true,&quot;autoOpen&quot;:false,&quot;overwrite&quot;:true,&quot;timestamp&quot;:false,&quot;ts&quot;:false,&quot;showPassed&quot;:true,&quot;showFailed&quot;:true,&quot;showPending&quot;:true,&quot;showSkipped&quot;:false,&quot;showHooks&quot;:&quot;failed&quot;,&quot;saveJson&quot;:false,&quot;saveHtml&quot;:true,&quot;dev&quot;:false,&quot;assetsDir&quot;:&quot;C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html\\assets&quot;,&quot;htmlFile&quot;:&quot;C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html\\index.html&quot;}"><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="{&quot;stats&quot;:{&quot;suites&quot;:1,&quot;tests&quot;:1,&quot;passes&quot;:1,&quot;pending&quot;:0,&quot;failures&quot;:0,&quot;testsRegistered&quot;:1,&quot;passPercent&quot;:100,&quot;pendingPercent&quot;:0,&quot;other&quot;:0,&quot;hasOther&quot;:false,&quot;skipped&quot;:0,&quot;hasSkipped&quot;:false,&quot;start&quot;:&quot;2024-01-05T13:52:04.811Z&quot;,&quot;end&quot;:&quot;2024-01-05T13:52:05.533Z&quot;,&quot;duration&quot;:722},&quot;results&quot;:[{&quot;uuid&quot;:&quot;6426cb80-2e3f-4c4e-96a2-6c766f63ba09&quot;,&quot;title&quot;:&quot;&quot;,&quot;fullFile&quot;:&quot;cypress\\e2e\\api-tests\\simpleJSONSchemaValidationTest.cy.js&quot;,&quot;file&quot;:&quot;cypress\\e2e\\api-tests\\simpleJSONSchemaValidationTest.cy.js&quot;,&quot;beforeHooks&quot;:[],&quot;afterHooks&quot;:[],&quot;tests&quot;:[],&quot;suites&quot;:[{&quot;uuid&quot;:&quot;fc132e6a-2a8c-4470-a9b0-553881e3be8a&quot;,&quot;title&quot;:&quot;simple JSON schema validation test&quot;,&quot;fullFile&quot;:&quot;cypress\\e2e\\api-tests\\simpleJSONSchemaValidationTest.cy.js&quot;,&quot;file&quot;:&quot;cypress\\e2e\\api-tests\\simpleJSONSchemaValidationTest.cy.js&quot;,&quot;beforeHooks&quot;:[],&quot;afterHooks&quot;:[],&quot;tests&quot;:[{&quot;title&quot;:&quot;Schema validation against response&quot;,&quot;fullTitle&quot;:&quot;simple JSON schema validation test Schema validation against response&quot;,&quot;timedOut&quot;:null,&quot;duration&quot;:690,&quot;state&quot;:&quot;passed&quot;,&quot;speed&quot;:&quot;fast&quot;,&quot;pass&quot;:true,&quot;fail&quot;:false,&quot;pending&quot;:false,&quot;context&quot;:null,&quot;code&quot;:&quot;cy.request({\n method: &#x27;GET&#x27;,\n url: &#x27;https://fakestoreapi.com/products&#x27;\n}).then(response =&gt; {\n const schema = {\n \&quot;$schema\&quot;: \&quot;http://json-schema.org/draft-07/schema#\&quot;,\n \&quot;title\&quot;: \&quot;Generated schema for Root\&quot;,\n \&quot;type\&quot;: \&quot;array\&quot;,\n \&quot;items\&quot;: {\n \&quot;type\&quot;: \&quot;object\&quot;,\n \&quot;properties\&quot;: {\n \&quot;id\&quot;: {\n \&quot;type\&quot;: \&quot;number\&quot;\n },\n \&quot;title\&quot;: {\n \&quot;type\&quot;: \&quot;string\&quot;\n },\n \&quot;price\&quot;: {\n \&quot;type\&quot;: \&quot;number\&quot;\n },\n \&quot;description\&quot;: {\n \&quot;type\&quot;: \&quot;string\&quot;\n },\n \&quot;category\&quot;: {\n \&quot;type\&quot;: \&quot;string\&quot;\n },\n \&quot;image\&quot;: {\n \&quot;type\&quot;: \&quot;string\&quot;\n },\n \&quot;rating\&quot;: {\n \&quot;type\&quot;: \&quot;object\&quot;,\n \&quot;properties\&quot;: {\n \&quot;rate\&quot;: {\n \&quot;type\&quot;: \&quot;number\&quot;\n },\n \&quot;count\&quot;: {\n \&quot;type\&quot;: \&quot;number\&quot;\n }\n },\n \&quot;required\&quot;: [\&quot;rate\&quot;, \&quot;count\&quot;]\n }\n },\n \&quot;required\&quot;: [\&quot;id\&quot;, \&quot;title\&quot;, \&quot;price\&quot;, \&quot;description\&quot;, \&quot;category\&quot;, \&quot;image\&quot;, \&quot;rating\&quot;]\n }\n };\n const validateSchema = ajv.compile(schema);\n const isSchemaValid = validateSchema(response.body);\n expect(isSchemaValid).to.be.true;\n});&quot;,&quot;err&quot;:{},&quot;uuid&quot;:&quot;33118bc3-f72a-4af4-afbc-4e46484c51e7&quot;,&quot;parentUUID&quot;:&quot;fc132e6a-2a8c-4470-a9b0-553881e3be8a&quot;,&quot;isHook&quot;:false,&quot;skipped&quot;:false}],&quot;suites&quot;:[],&quot;passes&quot;:[&quot;33118bc3-f72a-4af4-afbc-4e46484c51e7&quot;],&quot;failures&quot;:[],&quot;pending&quot;:[],&quot;skipped&quot;:[],&quot;duration&quot;:690,&quot;root&quot;:false,&quot;rootEmpty&quot;:false,&quot;_timeout&quot;:2000}],&quot;passes&quot;:[],&quot;failures&quot;:[],&quot;pending&quot;:[],&quot;skipped&quot;:[],&quot;duration&quot;:0,&quot;root&quot;:true,&quot;rootEmpty&quot;:true,&quot;_timeout&quot;:2000}],&quot;meta&quot;:{&quot;mocha&quot;:{&quot;version&quot;:&quot;7.0.1&quot;},&quot;mochawesome&quot;:{&quot;options&quot;:{&quot;quiet&quot;:false,&quot;reportFilename&quot;:&quot;mochawesome&quot;,&quot;saveHtml&quot;:false,&quot;saveJson&quot;:true,&quot;consoleReporter&quot;:&quot;spec&quot;,&quot;useInlineDiffs&quot;:false,&quot;code&quot;:true},&quot;version&quot;:&quot;7.1.3&quot;},&quot;marge&quot;:{&quot;options&quot;:{&quot;overwrite&quot;:false,&quot;html&quot;:false,&quot;json&quot;:true,&quot;reportDir&quot;:&quot;cypress\\reports\\html\\.jsons&quot;},&quot;version&quot;:&quot;6.2.0&quot;}}}" data-config="{&quot;reportFilename&quot;:&quot;index.html&quot;,&quot;reportDir&quot;:&quot;C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html&quot;,&quot;reportTitle&quot;:&quot;cypress-e2e-framework&quot;,&quot;reportPageTitle&quot;:&quot;Mochawesome Report&quot;,&quot;inline&quot;:false,&quot;inlineAssets&quot;:false,&quot;cdn&quot;:false,&quot;charts&quot;:false,&quot;enableCharts&quot;:false,&quot;code&quot;:true,&quot;enableCode&quot;:true,&quot;autoOpen&quot;:false,&quot;overwrite&quot;:true,&quot;timestamp&quot;:false,&quot;ts&quot;:false,&quot;showPassed&quot;:true,&quot;showFailed&quot;:true,&quot;showPending&quot;:true,&quot;showSkipped&quot;:false,&quot;showHooks&quot;:&quot;failed&quot;,&quot;saveJson&quot;:false,&quot;saveHtml&quot;:true,&quot;dev&quot;:false,&quot;assetsDir&quot;:&quot;C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html\\assets&quot;,&quot;htmlFile&quot;:&quot;C:\\Users\\Mishal\\Desktop\\cypress-e2e-framework\\cypress\\reports\\html\\index.html&quot;}"><div id="report"></div><script src="assets\app.js"></script></body></html>
47 changes: 47 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down

0 comments on commit 7baa49c

Please sign in to comment.