<?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: Simone Richini</title>
    <description>The latest articles on DEV Community by Simone Richini (@simosleepwalker).</description>
    <link>https://dev.to/simosleepwalker</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%2F979173%2F6cda7422-4608-4ba5-adfb-bce9db9e2e9e.jpeg</url>
      <title>DEV Community: Simone Richini</title>
      <link>https://dev.to/simosleepwalker</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/simosleepwalker"/>
    <language>en</language>
    <item>
      <title>How to involve your customers in Cypress Testing?</title>
      <dc:creator>Simone Richini</dc:creator>
      <pubDate>Wed, 30 Nov 2022 10:00:00 +0000</pubDate>
      <link>https://dev.to/simosleepwalker/how-to-involve-your-customers-in-cypress-testing-576g</link>
      <guid>https://dev.to/simosleepwalker/how-to-involve-your-customers-in-cypress-testing-576g</guid>
      <description>&lt;p&gt;I'm working as a consultant for an IT company, Some customer projects are not ready for E2E tests and it is not always easy to include them if you haven’t a clear idea of the code business domain flow.&lt;/p&gt;

&lt;p&gt;It is also common, that we don't know how the customers use the application from the front-end side, and this could lead to some discrepancies between the devs and the customer's teams in the use cases.&lt;/p&gt;

&lt;p&gt;That is why it could be useful to make our &lt;strong&gt;customers write tests&lt;/strong&gt; on their own.&lt;/p&gt;

&lt;p&gt;In these cases, Cypress helps us, providing some great features for writing some E2E tests.&lt;/p&gt;

&lt;p&gt;One of these features is &lt;strong&gt;Cypress Studio&lt;/strong&gt;, which allows you to write tests simply by clicking on the web page and tracking the interactions with your software.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to enable Cypress Studio?
&lt;/h3&gt;

&lt;p&gt;If we want to enable Cypress Studio we first need to add Cypress to our React project, which can be done with the command &lt;code&gt;npm install cypress -D&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once installed, you can run Cypress with &lt;code&gt;npx cypress open&lt;/code&gt; and make the initial configuration.&lt;/p&gt;

&lt;p&gt;After that, open the file &lt;em&gt;"cypress.config.ts"&lt;/em&gt; and in the entry &lt;code&gt;e2e&lt;/code&gt; insert this line: &lt;code&gt;experimentalStudio: true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The file should look like this now:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ev4iyo19ok3qeh6rg2o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ev4iyo19ok3qeh6rg2o.png" alt="Cypress config" width="423" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Problems:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If the elements we want to click are not distinguished by an id, Cypress Studio can't always keep track of the button interactions consistently.&lt;/li&gt;
&lt;li&gt;As developers, our input values for tests are not the real values that the customers use.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How can we solve these problems?
&lt;/h3&gt;

&lt;p&gt;One solution could be to get the elements by their tag or their label; however, with this solution, we can't directly use the tests written with Cypress Studio. &lt;br&gt;
Because they would need a re-elaboration by the devs to convert the tests in a way where elements are clicked based on the content of their label.&lt;/p&gt;

&lt;p&gt;One of the first things we had in mind was to assign a &lt;code&gt;data-cy&lt;/code&gt; attribute to every element on the page because Cypress studio can keep track of these elements.&lt;/p&gt;

&lt;p&gt;So we wrote a function that whenever there is an update in the body of our webpage, assigns to each element an attribute &lt;code&gt;data-cy=[num]&lt;/code&gt;, where num is the counter of the elements of the page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx55jj4bnr2zixjelwqd8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx55jj4bnr2zixjelwqd8.png" alt="idManager v1" width="793" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We inserted a new script into the 'package.json' and edited our app to make the idManager work only when we run it in test mode.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjzyxbfxj0grc7im7ax2z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjzyxbfxj0grc7im7ax2z.png" alt="package.json script" width="411" height="30"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ml0caaklp3smt24eww7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ml0caaklp3smt24eww7.png" alt="call to idManager" width="574" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At first, we were satisfied with this solution, we managed to create lots of tests and they all run perfectly, even when including them in our &lt;strong&gt;GitHub actions&lt;/strong&gt;.&lt;br&gt;
Another cool fact was that there were no repercussions on the responsiveness of our application since the idManager was used only if we built the app for test.&lt;/p&gt;

&lt;p&gt;It was all fun and games until we developed a new feature, the result of our GitHub Action was &lt;code&gt;FAILED: 0 tests passed&lt;/code&gt;.&lt;br&gt;
Ok cool, what's wrong now?&lt;/p&gt;

&lt;p&gt;Debugging, we figured out that the ids of the webpage were all different from the ones in the tests, due to the modifications we applied for the feature. So we understood that assigning the ids with a &lt;strong&gt;counter was not a good choice&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  How to make the idManager more solid toward modifications?
&lt;/h3&gt;

&lt;p&gt;After some brainstorming, we ended up with a solution:&lt;br&gt;
give the &lt;code&gt;data-cy&lt;/code&gt; a value equal to the one present in some predetermined attributes, like in the following example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;// BEFORE idManager
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
// AFTER idManager
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;data-cy=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's not enough, in some cases, we need to get the &lt;code&gt;textContent&lt;/code&gt; too; to obtain something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;// BEFORE idManager
&lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Users&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
// AFTER idManager
&lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;data-cy=&lt;/span&gt;&lt;span class="s"&gt;"users"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Users&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After some modifications, that's how we choose the value of &lt;code&gt;data-cy&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7nemaytiu17pq8oc0f22.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7nemaytiu17pq8oc0f22.png" alt="getDataCy function" width="800" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We first check if the tag has one of the attributes that we want to check, if it doesn't we check its &lt;code&gt;textContent&lt;/code&gt; (only if it's one of the tags that we are interested to check).&lt;/p&gt;

&lt;p&gt;Now that the &lt;code&gt;data-cy&lt;/code&gt; is based on some &lt;strong&gt;distinctive values&lt;/strong&gt; we can edit their positions as we want and the tests will always execute the right way.&lt;/p&gt;

&lt;p&gt;Here is an example of the use of Cypress Studio with a webpage using our idManager: &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/zrz8la9j8nc"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;As you can see, the test runs successfully. &lt;br&gt;
However, it is good to apply a little modification: make the assert check the text in the body instead of the div; with this little tweak, we can be sure that the text we are looking for is &lt;strong&gt;inside the page&lt;/strong&gt;, even if we decide to put it in another element.&lt;/p&gt;

&lt;p&gt;Our final test will result in this code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvu7rfjl7hnd2oyv3m1du.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvu7rfjl7hnd2oyv3m1du.png" alt="Test Login" width="767" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you very much for being here with me, I hope you find this article helpful!&lt;/p&gt;

&lt;p&gt;If you've liked it don't forget to leave a ❤️ and comment with your opinions 👀&lt;/p&gt;

</description>
      <category>career</category>
      <category>mentorship</category>
      <category>productivity</category>
      <category>leadership</category>
    </item>
  </channel>
</rss>
