{"componentChunkName":"component---src-templates-tag-js","path":"/tags/snapshot-testing/","result":{"data":{"site":{"siteMetadata":{"title":"LoginRadius Blog"}},"allMarkdownRemark":{"totalCount":1,"edges":[{"node":{"fields":{"slug":"/engineering/snapshot-testing-using-nightwatch-and-mocha/"},"html":"<h3 id=\"snapshot-testing\" style=\"position:relative;\"><a href=\"#snapshot-testing\" aria-label=\"snapshot testing permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Snapshot Testing</h3>\n<p><strong>Snapshot tests</strong> as the name implies, is a very powerful tool to test whether you the UI has change or not. A typical <strong>snapshot test</strong> case for a website/mobile app renders a UI component, takes a <strong>snapshot</strong>, then compares it to a reference <strong>snapshot</strong> file stored alongside the <strong>test</strong>.</p>\n<h3 id=\"snapshot-testing--benefits\" style=\"position:relative;\"><a href=\"#snapshot-testing--benefits\" aria-label=\"snapshot testing  benefits permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Snapshot Testing  Benefits</h3>\n<h4 id=\"for-qa-manual-and-automation\" style=\"position:relative;\"><a href=\"#for-qa-manual-and-automation\" aria-label=\"for qa manual and automation permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>For QA Manual And Automation</h4>\n<ul>\n<li>Easy to identify any change in the DOM element.</li>\n<li>Help to automation to check element id as same as previous.</li>\n<li>Help to check integration testing will be in right.</li>\n</ul>\n<h4 id=\"for-developer-unit-testing\" style=\"position:relative;\"><a href=\"#for-developer-unit-testing\" aria-label=\"for developer unit testing permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>For Developer Unit Testing</h4>\n<ul>\n<li>The developer can compare snapshot dom on every movement when the dynamic change happened on DOM.</li>\n<li>Get changes in DOM and update QA for update automation testing products.</li>\n</ul>\n<p>Need to install <strong>NPM</strong></p>\n<ol>\n<li>npm i mocha</li>\n<li>npm i clean-html</li>\n<li>npm i snap-shot</li>\n<li>npm i jsdom</li>\n<li>npm i jsdom-global</li>\n</ol>\n<blockquote>\n<p> <strong>NightWatch</strong> does not have snapshot feature. So, We will use <strong>mocha</strong> to take snapshots. But <strong>Mocha will be run by NightWatch.</strong></p>\n</blockquote>\n<h3 id=\"directory-structure\" style=\"position:relative;\"><a href=\"#directory-structure\" aria-label=\"directory structure permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Directory Structure</h3>\n<p>As per over automation project. We have created a \"snapshot\" folder under the \"test\" folder and we will be following the same structure as per the below project menu bar. </p>\n<p><a href=\"https://cdn.filestackcontent.com/solmjUZXTPWZgTNppBmW\"><img src=\"https://cdn.filestackcontent.com/solmjUZXTPWZgTNppBmW\" alt=\"N|Solid\"></a></p>\n<p>We also need to add Mocha test files, which will use to take snapshot and store under the root folder \"__snapshots__\".</p>\n<p><img src=\"https://cdn.filestackcontent.com/XGkI0wDrQoGDSp2djINg\" alt=\"(https://cdn.filestackcontent.com/XGkI0wDrQoGDSp2djINg)\"></p>\n<p>If you are running automation code via visual code editor, then you can setup a launch.json file which helps you to debug your test code with all file/individual file.</p>\n<p>You can see the settings below of the launch.json file.</p>\n<p><img src=\"https://cdn.filestackcontent.com/qDjAHwHIQtKp2hmBdaEn\" alt=\"(https://cdn.filestackcontent.com/qDjAHwHIQtKp2hmBdaEn)\"></p>\n<hr>\n<h4 id=\"update-snapshot\" style=\"position:relative;\"><a href=\"#update-snapshot\" aria-label=\"update snapshot permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Update Snapshot</h4>\n<p>After creating a snapshot, sometimes we need to update snapshot due to improvement, customer requirements, and any valid changes on UI. So here, we can have some other settings which  will help us.</p>\n<ol>\n<li>\n<p>If we want to update all snapshots by single command then we need to follow below instruction.</p>\n<p>Create a root folder file(update_snapshot.js) and paste code on it.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">(</span><span class=\"mtk4\">function</span><span class=\"mtk1\">(){</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      </span><span class=\"mtk12\">process</span><span class=\"mtk1\">.</span><span class=\"mtk12\">env</span><span class=\"mtk1\">.</span><span class=\"mtk12\">UPDATE</span><span class=\"mtk1\">\\=</span><span class=\"mtk7\">1</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">})()</span></span></code></pre>\n<p>Add in package.json</p>\n<p>  <img src=\"https://cdn.filestackcontent.com/TTvchMDTW6F5x87J0688\" alt=\"(https://cdn.filestackcontent.com/TTvchMDTW6F5x87J0688)\"> </p>\n</li>\n</ol>\n<blockquote>\n<p>We can update all snapshot by using → <code>npm run snapshot-u</code></p>\n</blockquote>\n<ol start=\"2\">\n<li>If we want to update the single snapshot then we can use the above technique but we just need to update the file name against \"test/snapshot\".</li>\n<li>\n<p>If we use vscode, then we need to update launch.json as per above information </p>\n<p>  <img src=\"https://cdn.filestackcontent.com/uWX0pIUaSzKFyVKJGIYB\" alt=\"(https://cdn.filestackcontent.com/uWX0pIUaSzKFyVKJGIYB)\"></p>\n<p>  and need to create a file(\"snapshot.config.js\") in the root folder and paste the below code.</p>\n<p>  <img src=\"https://cdn.filestackcontent.com/Hh3PNxKmSo2pSFRA9YeM\" alt=\"(https://cdn.filestackcontent.com/Hh3PNxKmSo2pSFRA9YeM)\"></p>\n</li>\n</ol>\n<blockquote>\n<p>Important-: If we want to run only a snapshot test then we will need a small change in the nightwatch.json file.</p>\n</blockquote>\n<p><img src=\"https://cdn.filestackcontent.com/EkBrPZffRv2ElZ0Qez36\" alt=\"(https://cdn.filestackcontent.com/EkBrPZffRv2ElZ0Qez36)\"></p>\n<p>You can find the complete reposrtory link <a href=\"https://github.com/niteshjain1987/NightWatch-Snapshot\">here</a></p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n</style>","frontmatter":{"date":"July 29, 2020","updated_date":null,"title":"Snapshot testing using Nightwatch and mocha","tags":["QA","Nightwatch","snapshot-testing"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.7699115044247788,"src":"/static/858d55e4f691f5e5780be7675877f12d/58556/snapshot_nightwatch.webp","srcSet":"/static/858d55e4f691f5e5780be7675877f12d/61e93/snapshot_nightwatch.webp 200w,\n/static/858d55e4f691f5e5780be7675877f12d/1f5c5/snapshot_nightwatch.webp 400w,\n/static/858d55e4f691f5e5780be7675877f12d/58556/snapshot_nightwatch.webp 800w,\n/static/858d55e4f691f5e5780be7675877f12d/99238/snapshot_nightwatch.webp 1200w,\n/static/858d55e4f691f5e5780be7675877f12d/135cd/snapshot_nightwatch.webp 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Nitesh Jain","github":"niteshjain1987","avatar":null}}}}]}},"pageContext":{"tag":"snapshot-testing"}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}