A Beginner's Guide to Writing End-to-End Tests with TestCafe
Tags: endtoendtesting, webdev, testing, javascript
Published: 2020-03-04 18:12:30

---
title: A Beginner's Guide to Writing End-to-End Tests with TestCafe
published: true
description: A step-by-step beginner's guide to setting up and writing end-to-end test cases in TestCafe.
tags: end-to-end testing, webdev, testing, javascript
canonical_url: https://awkwardblackcoder.com/2020/03/04/a-beginners-guide-to-writing-end-to-end-e2e-tests-with-testcafe/
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/dlfyks6oyg0x1ycwr2lq.png
---

Lately, with the planning of a new application launch, I'm learning all the aspects needed to design/develop an application, which isn't just coding and styling it. Yes, your application may look great when you're done, but can you imagine what will happen if any one component of a major system fails or if an issue goes undetected? This is where the importance of End-to-End testing(in addition to a series of other tests, of course) comes in. 

<h2>What is End-to-End testing?</h2>

End-to-end testing is a method used to check whether your application is behaving exactly how it's supposed to as a whole from start to finish. Through this technique, you are testing all of the important functionalities of your product, such as how well it is communicating with other databases, networks, and applications. 

<hr>

<p>In this post, I will go over the following:</p>
  
<ul><li>What is TestCafe</li>
  
<li>How to Install TestCafe</li>
  
<li>Writing and running your first test script</li></ul>

<hr>


  <h2>What is TestCafe?</h2>

<p>TestCafe is a new, open-source, Node.js-based end-to-end testing tool used to automate testing for web apps. It takes care of all testing stages: starting browsers, running tests, gathering test results and generating reports. Using TestCafe for end-to-end testing has many advantages: 

<ol>
<li>It is easy to install</li>
<li>It all supports all browsers and their versions</li>
<li>It doesn't need any plugins or has any dependencies; it works <em>right out of the box</em>, without you having to install any other software or web drivers.</li>

</ol>
</p>

<hr>

<h2>Installing TestCafe </h2>

<ul>
<li>First things first, because TestCafe is JavaScript/Node.js-based, you need to install node.js to your system. If you already have it installed, you should be able to install packages using the npm command.</li>

<li>Now, all you have to do to install testcafe is to run this command globally in your terminal:

<img src="https://dev-to-uploads.s3.amazonaws.com/i/j9vfb88vh1mmntlph9kq.png">

</li>
</ul>



<hr>

<h2>Writing Tests</h2>

<p>To practice writing tests, we will be using TestCafe demo page.

</p>

<ul>

<li>Create a directory to house your test files. I'll name mine 'TestCafe-Example'.</li>

<li>Open a code editor of your choice and open up your newly created folder. (I use VSCode as my editor) </li>

<li>Create a new file called 'app.test.js' in your directory.</li>
<hr>
<br>
<p>Now that you have your test file created, time to add in the code.</p>

</ul>

<ol>
<li><strong>Import the testcafe module<strong>

<img src="https://dev-to-uploads.s3.amazonaws.com/i/1xb9vpcq7lucx1q97z2j.png">
</li>
   
<li>
<strong>Create a fixture. </strong>
   
<p>A fixture is a category of tests used to make our tests cleaner and readable. A test suite can contain one or more fixtures. To declare a test fixture, use the fixture function:</p>
<sub style="list-style: none;">fixture(fixtureName) <em>or</em> fixture `fixtureName`</sub>

<img src="https://dev-to-uploads.s3.amazonaws.com/i/dizvfycgqx0svvwzq0hz.png">
<sub>Note: The fixture declaration can be used to specify the target webpage</sub>
</li>
<br>

<li><strong>Add a test.</strong>
   
	<p>To initiate a test, call the test function and pass the test code inside it. </p>
<p><sub>test(testName, fn(t))</sub></p>

<img src="https://dev-to-uploads.s3.amazonaws.com/i/wkwlu4a8vauvlcrpz8ak.png">

<p>In the above code sample, the test will type text into the 'Developer Name' input element and click the Submit button. The submit button will redirect you to a page that says 'Thank You, %username%!'(Feel free to change the text in the input element to whatever you like!)</p>

<p>The <strong>'t'</strong> object represents the test controller used to access the test run API's methods. You can use the test controller to:</p>

<ul>	
<li>call the test actions</li>
<li>handle browser dialog</li>
<li>use the wait function, and</li>
<li>execute assertions.</li>
</ul>
</li>
</ol>


To check that the text on the page contains the right name, we'll create a selector that locates the article header and adds an assertion that checks that the text says 'Thank you, Phoebe M.' (or whatever name you replaced mine with 😊)

<img src="https://dev-to-uploads.s3.amazonaws.com/i/n1w9o2xsyn9g0x6dgvtk.png">

<p>Now to test it! If running on a local machine, simply run this command:</p>

<img src="https://dev-to-uploads.s3.amazonaws.com/i/mqdjqv9zcmy7nm1umxx0.png">

<p>With this command, TestCafe will find and launch Google Chrome to run the test. Of course, if you're using a different browser or you named your test file something differently, feel free to update the command accordingly. </p>

<p><strong>Note:</strong> To see a list of browsers that TestCafe detects on your machine, run the following command:</p>

<img src="https://dev-to-uploads.s3.amazonaws.com/i/y6500cauz9c3nvrg6qnv.png">

<p>And that's it! The simplest way to write and run an end to end test script. </p>

<hr>

<h3><u>Resources</ul></h3>
<a href="https://www.tutorialspoint.com/software_testing_dictionary/end_to_end_testing.htm">TutorialsPoint - End-to-End Testing</a>
<a href="https://testcafe.devexpress.com/Documentation/Getting_Started/Getting_Started_Guide/#Setting_up_a_test_project">TestCafe - Getting Started Guide</a>
<a href="https://www.sitepoint.com/testcafe-easier-end-end-web-app-testing-node-js/">SitePoint</a>
<a href="https://devexpress.github.io/testcafe/documentation/test-api/test-code-structure.html">DevExpress - TestCafe Documentation</a>