This document provides an introduction to Cypress end-to-end (e2e) testing in Angular, outlining its advantages over manual testing and older frameworks like Protractor. It details the configuration, installation, and usage of Cypress for effective testing, including command examples and best practices for selecting elements. Additionally, it discusses future plans for Angular's testing ecosystem and provides links to detailed resources for implementation.
Future of AngularE2E & Plans for Protractor
• Protractor was created in 2013, when
• WebDriver APIs were not yet a standard and
• end-to-end (e2e) tests were hard to write due to lack of support for async / await.
• To solve this problem, Protractor wraps selenium-webdriver and abstracted
asynchronous operations from developers with the use of Control Flow.
• Protractor, being dependent on selenium-webdriver, is not able to upgrade to the
new version without introducing a huge breaking change and forcing users to do a
migration for all their tests.
Angular
未来计划
• Angular 将会deprecate protractor - Version 12
• Angular team will ensure smooth integration with
Cypress by using Angular CLI
11.
如果现在就要用的话
• Or followthe guidelines below to configure your angular projects to
use Cypress:
• https://docs.cypress.io/guides/migrating-to-cypress/protractor#Introduction
• https://nx.dev/latest/angular/modern-angular/protractor-to-cypress
12.
Why Cypress?
• Freeand open-source test runner
• Run in browser alongside with your application code
• Mocha, Chai, JQuery selectors built in
• Error reporting, screenshots and videos for improve debugging
• Great documentation and support
Demo 2: Yourfirst test
• Add intelliSense triple-slashes directive into your test file
• Write a describe with a single it to test the baseUrl of your application
• https://docs.cypress.io/api/commands/visit
• Run and test to see in action
21.
Steps to writea test
• Visit a page
• Query for an element
• Perform a command
• Make assertions
• Repeat as needed
https://docs.cypress.io/guides/getting-started/writing-your-first-test#Write-a-real-test
Demo 3: SelectingDOM elements
• Write a test that does the following
• Visit our page
• Select the header banner and validates it contains the text
• Selects any other element on the page
24.
Selector best practices
•Use data attribute to select
https://docs.cypress.io/guides/references/best-practices
25.
Selector best practices
•Use custom command to select
https://docs.cypress.io/guides/references/best-practices
26.
Demo 4: Addingtest selectors
• Update the html to add a data-cy attribute to any element
• Rewrite your Cypress test to select using the data-cy attribute instead
27.
Cypress Commands
• Commoncommands:
• click
• type
• clear
• check
• Trigger (https://docs.cypress.io/api/commands/trigger#Syntax)
• Make sure chained off a command that yields a cy element, like
cy.get() or cy.contains()
https://docs.cypress.io/guides/core-concepts/interacting-with-elements
Demo 5: TestingApp Functionality
• Write a test that does the following:
• Visit our application
• http://localhost:4200/sign-in
• Login using the following username and password
• hughes.brian@company.com
• admin
• Don’t worry about assertions first, just use selectors and commands
30.
Type of Assertions
•Page elements render as expected
• Content
• Style
• Visibility/Actionability
• User actions result in expected behaviour
• Links go to correct URLs
• Event listeners fire correctly
• Expected side effects occur
• HTTP requests
• Data updates
https://docs.cypress.io/guides/references/assertions
31.
Demo 6: Addingassertions
• Write a test that does the following:
• Visit our application
• http://localhost:4200/sign-in
• Login using the following username and password
• hong@company.com
• admin
• Assert the item you just enter
32.
Network Request
• Monitorall the network activities with the Cypress test runner.
• Spy or stub the network request
• Setup test state
• Bypass third party integrations
• Make assertion on the network requests
https://docs.cypress.io/guides/guides/network-requests
Demo 7: Asserta network request
• Visit our application
• Intercept a request
• Add a wait in your test for the network request, then assert on
whether its status is 200
Workshop
• Testing AngularApplications with Cypress Cecelia Martinez
| EnterpriseNG 2020 #ngconf - YouTube
• CypressCecelia/cypress-testing-angular-workshop: Exemplary real
world application built with Angular (github.com)