- 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 - Web 表格
- Cypress - 滑鼠操作
- Cypress - Cookie
- Cypress - GET 和 Post
- Cypress - 檔案上傳
- Cypress - 資料驅動測試
- Cypress - 提示彈出視窗
- Cypress - 儀表板
- Cypress - 螢幕截圖和影片
- Cypress - 除錯
- Cypress - 自定義命令
- Cypress - 裝置
- Cypress - 環境變數
- Cypress - 掛鉤
- Cypress - JSON 檔案設定
- Cypress - 報告
- Cypress - 外掛
- Cypress - GitHub
- Cypress 有用資源
- Cypress - 快速指南
- Cypress - 有用資源
- Cypress - 討論
Cypress - 變數
在 Cypress 中,使用 var、let 和 const 等變數。在使用閉包時,我們可以使用在沒有賦值的情況下獲取的物件。但是,在我們使用可變物件時,情況並非如此。
當物件修改其特徵時,我們可能需要將它的舊值與它的新值進行比較。
程式碼實現
我們可以透過使用以下命令來進行程式碼實現 −
cy.get('.btn').then(($span) => {
// value capture before button click and stored in const
const n = parseInt($span.text())
cy.get('b').click().then(() => {
// value capture after button click and stored in const
const m = parseInt($span.text())
// comparison
expect(n).to.eq(m)
})
})
在以上情況下,我們使用 const 變數,因為物件 $span 正在發生變化。在處理可變物件及其值時,建議使用 const 型別的變數。
廣告