<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prasad Mudedla</title>
    <description>The latest articles on DEV Community by Prasad Mudedla (@prasadmudedla).</description>
    <link>https://dev.to/prasadmudedla</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F90894%2F138ef9e6-54e8-451d-98d4-5a0aeaa0d5e8.png</url>
      <title>DEV Community: Prasad Mudedla</title>
      <link>https://dev.to/prasadmudedla</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prasadmudedla"/>
    <language>en</language>
    <item>
      <title>Easy e2e Testing with TestCafe</title>
      <dc:creator>Prasad Mudedla</dc:creator>
      <pubDate>Mon, 03 Aug 2020 11:44:29 +0000</pubDate>
      <link>https://dev.to/prasadmudedla/easy-e2e-testing-with-testcafe-4k25</link>
      <guid>https://dev.to/prasadmudedla/easy-e2e-testing-with-testcafe-4k25</guid>
      <description>&lt;p&gt;In the initial days of my career when I start to learn automation, Selenium was the wide spread tool that has some exciting features and there are lot of frameworks built on it with even more features and flexibility. When properly set up, they definitely provide us with reliable tests.&lt;/p&gt;

&lt;p&gt;However, in an ever-changing world, we aren't just utilising all these cool features.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Getting/installing drivers for browser support&lt;/li&gt;
&lt;li&gt;Creating capabilities and profiles&lt;/li&gt;
&lt;li&gt;Matching of driver and browser versions to avoid compatibility issues&lt;/li&gt;
&lt;li&gt;Grid configuration and separate launchers for running the tests in parallel&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, let's shift our focus to check the latest tools such as TestCafe to overcome these issues.&lt;/p&gt;

&lt;h1&gt;
  
  
  TestCafe
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://devexpress.github.io/testcafe/"&gt;TestCafe&lt;/a&gt; is a new, open-source, &lt;a href="https://nodejs.org/en/"&gt;Node.js&lt;/a&gt; based e2e testing framework. It's an excellent alternative to Selenium-based tools since it injects itself into the website as JS scripts, so it's more stable and faster. This allows TestCafe to run on any browser, including mobile devices and cloud services as well. It doesn't need Selenium to start your browser window, execute the tests in it and provide useful test report after the execution.&lt;/p&gt;

