Cypress E2E Testing Framework
Features
- No more async hell
Cypress automatically retries the query until the element is found. - Real time reloads
Cypress automatically reloads whenever you make changes to your tests. - Spies, stubs, and clocks
Verify and control the behavior of functions, server responses, or timers. - Run with native browsers
Keep fast, consistent and reliable tests.
運作方式

基本測試撰寫
| |
Common Querying Methods
Use jQuery-style selectors.
get(selector, options)
Yield: DOM elements.find(selector, options)
Get the descendent DOM elements of a specific selector.
Yield: DOM elements.contains(content, options)
Get the DOM element containing the text. Yield: DOM elements.
Example
HTML file
| |
test case
| |
Common Action Methods
click(position, options)submit(options)select(value, options)
All Yield current DOM elements.
Example
| |
Assertion Methods
should(chainers, method, value)
Notice: Any valid chainer that comes fromChaiorChai-jQueryorSinon-Chai.
Example
| |
Cypress Commands Are Asynchronous
Reference: Commands Are Asynchronous
Use a chain of promises
結合 Promise, Retry 和 timeout 來實作 Chainer object 實現非同步測試。

Example
How to set up timeout object.
| |
Run tests serially
將 command 加到特定佇列之中,並在最後才根據佇列順序來執行測試流程。
| |
Somethings you MUST need to know
1. Cypress is the top of Mocha testing framework
Reference: Mocha is a feature-rich JavaScript test framework
- Could use Mocha related reporters(we use
mochawesome). - Cannot change to your preferred frameworks.
2. Cypress is an E2E testing framework
- It is very hard to measure code coverage. Issue: Code Coverage #346
- 如果需要跑 Code Coverage,請額外使用其他 testing library (Mocha)來執行 Unit test.
3. Supported browser Canary/Chrome/Chromium/Electron
- 目前沒有支援 Chrome headless, 因為 Chrome extension 不支援。不過目前已有解法,還在開發中。
- Default use
headless electron. Will supportChrome headlessin coming days. Issue#832: Support chrome headless and change defaults for cypress run - If wanna run CI with Chrome, you can use command
cypress run --browser chrome. (Notice: This is in head mode not headless.) - Will support
Firefoxin the future.
Run Cypress with Cucumber
Cucumber
- A tool that supports Behaviour-Driven Development (BDD)
- Cucumber executes executable specifications written in plain language.
Feature file & Step js file
- Feature file
.feature- 可由PM或其他非工程師的人撰寫。 Step.js- 由工程師搭配 feature file 的內容來實作測試流程。
Write a Feature file
.feature, 每個 feature file 為一個功能。Feature file 使用可以閱讀的文字敘述並搭配幾個指定單字:Given(前提假設), When(當什麼改變), Then(結果), And,or But. 一個 Feature files 通常包含一系列相關的 scenarios。
Example:
| |
" " - 標示此詞為 Variable
| |
實作 Step.js file
- 如果 .feature 中的步驟沒有被實作在 step file 中,就會報錯。
Example
Feature
| |
Step.js file
| |
Connect Cucumber to Cypress
Use cucumber preprocessor plugin
- Know more about Cypress plugins Cypress plugin Document
- The current plugin we use is cypress-cucumber-preprocessor

Add your plugins to plugins/index.js
| |