javascript - 使用 Electron/Webdriver 拖放
问题描述
我正在尝试为测试拖放功能的电子应用程序编写测试。我尝试了两种不同的方法。Webdriver 的原生 dragAndDrop 方法以及 performActions。然而,在这两种情况下,似乎什么都没有发生。如果我在使用应用程序时手动拖动它,该元素似乎不会移动,即使它可以正常工作。我目前正在使用:
“react-beautiful-dnd”:“^13.1.0”,“electron”:“^9.4.3”,“electron-chromedriver”:“^11.0.0”,“mocha”:“^8.2.1”, “反应”:“^16.4.0”,“selenium-webdriver”:“^4.0.0-beta.1”,“spectron”:“^11.0.0”,“spectron-keys”:“0.0.1” ,
这是我使用执行操作时测试的样子:
it('should display the correct string when the rows are dragged and dropped in a different order', async () => {
const correctString = '"Field3": Value3^4 & "Field1": Value1^2 & "Field2": Value2^3'
const target = await app.client.$('[id="search_input"]');
const sourceElement = await app.client.$('[id="row_1"]');
//function to get coordinates of the source element and the target.
const getCoordsForElement = async (elementId) => {
const rect = await app.client.getElementRect(elementId);
const X = parseInt(rect.x + (rect.width / 2), 10);
const Y = parseInt(rect.y + (rect.height / 2), 10);
return [X, Y];
};
const [sourceX, sourceY] = await getCoordsForElement(sourceElement.elementId)
const [targetX, targetY] = await getCoordsForElement(target.elementId)
const [diffX, diffY] = [targetX - sourceX, targetY - sourceY]
await app.client.performActions([{
type: 'pointer',
id: 'finger1',
parameters: { pointerType: 'mouse' },
actions: [
{ type: 'pointerMove', duration: 0, x:sourceX , y:sourceY },
{ type: 'pointerDown', button: 1 },
{ type: 'pause', duration: 500 },
{ type: 'pointerMove', duration: 1000, origin: "pointer", x: diffX, y: diffY },
{ type: 'pointerUp', button: 1 }
]
}]);
await app.client.pause(1000)
const text = await searchInput.getValue()
console.log("TEXT: ", text)
assert.strictEqual(text, correctString, `Expected the correct string to be "${correctString}". Instead it was "${text}".`)
})
以下是我尝试使用 drapAndDrop 方法时的测试结果:
it('should display the correct string when the rows are dragged and dropped in a different order', async () => {
const correctString = '"Field3": Value3^4 & "Field1": Value1^2 & "Field2": Value2^3'
const target = await app.client.$('[id="search_input"]');
const sourceElement= await app.client.$('[id="row_1"]');
await sourceElement.dragAndDrop({x: -247, y: -210})
await app.client.pause(1000)
const text = await searchInput.getValue()
console.log("TEXT: ", text)
assert.strictEqual(text, correctString, `Expected the correct string to be "${correctString}". Instead it was "${text}".`)
})
解决方案
推荐阅读
- javascript - 管道无效 url 时节点请求模块服务器崩溃
- javascript - Use React Portals within the React-App (not outside the DOM)
- javascript - jquery 适用于 Firefox、Edge,但不适用于 Chrome
- php - 有没有办法根据一定数量生成一定数量的 Woocommerce 优惠券?
- arrays - 有没有办法在没有 usinf for 循环的情况下取消嵌套和取消列出 MongoDb 查询内联?
- bootstrap-select - 如何在 Bootstrap-Select 中禁用/启用 Select?
- swift - 如何在 NSCellTextView Swift 5 中不截断文本
- node.js - 如何从 nodejs 中运行和启动 mongodb
- xamarin - Xamarin Forms 未在 Android 中显示警报但在 ios 中工作正常
- rust - 哪些 Rust 1.2 容器支持 trait 对象?