1

大家早上好,我已经尝试了几天来执行一个在 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 坐标。

4

1 回答 1

2

我不确定你到底在尝试什么,但如果你的目标是通过赛普拉斯提供一般的拖放操作,那么有一个 npm 包 npm install --save-dev cypress-file-upload

你应该将它导入到 cypress command.js

详细信息在https://www.npmjs.com/package/cypress-file-upload中进行了说明,并且还有一个拖放示例。

我如何实现的一个例子是:

it("should upload a file", () => {
        cy.fixture('afile.withanextension','base64').then(file => {
            cy.get('input') // or how you find that element
                .upload({
                            file,
                            fileName: 'afile.withanextension',
                            mimeType: 'propermimetype' // e.g. image/jpg
                        },
                        {
                            uploadType:'input'
                        }
            )
        });
    })
于 2019-05-16T13:15:14.247 回答