
- Cypress 教程
- Cypress - 首頁
- Cypress - 簡介
- Cypress - 架構和環境搭建
- Cypress - 測試執行器
- Cypress - 建立第一個測試
- Cypress - 支援的瀏覽器
- Cypress - 基本命令
- Cypress - 變數
- Cypress - 別名
- Cypress - 定位器
- Cypress - 斷言
- Cypress - 文字驗證
- Cypress - 非同步行為
- Cypress - 使用 XHR
- Cypress - jQuery
- Cypress - 複選框
- Cypress - 標籤頁
- Cypress - 下拉選單
- Cypress - 警報
- Cypress - 子視窗
- Cypress - 隱藏元素
- Cypress - 框架
- Cypress - 網頁表格
- Cypress - 滑鼠操作
- Cypress - Cookie
- Cypress - GET 和 POST 請求
- Cypress - 檔案上傳
- Cypress - 資料驅動測試
- Cypress - 提示彈出視窗
- Cypress - 儀表盤
- Cypress - 截圖和影片
- Cypress - 除錯
- Cypress - 自定義命令
- Cypress - Fixtures (夾具)
- Cypress - 環境變數
- Cypress - Hooks (鉤子)
- Cypress - JSON 檔案配置
- Cypress - 報告
- Cypress - 外掛
- Cypress - GitHub
- Cypress 有用資源
- Cypress - 快速指南
- Cypress - 有用資源
- Cypress - 討論
Cypress - 複選框
check 和 uncheck 命令用於操作複選框。在 HTML 程式碼中,複選框具有一個 input 標籤,其 type 屬性的值為 checkbox。
Cypress 命令
與複選框相關的 Cypress 命令如下:
用於點選所有複選框的命令如下:
cy.get('input[type="checkbox"]').check()
用於點選 id 為 check 的複選框的命令如下:
cy.get('#chk').check()
用於點選值為 Cypress 的複選框的命令如下:
cy.get('input[type="checkbox"]').check('Cypress')
用於點選值為 Java 和 Python 的複選框的命令如下:
cy.get('input[type="checkbox"]').check(['Java','Python'])
用於點選值為 Java 並帶有選項的複選框的命令如下:
cy.get('.chk').check('Java', options)
用於點選值為 Java 和 Python 並帶有選項的複選框的命令如下:
cy.get('input[type="checkbox"]').check(['Java','Python'], options)
用於點選 class 為 check 並帶有選項的複選框的命令如下:
cy.get('.chk').check({force : true})
用於取消選中所有複選框的命令如下:
cy.get('input[type="checkbox"]').uncheck()
用於取消選中 id 為 check 的複選框的命令如下:
cy.get('#chk').uncheck()
用於取消選中值為 Cypress 的複選框的命令如下:
cy.get('input[type="checkbox"]').uncheck('Cypress')
用於取消選中值為 Java 和 Python 的複選框的命令如下:
cy.get('input[type="checkbox"]').uncheck(['Java','Python'])
用於取消選中值為 Java 並帶有選項的複選框的命令如下:
cy.get('.chk').uncheck('Java', options)
用於取消選中值為 Java 和 Python 的複選框並帶有選項的命令如下:
cy.get('input[type="checkbox"]').uncheck(['Java','Python'], options)
用於取消選中 class 為 check 並帶有選項的複選框的命令如下:
cy.get('.chk').uncheck({force : true)
Cypress 中的選項
Cypress 中可用的選項如下:
log – 預設值 – true – 用於開啟/關閉控制檯日誌。
timeout – 預設值 – defaultCommandTimeout(4000ms) – 用於設定在丟擲錯誤之前的最大等待時間。
force – 預設值 – false – 用於強制執行操作。
scrollBehaviour – 預設值 – scrollBehaviour(top) – 用於設定在命令執行前視口滾動到的元素位置。
waitForAnimations – 預設值 – waitForAnimations(true) – 用於等待元素完成動畫後再執行命令。
animationDistanceThreshold - 預設值 – animationDistanceThreshold (5) – 用於設定元素應超過的畫素距離才能被視為動畫。
check/uncheck 命令都需要與產生 DOM 元素的命令鏈式呼叫,並且可以對這些命令應用斷言。
Cypress 命令的實現
下面解釋了 Cypress 中命令的實現:
// test suite describe('Tutorialspoint', function () { // it function to identify test it('Scenario 1', function (){ // test step to launch a URL cy.visit("https://#/signup") //checkbox with assertion cy.get('input[type="checkbox"]').check().should('be.checked') //identify checkbox with class with assertion cy.get('.VfPpkd-muHVFf-bMcfAe').uncheck().should('not.be.checked') }) })
執行結果
輸出如下:

以上結果顯示,“顯示密碼”左側的複選框首先使用 check 命令選中(使用斷言 should 驗證)。
然後,它使用 uncheck 命令取消選中(也使用斷言 should 驗證)。