io-使用JavaScript window.prompt保存值
我是新来的柏树,我有一个问题 当我打开应用程序时,我有一个“保存按钮”。单击按钮时,会触发JSio-使用JavaScript window.prompt保存值,javascript,testing,integration-testing,e2e-testing,cypress,Javascript,Testing,Integration Testing,E2e Testing,Cypress,我是新来的柏树,我有一个问题 当我打开应用程序时,我有一个“保存按钮”。单击按钮时,会触发JSwindow.prompt()。在提示符内,您写下保存的名称(如“从John处保存”),然后在提示符内单击“确定” 我需要通过Cypress.io中的E2E测试来介绍这个用户故事 问题是,当我单击“保存按钮”时,当显示提示时,在click()事件中单击cypress freeze it('Make a new save with JS prompt', () => { cy.get('#sa
window.prompt()
。在提示符内,您写下保存的名称(如“从John处保存”),然后在提示符内单击“确定”
我需要通过Cypress.io中的E2E测试来介绍这个用户故事
问题是,当我单击“保存按钮”时,当显示提示时,在click()事件中单击cypress freeze
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文档或其他地方找不到可用的解决方案。好的,事实是,Cypress.io的当前版本不支持与
窗口
事件的交互。如何欺骗它的方法是cy.stub()
方法
这是我的解决方案。情景:
window.prompt()
打开 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
})
})
在默认情况下,通过存根点击change
window.pompt()
event并单击“确定”。这个解决方案现在对我有效。希望,它可以帮助其他人:)HTML方面:
<button id="createDirectory" (click)="createDirectory()">Create</button>
如何在cypress中实现这一点?
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:我们将在提示框中写入的字符串
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();
});
});