Cypress.io

Finally a real stable front-end testing framework

Lee Blazek

Today!

  • About Me
  • Who are you?
  • End-to-end testing
    • What is it?
    • Current support
    • What can it replace?
    • Basic usage
    • Alternates
    • gotchas
    • future features
  • Demos
    • cypress.io kitchensink
    • other?

About Me

React, Angular, JS, Mean Stack, Front-end, Node api's, with a side of iOS

"The New Guy"

What other people say about me?

??

What about you?

Automated testing a users actions in the browser. Usually connected to database, or mock DB.  Some times stubbed api calls

- ME

End to End Testing

What is it?

General e2e best practices (speed + reliability)

  • Don't reload page before every test("it" statement, ex. beforeEach()'s), this will triple or quadruple run times 
  • Don't abstract selectors do them in line(this introduces instability and inconsistency in tests)
  • You're not writing production application code, only commands for the tests( which in turn go thru layers of selenium, chrome). Abstraction and code complexity cause problems in e2e tests
  • Keep it simple and straight forward
  • test speed/performance is proportional to app performance

Current Support

Most all testing frameworks

are selenium based:

  1. Protractor (JS on top)
  2. Nightmare (JS on top)
  3. Nightwatch (JS on top)
  4. RSpec (ruby on top)
  5. Capybara(ruby on top)
  6. other?

In one way or another you can test

  1. Chrome
  2. Firefox
  3. IE
  4. Safari
  5. some times mobile devices

 

Cypress.io

 

Replace Other request libraries

  • No more sleeps!!
  • No more Selenium!!
  • Interactive GUI for writing and debugging
  • Time travel!!
  • easy stubbing of requests!
  • Cli for headless
  • built in screen shots
  • built in video
  • Docker Image to help with CI/CD

Basic Usage


context('My First Test', function() {
  it('should test stuff', function() {
    // Navigate browser to page
    cy.visit('https://example.cypress.io')
    // by the text inside it(all text!)
    cy.contains('about').click()
    cy.contains('type').should('exist')
    cy.contains('about').should('exist').click()
    // get is to get elements by css selector
    cy.get('h1').should('not.exist')
    // get/contains will intelligently return single object or array when needed
    cy.get('a').should('contain', 'about')
        .and('contain', 'help')
        .and('have.length', 2)
  })
})
/// <reference types="Cypress" />
import data from '../fixtures/data.json'

context('Assigned Rights', () => {
  before(() => {
    cy.server()
    cy.route("GET", '/api/users/data', data)
    cy.visit('/')
  })

  it('should open site', () => {
    cy.location('href').should('contain', '#/profile')
  })

  it('should have Covering Rights section', ()=>{
    cy.contains('name').should('exist')
    cy.get('.row').should('have.length.greaterThan', 10)
  })

Stubbing with cy.route()

GUI

  1. time travel
  2. tests don't reload page(unless instructed)
  3. can choose which files(tests) to run

Headless

  1. no time travel
  2. tests reload page in in between 
  3. runs all

Be Aware of:

  • Capitalization of text vs CSS uppercase etc  
  • if using mock data make sure its there
  • Keep it simple
  • use the smallest css selector or tag, or cy.contains()

Gotcha's

  1. Cookies are cleared between tests by default(but you can persist easily)
  2. Local and session storage are persisted by default(but you can clear easily)
  3. Can't Stub ES6 fetch out of the box, but there are work arounds
  4. Chrome and Electron Only
  5. No multi-window or tabs
  6.  Still some selector issues but its 95% reliable, and consistent in errors
  7. https://docs.cypress.io/guides/references/trade-offs.html#Inside-the-browser

 

 

Future Features

(don't try these at home kids)

DEMO's

  1. Patientchart GUI and headless
  2. reports
  3. screenshots and videos

Lee Blazek

  • www.berzek.io
  • info@berzerk.io
  • linkedin: www.linkedin.com/in/leeblazek
  • https://github.com/berzerk-interactive

Cypress.io(V2 - 2019)

By Lee Blazek

Cypress.io(V2 - 2019)

  • 359