Cypress.io-使用JavaScript window.prompt

时间:2019-02-18 09:07:46

标签: javascript testing integration-testing e2e-testing cypress

我是赛普拉斯的新手,但有问题。

在应用程序中,我有一个“保存按钮”。当您单击按钮时,将触发JS window.prompt()。在提示符内,输入保存名称(如“从John保存”),然后单击“确定”-在提示符内。

我需要通过Cypress.io的E2E测试来介绍这个用户故事。

问题是,当我单击“保存按钮”时,在显示提示时,赛普拉斯冻结了Click()事件。

it('Make a new save with JS prompt', () => {
   cy.get('#save-changes-button')
   .should('be.visible')
   .click() //here the prompt is displayed, cypress stop and wait for click() event finish
})

我可以寻求帮助吗?我没有在Cypress.io Docs或其他地方找到合适的解决方案。

2 个答案:

答案 0 :(得分:0)

好的,事实上,当前的Cypress.io版本不支持与window事件的交互。作弊的方法是cy.stub()方法。

这是我的解决方案。情况:

  1. 单击GUI中的“保存按钮”。
  2. window.prompt()已打开。
  3. 在提示中输入保存名称(例如“ Thomas已保存”)。
  4. 在提示中单击“确定”并保存值。

还有赛普拉斯中的代码:

    it('Save the value inside Prompt', () => {
            cy.window().then(win => {
                cy.stub(win, 'prompt').returns('The value you write inside prompt')
                cy.get('#save-changes-in-gui-button').click();
                //... Saved value assert
            })
    })

默认情况下,通过存根按下Cypress更改window.pompt()事件,并模拟单击“确定”。该解决方案现在对我有效。希望它可以帮助其他人:)

答案 1 :(得分:0)

HTML边:

<button id="createDirectory" (click)="createDirectory()">Create</button>

createDirectory()函数提示输入目录名称

JAVASCRIPT面:

createDirectory() {
  const x = prompt('Enter directory name');
  console.log(x);
}

如何在赛普拉斯中实现这一目标?

it('Creating new directory level 1', () => {
    cy.window().then(win => {
        /* In case of writing something in prompt, use stub and then click on button on which prompt opens */
        cy.stub(win, 'prompt').returns('level1');
        cy.get('button#createDirectory').click();
    });
});
  • level1:我们将在提示框中输入的字符串