Monday, September 28, 2020

Cypress: How to handle browser-based authentication pop up dialog in Cypress

Five years ago I have written a blog on how to handle browser-based authentication for selenium webdriver.

 

Now it is for cypress.


Cypress has come with a very easy solution.


A very simple syntax to handle the browser-based authentication,

If we have a username and password and the URL then the cypress command will be 


cy.visit(app_URL, {auth:{username:appUserName, password: appPassword}})


For example, we have

username : abc

password : abc123

URL : xyz.com


Then cypress command will be


cy.visit('https://www.xyz.com', {

auth: {

        username: abc,

        password: abc123

      }

})

 

There is an alternative way that cypress suggested,


cy.visit(http://username:password@domainName.com)


Note that we have a special character colon “:” between username and password.


For the above example,


Cypress command will be,


cy.visit('https://abc:abc123@www.xyz.com')


That’s all for now. We will discuss more on cypress in my future post.

Thank you. 

Happy automation testing with Cypress!

Saturday, September 12, 2020

Cypress: How to test a web application using Cypress

In the previous posts, we have discussed various features of cypress and the installation procedure. 

Here we will see how a sample test can be written and execute the test using a  cypress test runner.


Test case: Search with the text cypress on Google.


Steps:

  1. Go to Google.com

  2. Type the text ‘cypress’

  3. Click on the Search button

  4. Verify the text “JavaScript End to End Testing Framework | cypress.io”


Now start automating the script.


Create a project structure: 

  1. Open the folder using the visual studio code that I have created in my last post in order

to install the cypress application. Note that I am using visual studio code as the editor for scripting.
You can use any editor that you feel comfortable with.

  1. Go to Terminal->New Terminal to open a new terminal.



  1. Type the following command to create a new project structure.

npx cypress open





  1. It will create a cypress project structure. 




  1. Click the OK button and the project structure has been created 

  2. Cypress Test runner window is now opened and click the close button to close the window.

  3. The project structure looks like the following.

     

    

  1. Now expand the cypress folder and see the structure.

     


  1. There are four folders.

    1. fixtures: All the test data files are there,

    2. Integration: All the test scripts are there.

    3. Plugins: cypress plugins are there.

    4. Support: all the customized commands are there.

    I will discuss the project structure in a separate post.


Writing Test:

Now come to write the cypress test. 

  1. Expand the Integration folder and note that a folder named “examples” are displayed.



  1. Now delete that folder as it has some sample scripts and it is not needed now.

  2. create a new test file name cypress_test.spec.js

     



  1. Write the following script 



Note that we use describe..it blocks. As cypress is using mocha by default, it uses this block.

cy.visit() : to visit a page 

cy.get(): for getting element location or searching an element

cy.get().click() : to click an element

cy.contains(): to check the text available on the page.


We will discuss more details on cypress API in later posts.


  1. Save the file.



Run the test using TestRunner:

  1. Open a terminal window

  2. Type the following command in the terminal.

npx cypress open




  1. It will open the Cypress test runner.

     

Now click on the cypress_test.spec.js file and it will start running.

 

Run the test in the terminal:

  1. If you want to run the cypress test in terminal, then just open the terminal and type the following

command:

npx cypress run



  1. The test will start running in headless mode and display the result.




That’s all for now. We will discuss more on cypress in my future post.

Thank you. 

Happy automation testing with Cypress!

Sunday, April 19, 2020

Cypress: How cypress can be installed in our windows machine

In our previous post, we have discussed various features of our latest automation tool cypress.
Installation of cypress is easier than any other automation tools. We can install it through npm. 
It is a desktop application and it can run Windows, macOS, and Linux.

In my case, I am using windows OS.

Here I will show you a step by step process to install cypress.

Step 1: 
Create a folder on your drive with any name. Here I make a folder in my D:\Development\ directory named ‘cypress-test’.

Step 2: 
Now open your command prompt window or windows PowerShell editor and run the following command to install cypress globally. 
Note that you can install cypress globally or dev dependency. 

To install cypress globally run the following command.
npm install cypress -g

To install cypress locally you have to move to the directory where you want to develop the cypress script.

In my case, I just move to the directory D:\Development\ named\cypress-test’
The command is,



Run the following command to initiate a package.json file.



Now run the npm command to install cypress.



Now Cypress will start to install.



Note that I am installing cypress version 4.4.0.
This is the latest version when I am writing my post.



Now cypress has successfully installed and we are ready to write our first cypress test.
If you go to the cypress-test folder you see the following folder and files.

 

In the next post I will show how we can make our cypress project and write a test script and execute.

Saturday, April 18, 2020

Cypress: An open-source front end tool for an end to end testing

Nowadays end to end test is becoming popular and tester are very much fond of these tools. 
In that case, Cypress is one step ahead due to its first execution of the test script. Scripts are written in 
javascript and no runtime server needed. 

It is an open-source and developer-friendly tool and builds from scratch and no selenium dependency.

According to cypress, it has the following features that make it different from other tools. 
  • For every step, it takes a screenshot. When you hover it shows a recorded screenshot.
  • It can automatically rerun when any changes happen in the script.
  • It has spies, stubs, and clocks like unit tests.
  • Since it does not use any selenium or web driver, tests result are consistent, reliable and flake-free.
  • Debugging is user friendly.
  • No waits needed. It can automatically start and stops when needed.
  • Network Trafic can be stubbed when needed.
  • Screenshot can be taken automatically when scripts fail. Also, video can be recorded for the whole steps.

In my next post, I show how cypress can be installed and a simple test can be run. 

Welcome to the Cypress world!

Cypress: How to handle browser-based authentication pop up dialog in Cypress

Five years ago I have written a blog on how to handle browser-based authentication for selenium webdriver.   Now it is for cypress. Cypress...