简体   繁体   English

cy.wrap(JQueryObject).click() 与 JQueryObject.click()

[英]cy.wrap(JQueryObject).click() vs JQueryObject.click()

Hi can anyone explain to me a little bit why I can not just use element.click() on line cy.wrap(element).click() ?嗨,谁能向我解释一下为什么我不能在cy.wrap(element).click()线上使用element.click() cy.wrap(element).click() what is the benefit to use cy.wrap for it?使用 cy.wrap 有什么好处?

  cy.get(selector)
    .scrollIntoView()
    .each((element) => {
      if (element.text() === 'click me') {
        cy.wrap(element).click();
      }
    });

The short answer is because it will show up nice in the cypress logs and you will gain confidence about the user interactions with that element (as opposed to machine interaction).简短的回答是因为它会很好地显示在 cypress 日志中,并且您将对用户与该元素的交互(而不是机器交互)充满信心。

I guess that in this specific case you can view it as the difference between invoking the onclick handler manually and having a bot click a virtual mouse on the interactable element, the latter being much closer to the human interaction, resulting in more confidence.我想在这种特定情况下,您可以将其视为手动调用onclick处理程序和让机器人在可交互元素上单击虚拟鼠标之间的区别,后者更接近于人类交互,从而产生更大的信心。

You might find this cumbersome, but 90% of cases you won't even need to use cy.wrap because you can reach the wrapped element directly.您可能会觉得这很麻烦,但在 90% 的情况下,您甚至不需要使用cy.wrap因为您可以直接访问被包装的元素。

This snippet should do the job I reckon, I would also skip scrollIntoView() .这个片段应该可以完成我认为的工作,我也会跳过scrollIntoView() If this is this only element with "click me" on the page, I would supply just the text, without a selector.如果这是页面上唯一带有“单击我”的元素,我将只提供文本,而不提供选择器。

cy.contains(selector, 'click me')
  .scrollIntoView()
  .click()

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

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