最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to chain cy.get in cypress - Stack Overflow

programmeradmin1浏览0评论

I'm trying to get to #1 element then get to #2 element to click on #3 element.

but I'm having a trouble with getting the correct CSS selector in Cypress.

How to write a test script for this?

I've tried cy.get('.ui.active.visible.button.floating.dropdown.fr-dropdown').contains('Delete yield').click() but doesn't work.

Is there a way to get #1 first, then #2 to reach #3? this is not a real code but something like this.

cy.get('.yield-event__date-and-text--container').contains('10kg')
cy.get('.ui.active.visible.button.floating.dropdown.fr-dropdown').click()
cy.get('.item.fr-dropdown__option').contains('Delete yield').click()

Thanks a lot in advance

I'm trying to get to #1 element then get to #2 element to click on #3 element.

but I'm having a trouble with getting the correct CSS selector in Cypress.

How to write a test script for this?

I've tried cy.get('.ui.active.visible.button.floating.dropdown.fr-dropdown').contains('Delete yield').click() but doesn't work.

Is there a way to get #1 first, then #2 to reach #3? this is not a real code but something like this.

cy.get('.yield-event__date-and-text--container').contains('10kg')
cy.get('.ui.active.visible.button.floating.dropdown.fr-dropdown').click()
cy.get('.item.fr-dropdown__option').contains('Delete yield').click()

Thanks a lot in advance

Share Improve this question edited Dec 14, 2020 at 4:26 Richard Matsen 23.5k3 gold badges55 silver badges84 bronze badges asked Nov 10, 2020 at 5:41 user3601310user3601310 8732 gold badges12 silver badges19 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

You can write something like this using find():

cy.get('.yield-event__date-and-text--container').contains('10kg')
cy.get('.yield-event__date-and-text--container').find('i[class*="fr-dropdown__icon"]').click({force: true})
cy.get('.yield-event__date-and-text--container').find('Delete yield').click({force: true})

As @RosenMihaylov says, you might find it easier to use the Cypress "relationship" commands which follow the HTML structure, rather than CSS selectors.

Also I think you would need two clicks - one to open the menu and the second to invoke the delete action.

Step 1 - looks like the text devEnv_admin identifies the card you want

cy.contains('div', 'devEnv_admin')

which gives you the 7th div down.

Step 2 - the dropdown you need to click is 2nd sibling to the above

.siblings('div.note-event__dropdown')  // list of siblings matching the selector
.eq(0)                                 // take the first (and only)

which gives you the parent of the dropdown button.

Step 3 - but it looks like the child of that which has class button might have the click event handler (you might have to experiment here because elements with event handlers are sometimes difficult to find)

.children('div.button')   // list of children matching the selector
.eq(0)                    // take the first (and only)
.click();

which should open the menu, but it might take some milliseconds to appear

Step 4 - wait for the div with the text you need

cy.contains('span', 'Delete yield')   // this form of .contains() waits for the text
  .click();

In summary,

cy.contains('div', 'devEnv_admin')
  .siblings('div.note-event__dropdown')  // list of siblings matching the selector
  .eq(0)                                 // take the first (and only)
  .children('div.button')                // list of children matching the selector
  .eq(0)                                 // take the first (and only)
  .click();

cy.contains('span', 'Delete yield')   // this form of .contains() waits for the text
  .click();

There are other paths through the DOM elements and other selectors you can use, like .next() and .parent().

A lot depends on where the event handlers are attached, which is easiest to find out by looking at the source code.


Alternatively, using within()

cy.contains('.yield-event__date-and-text--container', 'devEnv_admin')  // pick the card
  .within(() => {  // restricts commands below to this card
    cy.get('div.button.dropdown').click();
    cy.contains('span', 'Delete yield').click();
  });

Cypress docs show 3 ways Children or Find or Within.

Personally I use .within, when I want to work in the scope of the element and .find - when I want to be able to work both within the scope and outside.

发布评论

评论列表(0)

  1. 暂无评论