我已在我的应用程序中做出反应Dnd(拖放)。我想为此进行e2e测试。
我想要模拟的是拖动特定数据并放到特定位置。我如何启用此功能?
我拥有的是:
test.js
const mouse = page.mouse;
await mouse.down();
await mouse.move(126, 19);
await page.waitFor(400);
使用此代码选择已完成。但拖动不起作用。我该如何实现呢?
答案 0 :(得分:0)
以下方法将允许您在Puppeteer中模拟拖放动作:
const example = await page.$( '#example' );
const bounding_box = await example.boundingBox();
await page.mouse.move( bounding_box.x + bounding_box.width / 2, bounding_box.y + bounding_box.height / 2 );
await page.mouse.down();
await page.mouse.move( 126, 19 );
await page.mouse.up();
答案 1 :(得分:0)
这是我用来拖放木偶的片段:
//This assumes only one element will be found for selectors you provide, otherwise there's ambiguity on which element was selected.
//In my code I'm throwing on more than 1 element found
async dragAndDrop(page, originSelector, destinationSelector) {
await page.waitFor(originSelector)
await page.waitFor(destinationSelector)
const origin = await page.$(originSelector)
const destination = await page.$(destinationSelector)
const ob = await origin.boundingBox()
const db = await destination.boundingBox()
console.log(`Dragging from ${ob.x + ob.width / 2}, ${ob.y + ob.height / 2}`)
await page.mouse.move(ob.x + ob.width / 2, ob.y + ob.height / 2)
await page.mouse.down()
console.log(`Dropping at ${db.x + db.width / 2}, ${db.y + db.height / 2}`)
await page.mouse.move(db.x + db.width / 2, db.y + db.height / 2)
await page.mouse.up()
}
请注意,not fully仍支持对操纵up的拖放操作。
答案 2 :(得分:0)
Puppeteer的任何特定解决方案都不适合我,因此我最终将本机javascript写入文件并将其导入Puppeteer(在我的情况下为Jest)。
drag-and-drop.js
async function dragAndDrop(source, target) {
await page.evaluate((source, target) => {
source = document.querySelector('#'+source);
event = document.createEvent("CustomEvent");
event.initCustomEvent("mousedown", true, true, null);
event.clientX = source.getBoundingClientRect().top;
event.clientY = source.getBoundingClientRect().left;
source.dispatchEvent(event);
event = document.createEvent("CustomEvent");
event.initCustomEvent("dragstart", true, true, null);
event.clientX = source.getBoundingClientRect().top;
event.clientY = source.getBoundingClientRect().left;
source.dispatchEvent(event);
event = document.createEvent("CustomEvent");
event.initCustomEvent("drag", true, true, null);
event.clientX = source.getBoundingClientRect().top;
event.clientY = source.getBoundingClientRect().left;
source.dispatchEvent(event);
target = document.querySelector('#'+target);
event = document.createEvent("CustomEvent");
event.initCustomEvent("dragover", true, true, null);
event.clientX = target.getBoundingClientRect().top;
event.clientY = target.getBoundingClientRect().left;
target.dispatchEvent(event);
event = document.createEvent("CustomEvent");
event.initCustomEvent("drop", true, true, null);
event.clientX = target.getBoundingClientRect().top;
event.clientY = target.getBoundingClientRect().left;
target.dispatchEvent(event);
event = document.createEvent("CustomEvent");
event.initCustomEvent("dragend", true, true, null);
event.clientX = target.getBoundingClientRect().top;
event.clientY = target.getBoundingClientRect().left;
target.dispatchEvent(event);
}, source, target);
}
test.js
const dragAndDrop = require('./drag-and-drop')
describe('when dragging and dropping todo', () => {
it('should change order on DOM', async () => {
const firstTodo = await page.evaluate(() => document.querySelectorAll('.input-container .input')[0].id);
const secondTodo = await page.evaluate(() => document.querySelectorAll('.input-container .input')[1].id);
dragAndDrop(firstTodo, secondTodo);
const newFirstTodo = await page.evaluate(() => document.querySelectorAll('.input-container .input')[0].id);
const newSecondTodo = await page.evaluate(() => document.querySelectorAll('.input-container .input')[1].id);
expect(newFirstTodo).toEqual(secondTodo)
expect(newSecondTodo).toEqual(firstTodo)
});
});
比内置的Puppeteer功能要多得多的工作,但希望对于其他需要更多控制拖放的人来说,这是一个简单易行的复制和粘贴解决方案。