简体   繁体   English

在不调用 click 命令的情况下使用 Cypress 单击按钮?

[英]Click a button with Cypress without invoking the click command?

I'm trying to automate a page within an application that has two buttons on a page.我正在尝试在一个页面上有两个按钮的应用程序中自动化一个页面。 One is enabled and can be clicked.一个是启用的,可以点击。 The other is disabled on the first load.另一个在第一次加载时被禁用。 But once the first button is pressed, a tab new browser tab is opened and then the second button is enabled.但是一旦按下第一个按钮,就会打开一个选项卡新浏览器选项卡,然后启用第二个按钮。 Then the user can click that and go onto a form.然后用户可以点击它和 go 到一个表单上。

Obviously Cypress can't be handling new tabs, but is there a way I can invoke the click command without actually calling the event through a click?显然赛普拉斯不能处理新标签,但是有没有一种方法可以调用点击命令,而无需通过点击实际调用事件?

I have tried cy.get(".button1").trigger("click") but a new tab is still opened.我已经尝试过cy.get(".button1").trigger("click")但仍然打开了一个新选项卡。

One solution I guess is to break it into two tests.我想一种解决方案是将其分为两个测试。 But is there a better way?但是有更好的方法吗?

Personally, I would test this not by clicking on the button, but by checking to see if it is clickable (ie does not have attribute disabled).就个人而言,我不会通过单击按钮来测试它,而是通过检查它是否可单击(即没有禁用属性)。 If you're using event listeners and are testing in Chrome , you can use getEventListeners() to check whether the event listener exists, or you can check any onclick/ng-click attributes you might have set on it.如果您正在使用事件侦听器并在 Chrome中进行测试,您可以使用getEventListeners()来检查事件侦听器是否存在,或者您可以检查您可能在其上设置的任何onclick/ng-click属性。

If you do want to click the button to test the event, but actually not open the new tab, just find out how the tab is opened (like window.open(..., '_blank') ), and monkeypatch it just for that test.如果您确实想单击按钮来测试事件,但实际上并没有打开新标签,只需找出标签是如何打开的(如window.open(..., '_blank') ),然后对其进行猴子补丁那个测试。

you can do some sort of action like this depending on the properties of the Button you have.您可以根据您拥有的 Button 的属性执行某种类似的操作。 This code below is making by button active again without clicking it from disabled state.下面的代码通过按钮再次激活,而不是从禁用的 state 中单击它。

eg例如

 cy.get('#btnSubmit').invoke("removeAttr","disabled")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM