KEMBAR78
Introduction to cypress in Angular (Chinese) | PPTX
Introduction to
Cypress e2e testing
in Angular
我是谁?
• 尤丰达 - Yew Hong Tat
• 软件工程师 @ 沃旭能源 Orsted
• 熟悉 Angular 和 C#
• 爬山,慢跑爱好者
hongtat_yew
hongtat88
手动测试 vs 自动测试
e2e testing
为什么要
做e2e测试?
• 增加信心
• 减少程序错误
• 确保关键功能在发布的时候可以正常运行
• e2e 测试是模仿用户的行为
e2e 测试 in
Angular
• Protractor
• Cypress
• Katalon
• Selenium (它不是测试工具, 它是自动化工具)
Protractor
Future of Angular E2E & 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
如果现在就要用的话
• Or follow the 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
Why Cypress?
• Free and 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
Installation
https://on.cypress.io/install
Open Test Runner
https://on.cypress.io/test-runner
Folder structure
https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Folder-structure
Configuration
https://docs.cypress.io/guides/references/configuration
Demo 1
• Install Cypress
• Set base url in Cypress configuration
• Add cy:run and cy:open scripts into package.json
Test structure
https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests
Mocha
• https://docs.cypress.io/guides/references/bundled-tools#Mocha
Demo 2: Your first 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
Steps to write a 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
Cypress API Basics
• Selectors
• cy.get()
• cy.contains()
• Commands
• Build in actionability check
• cy.click()
• cy.type()
• Assertions
• Chai Assertion Library
• BDD (expect, should)
• TDD (assert)
https://docs.cypress.io/guides/getting-started/writing-your-first-test#Write-a-real-test
Demo 3: Selecting DOM 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
Selector best practices
• Use data attribute to select
https://docs.cypress.io/guides/references/best-practices
Selector best practices
• Use custom command to select
https://docs.cypress.io/guides/references/best-practices
Demo 4: Adding test 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
Cypress Commands
• Common commands:
• 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
Cypress Commands
• Alternative commands and assertions to make command retryable
Demo 5: Testing App 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
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
Demo 6: Adding assertions
• 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
Network Request
• Monitor all 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
Network Request
https://docs.cypress.io/api/commands/intercept
Network Request
https://docs.cypress.io/api/commands/intercept
Demo 7: Assert a 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
Thank you
Desk:
https://1drv.ms/p/s!AhzbaHAE3IDkguc09FbmZAr9Aduc5w?e=4KEs3l
Code: hongtat88/cypress-tw (github.com)
Write Cypress in TypeScript
• https://docs.cypress.io/guides/tooling/typescript-support#Install-
TypeScript
Workshop
• Testing Angular Applications with Cypress Cecelia Martinez
| EnterpriseNG 2020 #ngconf - YouTube
• CypressCecelia/cypress-testing-angular-workshop: Exemplary real
world application built with Angular (github.com)
Login methods
• https://docs.cypress.io/guides/testing-strategies/auth0-
authentication#Auth0-Application-Setup

Introduction to cypress in Angular (Chinese)

Editor's Notes

  • #16 Writing a Plugin | Cypress Documentation
  • #36 Project.component.ts 'https://reqres.in/api/users' cy.wait('@getUsers') .its('response.statusCode') .should('equal', 200);