Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - Cypress.io-使用JavaScript window.prompt保存值(Cypress.io - save value with JavaScript window.prompt)

I am new in Cypress and I have a problem.(我是赛普拉斯的新手,但有问题。)

I the app, I have a "Save Button".(在应用程序中,我有一个“保存按钮”。)

When you click the button, it is triggered JS window.prompt() .(当您单击按钮时,将触发JS window.prompt() 。) Inside the prompt you write the name of the save (like "Save from John") and click to "OK" - inside the prompt.(在提示符内,输入保存名称(如“从John保存”),然后单击“确定”-在提示符内。)

I need to cover this user story by the E2E test in Cypress.io.(我需要通过Cypress.io的E2E测试来介绍这个用户故事。)

Problem is, when I click the "Save button", cypress freeze in Click() event, when the prompt is displayed.(问题是,当我单击“保存按钮”时,当显示提示时,柏树会在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
})

May I ask for some help?(我可以寻求帮助吗?)

I did not found siutable solution in Cypress.io Docs or elsewhere.(我在Cypress.io Docs或其他地方没有找到合适的解决方案。)   ask by bares697 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, fact is, the current version of the Cypress.io does not support interaction with window events.(好的,事实是,当前的Cypress.io版本不支持与window事件的交互。)

The way how to cheat it is the cy.stub() method.(欺骗方法是cy.stub()方法。)

This is my solution.(这是我的解决方案。)

The scenario:(场景:)
  1. Click the "Save Button" in GUI.(单击GUI中的“保存按钮”。)
  2. window.prompt() is opened.(window.prompt()已打开。)
  3. Write the save name (like "Saved by Thomas") into the prompt.(在提示中输入保存名称(如“ Thomas已保存”)。)
  4. Click "OK" in prompt and save the value.(在提示中单击“确定”并保存值。)

And the code in Cypress:(赛普拉斯中的代码:)

    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 by default change window.pompt() event by the stub and simulate click "OK".(赛普拉斯默认情况下通过存根更改window.pompt()事件并模拟单击“确定”。)

This solution works for me now.(该解决方案现在对我有效。) Hope, it could help someone else :)(希望,它可以帮助别人:))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...