保存值并以后在命令链中使用

发布于 2025-02-09 10:17:00 字数 323 浏览 2 评论 0原文

我正在测试一个拖动方案,想要获得元素位置并在Mousemove中使用该值。我虽然cypress.env()是保存值的一种方法,但它在Mousemove参数中显示为空。如何保存初始数据?

cy.get('.draggable')
  .then($el => { Cypress.env('start', $el[0].getBoundingClientRect()) })
  .trigger('mousemove', , { pageX: Cypress.env('start').x +20 })

I'm testing a drag scenario, want to get an elements position and use the value in a mousemove. I though that Cypress.env() was a way to save values, but it appears empty in the mousemove parameters. How do I save the initial data?

cy.get('.draggable')
  .then($el => { Cypress.env('start', $el[0].getBoundingClientRect()) })
  .trigger('mousemove', , { pageX: Cypress.env('start').x +20 })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞笑我太脆弱 2025-02-16 10:17:00

测试命令在运行测试时采用其值,但是命令稍后运行(异步),因此cypress.env('start') 在排队时尚未定义。

您可以添加一个。然后()延迟排队命令,它将读取最新值。

cy.get('.draggable')
  .then($el => { Cypress.env('start', $el[0].getBoundingClientRect()) })
  .then($el => {
    cy.wrap($el).trigger('mousemove', { pageX: Cypress.env('start').x +20 })
  })

The test commands take their values as the test is run, but the commands run later (asynchronously), so Cypress.env('start') is not yet defined when the mousemove command is queued.

You can add a .then() to delay queueing that command, and it will read the latest value.

cy.get('.draggable')
  .then($el => { Cypress.env('start', $el[0].getBoundingClientRect()) })
  .then($el => {
    cy.wrap($el).trigger('mousemove', { pageX: Cypress.env('start').x +20 })
  })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文