Skip to content

Latest commit

 

History

History
127 lines (108 loc) · 6.67 KB

cypress-reference.md

File metadata and controls

127 lines (108 loc) · 6.67 KB

Cheatsheets: Cypress Reference

Navigating

  • setCookie
    • cy.setCookie("cookie_consent", "agreed")
  • visit
    • cy.visit("https://www.kiwi.com") - absolute
    • cy.visit("/account/login") – relative, will use baseUrl from cypress.json config
  • go
    • cy.go("back") / cy.go("forward"), same as cy.go(-1) / cy.go(1)
  • reload
    • cy.reload()

Selecting

  • get
    • cy.get("h1.title") / cy.get("[data-test='uniqueSelectorJustForTests']")
    • cy.get("#todos").as("todos") …then… cy.get("@todos")
  • ️contains
    • cy.contains("Register")
    • cy.get("button").contains("Submit")
  • focused
    • cy.focused()

Asserting/Expecting

  • should
    • cy.get(…).should("have.class", "active")
    • cy.get(…).should("have.length", 5)
    • cy.get(…).should("be.disabled")
    • cy.get(…).should("contain", "Joe")
    • cy.get(…).should("not.contain", "Joe")

➡️ List of Assertions ⬅️

Interaction

  • click & dblclick
    • cy.get(»selector«).click()
    • cy.get(»selector matching multiple elements«).click({ multiple: true }) – use with caution!
    • cy.get(»selector matching element which is hidden«).click({ force: true }) – use with caution!
  • type & clear
    • cy.get(»input«).type("Brno")
    • cy.get(»input«).type("Br{downarrow}{enter}") – e.g. testing autocomplete
    • cy.get(»input«).type("Slow typing", { delay: 1000 }) – use only when necessary
    • cy.get(»input with some text already«).clear().type("New value")
  • check & uncheck
    • cy.get(»terms&conditions checkbox«).check()
  • select
    • cy.get(»nationality«).select("cz")
  • focus & blur
    • cy.get(»input«).focus().should("have.class", "active")
    • cy.get(»input«).type("joe@gmail").blur().should("have.class", "invalid")
  • submit
    • cy.get(»form«).submit()
    • 🐨 some people prefer clicking on submit button / pressing "Enter" instead

Viewport

  • viewport
    • cy.viewport(1280, 1024)
    • cy.viewport("macbook-15")
    • cy.viewport("iphone-5", "portrait")
  • scrollIntoView
    • cy.get("footer").scrollIntoView()
  • scrollTo
    • cy.scrollTo(0, 500)x/right, y/down
    • cy.get(»sidebar«).scrollTo("bottom")

Utils

  • wait – milliseconds!
    • cy.wait(5000) – use only when necessarily
  • screenshot
    • cy.screenshot() – automatic filename from test filename and test suite structure
    • cy.screenshot(»filename«)

Debugging 🛠

Interaction programmatically 🤖 TODO: Later

Environment 🤖 TODO: Later

Mocking 🌍 TODO: Later

More selectors 🙈

⚠️ Prefer selectors directly inside cy.get(»selector«)
😐 🤩
cy.get("button").first() cy.get("button:first")
cy.get("button").eq(3) cy.get("button:eq(3)")
cy.get("button").not(".unwanted") cy.get("button:not(.unwanted)")
cy.get("»modal«").find(»close btn«) cy.get("»modal« »close btn«")
cy.get("tr").filter(".odd") cy.get("tr.odd")