大家早上好,我已经尝试了几天来执行一个在 Selenium 中执行拖放事件的 e2e 测试,经过几种方法解决这个问题,有人告诉我关于 Cypress,所以,我在这里尝试学习这些新东西。
问题是,如何使用 Cypress 从某个元素中获取 x 位置和 y 位置?在 GitHub 问题中,有人使用了此功能
function moveElement(element, x, y) {
cy.get(`${element}`)
.trigger('mousedown', { which: 1 })
.trigger('mousemove', { clientX: x, clientY: y })
.trigger('mouseup', { force: true })
}
但是,我不认为直接插入 x 和 y 坐标是最好的工作方式,所以,在 Selenium 中我得到了类似的东西
int getXCoordinate(WebElement){
location = WebElement.getLocation();
x = location.getX();
return X;
}
有没有办法编写类似的函数?
更新
对于那些对此感兴趣的人,cypress 运行普通的 ES6,所以你可以使用
element.left+window.scrollX
检索 X 坐标和
element.top+window.scrollY
检索 Y 坐标。