&lt;p&gt;One of its main perks is that it takes about a minute to setup and to start testing (and it doesn't use WebDriver).&lt;/p&gt;

&lt;p&gt;It works with most popular operating systems and browsers. Tests are written in JavaScript or TypeScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup for Testing
&lt;/h2&gt;

&lt;p&gt;First make sure you have Node.js version 4.x or higher installed. Before doing anything else, we need to create NodeJS package, i.e.just a directory containing a &lt;code&gt;package.json&lt;/code&gt; file. We can easily do that in 2 steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an empty directory.&lt;/li&gt;
&lt;li&gt;Run: &lt;code&gt;npm init&lt;/code&gt; inside the new directory.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This should ask for a bunch of information. For those interested this is driven by &lt;a href="https://github.com/npm/init-package-json"&gt;init-package-json&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iVVoQ5bf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/osmpiu2ep6v2po25thut.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iVVoQ5bf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/osmpiu2ep6v2po25thut.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this, we need to add &lt;em&gt;TestCafe&lt;/em&gt; library in the project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install testcafe --save
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Architecting
&lt;/h2&gt;

&lt;p&gt;One of the bests currently solutions to architecting a testing project is using the &lt;strong&gt;Page Objects Pattern&lt;/strong&gt;. The goal behind this pattern is to abstract any page information away from the actual tests. Ideally, you should store all selectors or specific instructions that are unique for a certain page in a page objects file so that you still can run your test after you've completely redesigned your page.&lt;/p&gt;

&lt;p&gt;The main advantage of this pattern is the &lt;strong&gt;encapsulation of elements&lt;/strong&gt;, &lt;strong&gt;methods and actions,&lt;/strong&gt;then we can reuse the code in any test and if we change the library that runs the tests, we can reuse the test structure.&lt;/p&gt;

&lt;p&gt;To support this architecture, I recommend you create a directory to save the page objects files and another to save the tests files, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tJNXBIjT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jlvrsl2m43j935j8m6rm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tJNXBIjT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jlvrsl2m43j935j8m6rm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The first test
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://devexpress.github.io/testcafe/documentation/getting-started/"&gt;TestCafe documentation&lt;/a&gt; is pretty helpful, in fact, I learnt most of the things I know by just following through the docs. &lt;br&gt;&lt;br&gt;
A very helpful method you would be making use of most of the time whilst interfacing with testcafe is the &lt;code&gt;Selector&lt;/code&gt; method. This method makes sure to select the elements within the DOM that you would love to test.&lt;/p&gt;

&lt;p&gt;The test will be opening google and get the title of the tab. So, I have created a page (page-object/googlesearchpage.js) with elements needed in the page-object folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Kcs5netX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0xm1fmndbkt8zludjsha.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Kcs5netX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0xm1fmndbkt8zludjsha.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On googlesearchpage.js, I have added an input element and a method to get title of the page. The next step is to create the test script file (tests/test.js) and import the methods from the page objects:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3duw5A5a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0aceggtmx9a1entzze6g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3duw5A5a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0aceggtmx9a1entzze6g.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now add a script — &lt;code&gt;./node_modules/.bin/testcafe chrome tests&lt;/code&gt; in the package.json file. Now the package.json will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fCzhzrd_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6xmc4ybczaj3vt6ha0dv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fCzhzrd_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6xmc4ybczaj3vt6ha0dv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Done! Now we will be able to run the test. Open the terminal and execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run test:chrome
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Extending the tests
&lt;/h2&gt;

&lt;p&gt;Now, i will try to extend the tests&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add one more method to googlesearchpage to search for a text&lt;/li&gt;
&lt;li&gt;Create a new page in the page-object for the search results page.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Run the command &lt;code&gt;npm run test:chrome&lt;/code&gt; once again in the terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DHm9HLTx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lgrqs25s4quybqwgmqxx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DHm9HLTx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lgrqs25s4quybqwgmqxx.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code for the extended tests is here: &lt;a href="https://github.com/prasadmudedla/test-cafe-project.git"&gt;https://github.com/prasadmudedla/test-cafe-project.git&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Running in other browsers
&lt;/h2&gt;

&lt;p&gt;Another important feature that &lt;em&gt;testcafe&lt;/em&gt; have is that we can run the tests in a lot of browsers. You can see the available browsers &lt;a href="https://devexpress.github.io/testcafe/documentation/using-testcafe/common-concepts/browsers/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To make the execution simpler, let's add some scripts in the package file at scripts section and run it using &lt;em&gt;npm run&lt;/em&gt; function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k967IMSc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qsea9ggzid7zfi9tjbce.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k967IMSc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/qsea9ggzid7zfi9tjbce.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
package.json&lt;/p&gt;

&lt;h2&gt;
  
  
  Extending TestCafe Functionality
&lt;/h2&gt;

&lt;p&gt;You can visit GitHub to learn more about the &lt;a href="https://github.com/DevExpress/testcafe#testcafe-ecosystem"&gt;TestCafe ecosystem&lt;/a&gt;. If you need more functionality in test code, you can use any node.js module. TestCafe also provides the extended capabilities for &lt;a href="https://devexpress.github.io/testcafe/documentation/extending-testcafe/browser-provider-plugin/"&gt;using portable and remote browsers&lt;/a&gt; and &lt;a href="https://devexpress.github.io/testcafe/documentation/extending-testcafe/reporter-plugin/"&gt;customizing the test reports&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And if you feel like making your own plugin, check out the &lt;a href="https://devexpress.github.io/testcafe/documentation/extending-testcafe/"&gt;tips in the documentation&lt;/a&gt;. You can also send a quick note to TestCafe developers to share your plugin with the community (via &lt;a href="https://github.com/DevExpress/testcafe/issues"&gt;GitHub issue&lt;/a&gt; or &lt;a href="https://twitter.com/DXTestCafe"&gt;Twitter&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Hopefully this article helps you avoid some of the mistakes I made when using these tools. Happy testing!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/tag/testcafe"&gt;Testcafe&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/tag/automation"&gt;Automation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/tag/automation-testing"&gt;Automation Testing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/tag/automation-tools"&gt;Automation Tools&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/tag/ui-automation-testing"&gt;Ui Automation Testing&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
