How to end-to-end testing your application using Cypress.io

Image from https://unsplash.com/@kellysikkema
Image from https://unsplash.com/@kellysikkema

In this article, you will learn:





  • What is Cypress and when to use it





  • Testing Basics Using Cypress





  • Advanced Cypress Commands





  • Interacting with user interface elements





  • Best Practices Using Cypress






Introduction

To test your applications, you need to take the following steps:





  • Launch the application





  • Wait for the server to start





  • ( , )





  • , ( , ..)









. , .





Cypress. Cypress , , :





  • ( , ..)













!  Cypress .  , , , .





, โ€” , . Cypress .





, Cypress, .






Cypress

, :





Project initialization

, Cypress:





Installing Cypress
Cypress

 . Linux,   ,  Cypress NPM.





, Cypress , :





Opening of Cypress
Cypress

(Test Runner):





Test Runner interface
Test Runner

.






Cypress

Cypress ,   cypress/integration



.  :





Go to cypress / integration
cypress/integration

JavaScript  basicTest.js



:





Creating a JavaScript file
JavaScript

Cypress, Test Runner :





Real-time file structure update

.





/cypress/integration/basicTest.js



:





The code for the basicTest.js file
basicTest.js
  • 1:  describe



    Cypress .





  • 2:  it



    , .





  • 3: .  , 2 + 2 4.  false



    , .





,  basicTest.js



Cypress.





Clicking on basicTest.js in Test Runner
basicTest.js Test Runner

:





Test run result

! , .





, ?   /cypress/integration/basicTest.js



   describe



:





Code to add to basicTest.js
basicTest.js
  • 2: 4 5 10, . , .





.  :





The result of our second test

, .  , .





basicTest.js



  :





Code to add to basicTest.js
basicTest.js
  • 2: 5 5   100, .





:





Test result: successful!
: !

!  .  expect



  BDD (behavior-driven) .  , (test-driven assertions).





 /cypress/integration/basicTest.js



:





(test-driven assertions) assert

TDD  assert



.





 basicTest.js



:





  • 2:  name



      age



    .





  • 6:  isObject



    person



    .   true



    ,  value is object



    .  , .





  • 10: ,  name



      .





  • 14: ,  name



      .





.  :





The result of running our test

!  .  Cypress.





 basicTest.js



:





-

 Demoblaze , , .





 /cypress/integration/



 basicCommandsTest.js



.  :





Code for basicCommandsTest.js
basicCommandsTest.js
  • 3:  visit



    , Cypress - Demoblaze.





 basicCommandsTest.js



Test Runner:





Click on basicCommandsTest.js in Test Runner
basicCommandsTest.js Test Runner

:





!  .  Cypress.





 basicCommandsTest.js



:






Cypress:

. , , HTML Cypress.





Cypress   JQuery  -.





, myButton



id



, :





Getting an element via the id of an element
id

, myButton



, :





Getting an element through a class name

.





  The-Internet  .  -   / .





ยซ ยป.





Test page

DevTools, ,  button



 onclick



,  addElement()



.





Screenshot from DeveloperTools
DeveloperTools

  :





Item identification

 /cypress/integration



 runningClickCommand.js



.  :





  • 2: -.





  • 6: button



    .





. :





Outputting the result

, !  , ,  Add Element



.





.





  The-Internetโ€™s login.  .





Screenshot of the site for testing
Screenshot from DeveloperTools
DeveloperTools
Screenshot from DeveloperTools
DeveloperTools

username



 id



 username



,  password



 id



 password



.  , Login



 type



 submit



.  ,  username



 password



,   JQuery id



:





Identifying an element by its id
id

,  button



,    , :





 /cypress/integration



 runningTypeCommand.js



.  :





  • 3: .





  • 6:  username



     type



      ,  tomsmith



    .





  • 7:  password



     SuperSecretPassword



    .





  • 10: ยซยป.





.  :





Outputting the result of running the code

!  .





  The-Internet .





DevTools:





Developer Tools
Developer Tools

 type



  checkbox



.  ,   form



 id



 checkboxes



.    JQuery -:





Identifying our checkboxes

 /cypress/integration/



 runningCheckCommand.js



:





  • 4: ,  check



    , .





  • 7: Cypress .





  • 8: .   uncheck



    , .





. :





Test run result

!  .  Cypress.





.  , HTML, ,  ul



 li



.





 should



.     โ€” The-Internetโ€™s Add Element





Screenshot of the tested page
Developer Tools
Developer Tools

 Delete



 added-manually



.   button



:





Our approval

:





Receiving an item and then approving

, :





Our approval

:





Receiving an item and then approving

 /cypress/integration/runningClickCommand.js



:





Code for runningClickCommand.js
runningClickCommand.js
  • 1:  added-manually



    .  , (have.length)



      .





  • 3:  Add Element



    , , (have.text)



       Add Element



    .





.  :





Launch Result

!  .   each



.





 cypress/integration/runningClickCommand.js



:





each

   The-Internetโ€™s Add Elements page:





Screenshot of test site

,  Delete



.  ;  ,  Delete



  . , .





-  each



. , .   Delete



 click



.





Developer Tools:





 Delete



 class



 added-manually



.       each



, :





Retrieving an element and using each then
each

 /cypress/integration/runningClickCommand.js



:





Code for runningClickCommand.js
runningClickCommand.js
  • 2: ,  .added-manually



    .   $el



    .





  • 3: , Cypress.  .





:





Result of code execution

!  , .





:





:





  • 2: , Cypress,  Add Element



    20 .





.  :





Result of code execution

, - . !





, Cypress.





 cypress/integration/runningClickCommand.js



:






, .  :





Not the best structure

Test Runner :





Displaying the test structure in the Test Runner
Test Runner

Cypress , .  , :





Good project structure

, :





:





Sample code

,     get



 type



.    ,  .





 cypress/support/commands.js



:





  • 1: ,  identifier



     data



    .





  • 2: , .





 .  /cypress/support/commands.js



.





:





Sample code

, .





ยซยป

:





HTML  id



 first



.





Cypress .  , , :





 and



 should



.





Cypress

exec



  .  .





 localhost



, ,    Cypress.





Terminal Commands

GitHub

GitHub





  • Cypress Tutorial Videos





  • End-To-End Testing With Cypress by codedamn





  • Real Confidence With Cypress





  • Best practices with Cypress: AssertJS Conference






Testing is a key step in the development process because it ensures that your application works correctly. Some programmers prefer to manually test their programs because writing tests takes a significant amount of time and energy. Fortunately, Cypress has solved this problem by allowing the developer to write tests in a short time frame.





Thank you for making it to the end! If you feel any confusion, I advise you to play around with the code and go through the examples.








All Articles