How to check if an jQuery event is bound to an element from a cypress test

Dan

I am testing some code that uses jQuery to bind click events, and I am having a problem with the test running before the click event is bound.

I want to avoid putting arbitrary waits in the test, so I was trying to find some way to check if the event is bound, and then I can wait for this before making test assertions.

I came up with this super dirty approach in the console:

$._data($('.element-being-tested')[0], 'events' ).click[0].handler.toString().includes('something I know is in the function')

But I cant transfer this to cypress, because Cypress.$._data() doesn't exist.

How do check if a query event is bound?

Richard Matsen

Instead of directly testing that the event exists, you could use Cypress retry-ability to keep testing the event's affect, something like

cy.get('.element-that-changes-after-event').then($target => {
  // use closure to get a reference to changing element

  cy.get('.element-being-tested')
    .should($clickable => {       // .should(cb) callback function will be retried

      $clickable.click()
      expect($target.text()).to.equal('Content that appears after click event')

    });

});

See Assert class name contains heading-

There may be a simpler way to do this, please post your test.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do you check the equality of the inner text of a element using cypress?

分類Dev

How can I check an element for multiple CSS classes in Cypress?

分類Dev

Cypress Test: finds no <li> element

分類Dev

How can I animate bound data from an event trigger?

分類Dev

Why does this dynamically created element not bound to an event?

分類Dev

How to check whether an element is Present inside another element or not in Jquery?

分類Dev

How to test that an element is randomly selected from a list?

分類Dev

How to stub contentwindow.print with cypress/test printJS with cypress

分類Dev

How to fill a rectangle with an image that is coming from a bound listbox on event fire in WP7

分類Dev

Cypress, how to check for text typed into a field?

分類Dev

How to select an svg element with title using cypress?

分類Dev

How to check if a bound method is the same as unbound

分類Dev

How to check if one element is exist under a form in jQuery

分類Dev

How to dynamically check if an element has mulitple class using jquery or javascript

分類Dev

How to prepend html into on page element from HTML onClick event?

分類Dev

jQuery - check if variable is dom element

分類Dev

Check if element has no children with jquery

分類Dev

jquery check the attribute element value

分類Dev

check if there is a new element using jquery

分類Dev

jquery : How to bind change event when the input is not from the user

分類Dev

How to get selector on which 'click' event has been bound?

分類Dev

how to check if period sign(.) is removed from the word in textbox in jQuery?

分類Dev

JQuery - Keep the same event [mouseenter] in a nested element

分類Dev

jQuery element stored with `click` event not working as required

分類Dev

jQuery - Add click event to a dynamically created element

分類Dev

jQuery event affecting only this instance of the element

分類Dev

how to select from $('[name="test"]') table result in jQuery

分類Dev

How to check the test coverage in java?

分類Dev

How to check that last test is failed?

Related 関連記事

  1. 1

    How do you check the equality of the inner text of a element using cypress?

  2. 2

    How can I check an element for multiple CSS classes in Cypress?

  3. 3

    Cypress Test: finds no <li> element

  4. 4

    How can I animate bound data from an event trigger?

  5. 5

    Why does this dynamically created element not bound to an event?

  6. 6

    How to check whether an element is Present inside another element or not in Jquery?

  7. 7

    How to test that an element is randomly selected from a list?

  8. 8

    How to stub contentwindow.print with cypress/test printJS with cypress

  9. 9

    How to fill a rectangle with an image that is coming from a bound listbox on event fire in WP7

  10. 10

    Cypress, how to check for text typed into a field?

  11. 11

    How to select an svg element with title using cypress?

  12. 12

    How to check if a bound method is the same as unbound

  13. 13

    How to check if one element is exist under a form in jQuery

  14. 14

    How to dynamically check if an element has mulitple class using jquery or javascript

  15. 15

    How to prepend html into on page element from HTML onClick event?

  16. 16

    jQuery - check if variable is dom element

  17. 17

    Check if element has no children with jquery

  18. 18

    jquery check the attribute element value

  19. 19

    check if there is a new element using jquery

  20. 20

    jquery : How to bind change event when the input is not from the user

  21. 21

    How to get selector on which 'click' event has been bound?

  22. 22

    how to check if period sign(.) is removed from the word in textbox in jQuery?

  23. 23

    JQuery - Keep the same event [mouseenter] in a nested element

  24. 24

    jQuery element stored with `click` event not working as required

  25. 25

    jQuery - Add click event to a dynamically created element

  26. 26

    jQuery event affecting only this instance of the element

  27. 27

    how to select from $('[name="test"]') table result in jQuery

  28. 28

    How to check the test coverage in java?

  29. 29

    How to check that last test is failed?

ホットタグ

アーカイブ