Cypress 简明教程
Cypress - Prompt Pop-up Window
Cypress 可以处理提示弹出窗口,用户可以在其中输入值。提示有一个文本字段,其中输入是采用。要处理提示弹出窗口,请使用 cy.window() 方法。
Cypress can handle prompt pop-up windows, where users can input values. A prompt has a text field, where the input is taken. To handle a prompt pop-up, cy.window() method is used.
获取提示符对象的值(远程窗口)。在确认/警告弹出窗口中,我们必须触发浏览器事件。但对于提示弹出窗口,我们必须使用 cy.stub() 方法。
It obtains the value of the object of the prompt (remote window). In a confirmation/alert pop-up, we have to fire a browser event. But for prompt pop-up, we have to use cy.stub() method.
Example
让我们看一下下面的示例,单击单击 JS 提示按钮时,会显示一个提示弹出窗口,如下所示−
Let us look at the below example, on clicking the Click for JS Prompt button, a prompt pop up gets displayed, as shown below −

随即显示带用户输入字段的提示。如您所见,Tutorialspoint 已输入在提示弹出窗口中,如下所示。
The following prompt with the user input field gets displayed. Tutorialspoint is entered in the prompt pop-up, as shown below.

您输入的 − Tutorialspoint 显示在结果下方。
You entered − Tutorialspoint gets displayed under Result.
您可以在以下所示的屏幕中看到这一点−
This can be seen in the screen displayed below −

Implementation
下面是 Cypress 中用于显示提示弹出窗口的命令的实现 −
Given below is an implementation of the commands for displaying prompt pop-up windows in 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')
});
});
});
Execution Results
Execution Results
输出如下 −
The output is as follows −

输出日志显示成功验证了文本内容。
The output logs show the successful verification of the text.
You entered − Tutorialspoint ,是单击提示弹出窗口中的确定按钮时产生的。另外,应用于提示窗口的存根在输出日志中可见。
You entered − Tutorialspoint, is produced on clicking OK button on prompt pop up. Also, the stub applied on the prompt window is visible on the output log.