Skip to main content
Home
  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact
programsbuzz facebook programsbuzz twitter programsbuzz linkedin
  • Log in

Main navigation

  • Tutorials
    • Quality Assurance
    • Software Development
    • Machine Learning
    • Data Science
  • About Us
  • Contact

Cypress Writing Your First Test

Profile picture for user arilio666
Written by arilio666 on 12/09/2021 - 14:07

Hi, guys, In previous tutorial we discussed about describe which is used to group test and it block which we use for creating test cases. Today in this article is gonna be absolute fun, because in this cypress article we are gonna write our first simple test using cypress basic commands.

  • We will be covering several topics while writing our first cypress test which includes visit, get, log, reload, clear, type, click and should.
  • Don't worry all will be explained along the way or before proceeding.

that's right!

We will take a look at what these commands do in a single line of explanation first before we move on to the code.

  1. Visit - obviously from the name we know that this command will visit the respective site we provide in it.
  2. Get - This command is used to fetch the elements from DOM in the form of selectors CSS or another style of our wish.
  3. Log - print message to cypress command log.
  4. Reload - This just reloads the website we are about to visit.
  5. Clear - This command clears any existing texts present in the text field.
  6. Type - When passed with the correct selector this command performs the typing operation.
  7. Click - When passed with the correct selector this command performs the click operation.
  8. Should - Used mostly for asserting.

Note all the commands above are always coupled up with cy.command because that's the way it does its operation in cypress.

Example

For demo purposes, we will be automating our respective article site https://www.programsbuzz.com/

Today we will be doing the following things: We are gonna automate the log-in functionality of the program buzz site.

Step 1: We need to visit the page https://www.programsbuzz.com/user/login.
Step 3: Reload the page and log it.
Step 3: Type in the wrong username and password.
Step 4: Click login.
Step 5: Verify the error message.

It should look something like this:

describe('Automating The Signin Of ProgramsBuzz Site',()=> {
  it.only('visit the site ', () => {
        cy.visit('https://www.programsbuzz.com/user/login')

        cy.log('Before Reload')
        cy.reload()
        cy.log('After Reload')

        cy.get('#edit-name').clear().type('Man Mala Man')

        cy.get('#edit-pass').clear().type('mamamamama')
        cy.get('#edit-submit').click()

        cy.get('.messages--error').should(
            'contain',
            'Unrecognized username or password. Forgot your password?'
        )
    })
})
  • So we have successfully written a functional test script to test the login functionality and assert the error message.
  • First, we are visiting the page https://www.programsbuzz.com/user/login.
  • We are logging the before and after reload with cy.reload to check whether the page is reloading.
  • This concludes or first test step.
  • In the second test step we are getting the username web element here we have used id as they are unique.
  • In the third test step similar to username we got the password web element.
  • In the fourth test step, we are clicking login.
  • In the final fifth test step when the error message appears after clicking login, we are extracting the element of the error message and asserting with should command that it should contain the following message along with it.

cypress write your first test

  • You can see from the output all the test steps passed and also you can see the assertion of the error message has succeeded too.
Related Content
Cypress Tutorial
Describe and It in Cypress
Cypress Locators
Tags
Cypress
  • Log in or register to post comments

Choose Your Technology

  1. Agile
  2. Apache Groovy
  3. Apache Hadoop
  4. Apache HBase
  5. Apache Spark
  6. Appium
  7. AutoIt
  8. AWS
  9. Behat
  10. Cucumber Java
  11. Cypress
  12. DBMS
  13. Drupal
  14. GitHub
  15. GitLab
  16. GoLang
  17. Gradle
  18. HTML
  19. ISTQB Foundation
  20. Java
  21. JavaScript
  22. JMeter
  23. JUnit
  24. Karate
  25. Kotlin
  26. LoadRunner
  27. matplotlib
  28. MongoDB
  29. MS SQL Server
  30. MySQL
  31. Nightwatch JS
  32. PactumJS
  33. PHP
  34. Playwright
  35. Playwright Java
  36. Playwright Python
  37. Postman
  38. Project Management
  39. Protractor
  40. PyDev
  41. Python
  42. Python NumPy
  43. Python Pandas
  44. Python Seaborn
  45. R Language
  46. REST Assured
  47. Ruby
  48. Selenide
© Copyright By iVagus Services Pvt. Ltd. 2023. All Rights Reserved.

Footer

  • Cookie Policy
  • Privacy Policy
  • Terms of Use