Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cypress - Best Practices

Brian Mann
February 27, 2018

Cypress - Best Practices

Best practices in writing tests with Cypress including how to organize tests, strategies for testing logging in, and controlling state.

Brian Mann

February 27, 2018
Tweet

More Decks by Brian Mann

Other Decks in Technology

Transcript

  1. • View Articles • View Article • View Author •

    View Comments • Log In • Register Logged In Not Logged In • Settings • Log Out • Create Article • Edit Article • Delete Article • Favorite Articles • Create Comments • Delete Comments
  2. Edit Article Delete Article Favorite Article Settings Log Out Create

    Comment Delete Comment Create Article View Article View Comment Register Log In Server Database
  3. Strategies 1 Stub Requests Control the network - cy.server() -

    cy.route() - Set status codes - Set response bodies - Test edge cases (empty views / 500) Fast, Easy, Flexible No Server / DB Not True E2E Requires Fixtures
  4. Strategies 1 Stub Requests Fast, Easy, Flexible No Server /

    DB Not True E2E Requires Fixtures 2 Static User Shared User + Credentials - OAuth Login (Github / Google) - Pre-seeded Databases - [email protected] / joe
  5. Strategies 1 Stub Requests Fast, Easy, Flexible No Server /

    DB Not True E2E Requires Fixtures 2 Static User Real Session E2E Shares Test State Seed the DB Requires Server
  6. Strategies 1 Stub Requests Fast, Easy, Flexible No Server /

    DB Not True E2E Requires Fixtures 2 Static User Real Session E2E Shares Test State Seed the DB Requires Server 3 Dynamic User New User for each Test - Modify DB within Tests - Query DB within Tests
  7. Strategies 1 Stub Requests Fast, Easy, Flexible No Server /

    DB Not True E2E Requires Fixtures 2 Static User Real Session E2E Shares Test State Seed the DB Requires Server 3 Dynamic User No State Mutations Slow / Complex DB Setup / Teardown Flexible / Powerful
  8. Settings Commands (7 of 9) Failed at testing in isolation

    Added 0% more confidence ƭ login_spec.js 100% Confidence
  9. Settings Commands (7 of 9) Accounted for 75% of the

    duration Failed at testing in isolation Added 0% more confidence
  10. 1. onFormSubmit 2. Reads form values 3. POST /api/users/login 4.

    Success: Save Token 5. On load, check for token
  11. Best Practices Don’t use the UI to build up state

    Set state directly / programmatically Don’t use page objects to share UI knowledge Write specs in isolation, avoid coupling Don’t limit yourself trying to act like a user You have native access to everything