cypress.io-使用javascript窗口保存值



我是柏树的新手,我有问题。

i该应用程序,我有一个"保存按钮"。单击按钮时,将触发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或其他地方没有找到可疑的解决方案。

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

这是我的解决方案。场景:

  1. 单击GUI中的"保存按钮"。
  2. window.prompt()开放。
  3. 将保存名称(例如"托马斯保存")在提示中。
  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
            })
    })

柏树默认情况下通过存根更改window.pompt()事件,然后模拟单击"确定"。现在,该解决方案对我有用。希望,它可以帮助别人:)

html side:

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

createirectory()函数目录名称的提示

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:我们将在提示盒中写入的字符串

最新更新