如何使用柏树在测试自动化中处理窗口提示?

发布于 2025-02-13 17:08:40 字数 1711 浏览 1 评论 0原文

我是使用柏树进行Web自动化的新手。我仍在互联网上寻找答案,但我找不到适合我的解决方案。

这是我在测试中要做的事情:

  • 用户单击链接。
  • 打开一个新选项卡并出现Windows提示符,请求用户输入(用户名,密码)。
    (由于Cypress不允许打开新选项卡,因此我删除了target属性。)
  • 成功登录后,该页面具有下载按钮。
  • 用户单击下载按钮。

第一次斗争 - 我无法将值输入Windows提示。在以下代码中,我试图查看Windows提示符上的“登录”按钮是否会单击,但事实并非如此。

cy.window().then(win => {
    cy.get('@documentPassword').then((finalPassword) => {
        const stub =cy.stub(win, 'prompt')
        stub.returns('test')
        cy.get('button#signin').click()
    })
})

我遇到了一个断言错误:在25000ms之后重试的时间:期望找到元素:button#signin,但从未找到它。

对此不运气,我继续提出另一个建议。


第二次斗争 - 我尝试将用户名和密码放入链接中,例如:https://username: [email  procected]。只是要注意,当我手动将链接粘贴到浏览器中时,它可以起作用。为了测试这一点,这是我所做的:

cy.visit('https://mailtrap.io')
// ...other steps
cy.forceVisit('https://username:[email protected]')

我在commands.js文件中添加了一个自定义命令 forcevisit :

Cypress.Commands.add('forceVisit', url => {
    cy.window().then(win => {
        return win.open(url, '_self'); 
      });
});

结果是第二个URL不加载。

希望你们有任何见解。提前致谢。

I am new to using Cypress for web automation. I am still scouring through the internet looking for answers to this but I cannot find a solution that works for me.

This is what I'm trying to do in my test:

  • User clicks a link.
  • A new tab is opened and a windows prompt appears, requesting user input (username, password).
    (Since Cypress doesn't allow opening new tabs, I've removed the target attribute.)
  • Upon logging in successfully, the page has a download button.
  • User clicks on the download button.

The first struggle - I could not enter values into the windows prompt. In the below code, I was trying to see if the 'Sign In' button on the windows prompt would be clicked, but it was not.

cy.window().then(win => {
    cy.get('@documentPassword').then((finalPassword) => {
        const stub =cy.stub(win, 'prompt')
        stub.returns('test')
        cy.get('button#signin').click()
    })
})

I got an Assertion Error: Timed out retrying after 25000ms: Expected to find element: button#signin, but never found it.

After no luck with this, I moved on to another suggestion.


The second struggle - I tried putting the username and password into the link, like this: https://username:[email protected]. Just to note, when I paste the link manually into a browser, it works. To test this out, this what I had done:

cy.visit('https://mailtrap.io')
// ...other steps
cy.forceVisit('https://username:[email protected]')

I added a custom command forceVisit to the commands.js file:

Cypress.Commands.add('forceVisit', url => {
    cy.window().then(win => {
        return win.open(url, '_self'); 
      });
});

The result is the second url does not load.

Hoping for any insight from you guys. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笏见 2025-02-20 17:08:40

这对我有用:

cy.visit('https://mytestingwebsite.com', {
  auth: {
   username: 'username',
   password: 'password'
  }
})

我第一次尝试时对我不起作用,因为我仍然通过URL中的凭据。

This works for me:

cy.visit('https://mytestingwebsite.com', {
  auth: {
   username: 'username',
   password: 'password'
  }
})

This didn't work for me the first time I tried it because I still passed the credentials in the url.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文