简体   繁体   中英

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.

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.

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.

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.

you can do some sort of action like this depending on the properties of the Button you have. This code below is making by button active again without clicking it from disabled state.

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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