Khu*_*shi 6 testing reactjs e2e-testing puppeteer
我在应用程序中反应了Dnd(拖放)。我想为此进行端到端测试。
我要模拟的是将特定数据拖放到特定位置。如何启用此功能?
我所拥有的是:
test.js
const mouse = page.mouse;
await mouse.down();
await mouse.move(126, 19);
await page.waitFor(400);
Run Code Online (Sandbox Code Playgroud)
使用此代码选择已完成。但拖动不起作用。我应该如何实施呢?
Gra*_*ler 14
以下方法将允许您在 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();
Run Code Online (Sandbox Code Playgroud)
Puppeteer 的特定解决方案都不适合我,所以我最终将原生 javascript 写入文件并将其导入 Puppeteer(在我的情况下为 Jest)。
拖放.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);
}
Run Code Online (Sandbox Code Playgroud)
测试.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)
});
});
Run Code Online (Sandbox Code Playgroud)
比内置的 Puppeteer 功能稍微多一些工作,但希望这是一个足够简单的复制和粘贴解决方案,适用于需要更多控制拖放的任何人。
这是我在 puppeteer 中用于拖放的片段:
// 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 function dragAndDrop(page, originSelector, destinationSelector) {
const origin = await page.waitForSelector(originSelector)
const destination = await page.waitForSelector(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()
}
Run Code Online (Sandbox Code Playgroud)
请注意,puppeteer 中对拖放的支持尚未完全实现。
| 归档时间: |
|
| 查看次数: |
3427 次 |
| 最近记录: |