
- 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 - 獲取和釋出
- Cypress - 檔案上傳
- Cypress - 資料驅動測試
- Cypress - 提示彈出視窗
- Cypress - 儀表板
- Cypress - 螢幕截圖和影片
- Cypress - 除錯
- Cypress - 自定義命令
- Cypress - Fixture
- Cypress - 環境變數
- Cypress - Hook
- Cypress - JSON 檔案配置
- Cypress - 報告
- Cypress - 外掛
- Cypress - GitHub
- Cypress 有用資源
- Cypress - 快速指南
- Cypress - 有用資源
- Cypress - 討論
Cypress - 提示彈出視窗
Cypress 可以處理提示彈出視窗,使用者可以在其中輸入值。提示視窗包含一個文字欄位,用於獲取輸入。要處理提示彈出視窗,可以使用 cy.window() 方法。
它獲取提示(遠端視窗)物件的值。在確認/警報彈出視窗中,我們必須觸發瀏覽器事件。但是對於提示彈出視窗,我們必須使用 cy.stub() 方法。
示例
讓我們看下面的示例,點選“點選獲取 JS 提示”按鈕後,會顯示一個提示彈出視窗,如下所示:

顯示以下帶有使用者輸入欄位的提示。在提示彈出視窗中輸入 Tutorialspoint,如下所示。

您輸入 - Tutorialspoint 顯示在“結果”下方。
這可以在下面顯示的螢幕中看到:

實現
下面是 Cypress 中顯示提示彈出視窗的命令實現:
describe('Tutorialspoint Test', function () { // test case it("Scenario 1", function () { //URL launch cy.visit("https://the-internet.herokuapp.com/javascript_alerts") //handling prompt alert cy.window().then(function(p){ //stubbing prompt window cy.stub(p, "prompt").returns("Tutorialspoint"); // click on Click for JS Prompt button cy.get(':nth-child(3) > button').click() // verify application message on clicking on OK cy.get('#result').contains('You entered: Tutorialspoint') }); }); });
執行結果
輸出如下:

輸出日誌顯示文字驗證成功。
您輸入 - Tutorialspoint,在點選提示彈出視窗上的“確定”按鈕後生成。此外,應用於提示視窗的存根在輸出日誌中可見。
廣告