我们现在开始工作了。我们需要使用自定义函数。
似乎可能是由于我们的元素有圆角,没有到达0,0位置,所以我们需要尝试找到元素的中间位置。 p>
因此,这是我们需要做的。
await I.dragDown(
{
css:
'#myAssessments > main > div > * > div:nth-child(2) > div:nth-child(1)',
},
200,
);
async dragDown(locator, pixels) {
await this.dragInDirection('down', locator, pixels);
}
async dragInDirection(direction, locator, pixels) {
const queryString = resolveLocator(locator);
const location = await this.getElementLocation(queryString);
const x = parseInt(location.x + location.width / 2, 10);
const y = parseInt(location.y + location.height / 2, 10);
await this.performBrowserActions([
createActionSet({ x, y }, direction, pixels),
]);
}
function resolveLocator(locator) {
let queryString;
if (locator.dataTestId) {
queryString = `[data-testid=${locator.dataTestId}]`;
} else if (locator.test) {
queryString = `[data-test-id=${locator.test}]`;
} else if (locator.css) {
queryString = locator.css;
} else {
queryString = locator;
}
return queryString;
}
function createActionSet(origin, direction, length) {
const { x, y } = origin;
let xMoveTo = 0;
let yMoveTo = 0;
switch (direction) {
case 'up':
yMoveTo = -length;
break;
case 'down':
yMoveTo = length;
break;
case 'right':
xMoveTo = length;
break;
case 'left':
xMoveTo = -length;
break;
default:
return new Error('Direction passed was wrong or no direction passed');
}
return {
type: 'pointer',
id: 'finger1',
parameters: { pointerType: 'touch' },
actions: [
{ type: 'pointerMove', duration: 0, x, y },
{ type: 'pointerDown', button: 0 },
{ type: 'pause', duration: 500 },
{
type: 'pointerMove',
duration: 1000,
origin: 'pointer',
x: xMoveTo,
y: yMoveTo,
},
{ type: 'pointerUp', button: 0 },
],
};
}
如果不使用自定义定位器,您也许可以简化并这样做:
async dragInDirection(direction, locator, pixels) {
const location = await this.getElementLocation(locator);
const x = parseInt(location.x + location.width / 2, 10);
const y = parseInt(location.y + location.height / 2, 10);
await this.performBrowserActions([
createActionSet({ x, y }, direction, pixels),
]);
}
这是带有键盘标签的版本:
await I.pressKey('Tab');
await I.pressKey('Tab'); // Tabbing to focus the card
await I.pressKey('Space'); // "Jump" item
await I.pressKey('ArrowDown'); // Move item
await I.pressKey('Space'); // Drop item