Skip to content

Commit e7352c6

Browse files
committed
fix(android): enhance scrolling methods to support startPoint parameter and improve swipe duration handling
1 parent 6f9ae97 commit e7352c6

File tree

2 files changed

+93
-29
lines changed

2 files changed

+93
-29
lines changed

packages/android/src/page/index.ts

Lines changed: 86 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -471,11 +471,14 @@ ${Object.keys(size)
471471
get mouse() {
472472
return {
473473
click: (x: number, y: number) => this.mouseClick(x, y),
474-
wheel: (deltaX: number, deltaY: number) =>
475-
this.mouseWheel(deltaX, deltaY),
474+
wheel: (deltaX: number, deltaY: number, duration?: number) =>
475+
this.mouseWheel(deltaX, deltaY, duration),
476476
move: (x: number, y: number) => this.mouseMove(x, y),
477-
drag: (from: { x: number; y: number }, to: { x: number; y: number }) =>
478-
this.mouseDrag(from, to),
477+
drag: (
478+
from: { x: number; y: number },
479+
to: { x: number; y: number },
480+
duration?: number,
481+
) => this.mouseDrag(from, to, duration),
479482
};
480483
}
481484

@@ -529,30 +532,76 @@ ${Object.keys(size)
529532
return '';
530533
}
531534

532-
async scrollUntilTop(): Promise<void> {
535+
async scrollUntilTop(startPoint?: Point): Promise<void> {
536+
if (startPoint) {
537+
const { height } = await this.size();
538+
const start = { x: startPoint.left, y: startPoint.top };
539+
const end = { x: start.x, y: height };
540+
541+
await repeat(defaultScrollUntilTimes, () =>
542+
this.mouseDrag(start, end, defaultFastScrollDuration),
543+
);
544+
await sleep(1000);
545+
return;
546+
}
547+
533548
await repeat(defaultScrollUntilTimes, () =>
534-
this.mouseWheel(0, 9999999, defaultFastScrollDuration),
549+
this.mouseWheel(0, -9999999, defaultFastScrollDuration),
535550
);
536551
await sleep(1000);
537552
}
538553

539-
async scrollUntilBottom(): Promise<void> {
554+
async scrollUntilBottom(startPoint?: Point): Promise<void> {
555+
if (startPoint) {
556+
const start = { x: startPoint.left, y: startPoint.top };
557+
const end = { x: start.x, y: 0 };
558+
559+
await repeat(defaultScrollUntilTimes, () =>
560+
this.mouseDrag(start, end, defaultFastScrollDuration),
561+
);
562+
await sleep(1000);
563+
return;
564+
}
565+
540566
await repeat(defaultScrollUntilTimes, () =>
541-
this.mouseWheel(0, -9999999, defaultFastScrollDuration),
567+
this.mouseWheel(0, 9999999, defaultFastScrollDuration),
542568
);
543569
await sleep(1000);
544570
}
545571

546-
async scrollUntilLeft(): Promise<void> {
572+
async scrollUntilLeft(startPoint?: Point): Promise<void> {
573+
if (startPoint) {
574+
const { width } = await this.size();
575+
const start = { x: startPoint.left, y: startPoint.top };
576+
const end = { x: width, y: start.y };
577+
578+
await repeat(defaultScrollUntilTimes, () =>
579+
this.mouseDrag(start, end, defaultFastScrollDuration),
580+
);
581+
await sleep(1000);
582+
return;
583+
}
584+
547585
await repeat(defaultScrollUntilTimes, () =>
548-
this.mouseWheel(9999999, 0, defaultFastScrollDuration),
586+
this.mouseWheel(-9999999, 0, defaultFastScrollDuration),
549587
);
550588
await sleep(1000);
551589
}
552590

553-
async scrollUntilRight(): Promise<void> {
591+
async scrollUntilRight(startPoint?: Point): Promise<void> {
592+
if (startPoint) {
593+
const start = { x: startPoint.left, y: startPoint.top };
594+
const end = { x: 0, y: start.y };
595+
596+
await repeat(defaultScrollUntilTimes, () =>
597+
this.mouseDrag(start, end, defaultFastScrollDuration),
598+
);
599+
await sleep(1000);
600+
return;
601+
}
602+
554603
await repeat(defaultScrollUntilTimes, () =>
555-
this.mouseWheel(-9999999, 0, defaultFastScrollDuration),
604+
this.mouseWheel(9999999, 0, defaultFastScrollDuration),
556605
);
557606
await sleep(1000);
558607
}
@@ -563,13 +612,13 @@ ${Object.keys(size)
563612

564613
if (startPoint) {
565614
const start = { x: startPoint.left, y: startPoint.top };
566-
const endY = Math.max(0, start.y - scrollDistance);
615+
const endY = Math.min(height, start.y + scrollDistance);
567616
const end = { x: start.x, y: endY };
568617
await this.mouseDrag(start, end);
569618
return;
570619
}
571620

572-
await this.mouseWheel(0, scrollDistance);
621+
await this.mouseWheel(0, -scrollDistance);
573622
}
574623

575624
async scrollDown(distance?: number, startPoint?: Point): Promise<void> {
@@ -578,13 +627,13 @@ ${Object.keys(size)
578627

579628
if (startPoint) {
580629
const start = { x: startPoint.left, y: startPoint.top };
581-
const endY = Math.min(height, start.y + scrollDistance);
630+
const endY = Math.max(0, start.y - scrollDistance);
582631
const end = { x: start.x, y: endY };
583632
await this.mouseDrag(start, end);
584633
return;
585634
}
586635

587-
await this.mouseWheel(0, -scrollDistance);
636+
await this.mouseWheel(0, scrollDistance);
588637
}
589638

590639
async scrollLeft(distance?: number, startPoint?: Point): Promise<void> {
@@ -593,13 +642,13 @@ ${Object.keys(size)
593642

594643
if (startPoint) {
595644
const start = { x: startPoint.left, y: startPoint.top };
596-
const endX = Math.max(0, start.x - scrollDistance);
645+
const endX = Math.min(width, start.x + scrollDistance);
597646
const end = { x: endX, y: start.y };
598647
await this.mouseDrag(start, end);
599648
return;
600649
}
601650

602-
await this.mouseWheel(scrollDistance, 0);
651+
await this.mouseWheel(-scrollDistance, 0);
603652
}
604653

605654
async scrollRight(distance?: number, startPoint?: Point): Promise<void> {
@@ -608,13 +657,13 @@ ${Object.keys(size)
608657

609658
if (startPoint) {
610659
const start = { x: startPoint.left, y: startPoint.top };
611-
const endX = Math.min(width, start.x + scrollDistance);
660+
const endX = Math.max(0, start.x - scrollDistance);
612661
const end = { x: endX, y: start.y };
613662
await this.mouseDrag(start, end);
614663
return;
615664
}
616665

617-
await this.mouseWheel(-scrollDistance, 0);
666+
await this.mouseWheel(scrollDistance, 0);
618667
}
619668

620669
private async ensureYadb() {
@@ -723,20 +772,26 @@ ${Object.keys(size)
723772
private async mouseDrag(
724773
from: { x: number; y: number },
725774
to: { x: number; y: number },
775+
duration?: number,
726776
): Promise<void> {
727777
const adb = await this.getAdb();
728778

729779
// Use adjusted coordinates
730780
const { x: fromX, y: fromY } = this.adjustCoordinates(from.x, from.y);
731781
const { x: toX, y: toY } = this.adjustCoordinates(to.x, to.y);
732782

733-
await adb.shell(`input swipe ${fromX} ${fromY} ${toX} ${toY} 300`);
783+
// Ensure duration has a default value
784+
const swipeDuration = duration ?? 300;
785+
786+
await adb.shell(
787+
`input swipe ${fromX} ${fromY} ${toX} ${toY} ${swipeDuration}`,
788+
);
734789
}
735790

736791
private async mouseWheel(
737792
deltaX: number,
738793
deltaY: number,
739-
duration = defaultNormalScrollDuration,
794+
duration?: number,
740795
): Promise<void> {
741796
const { width, height } = await this.size();
742797

@@ -758,8 +813,11 @@ ${Object.keys(size)
758813
deltaY = Math.max(-maxNegativeDeltaY, Math.min(deltaY, maxPositiveDeltaY));
759814

760815
// Calculate the end coordinates
761-
const endX = startX + deltaX;
762-
const endY = startY + deltaY;
816+
// Note: For swipe, we need to reverse the delta direction
817+
// because positive deltaY should scroll up (show top content),
818+
// which requires swiping from bottom to top (decreasing Y)
819+
const endX = startX - deltaX;
820+
const endY = startY - deltaY;
763821

764822
// Adjust coordinates to fit device ratio
765823
const { x: adjustedStartX, y: adjustedStartY } = this.adjustCoordinates(
@@ -773,9 +831,12 @@ ${Object.keys(size)
773831

774832
const adb = await this.getAdb();
775833

834+
// Ensure duration has a default value
835+
const swipeDuration = duration ?? defaultNormalScrollDuration;
836+
776837
// Execute the swipe operation
777838
await adb.shell(
778-
`input swipe ${adjustedStartX} ${adjustedStartY} ${adjustedEndX} ${adjustedEndY} ${duration}`,
839+
`input swipe ${adjustedStartX} ${adjustedStartY} ${adjustedEndX} ${adjustedEndY} ${swipeDuration}`,
779840
);
780841
}
781842

packages/android/tests/ai/setting.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ describe(
1616
});
1717

1818
await agent.launch('com.android.settings/.Settings');
19-
2019
await agent.aiAction('scroll list to bottom');
2120
await agent.aiAction('open "More settings"');
22-
await agent.aiAction('scroll list to bottom');
21+
await agent.aiAction('scroll left until left edge');
22+
await agent.aiAction('scroll right until right edge');
2323
await agent.aiAction('scroll list to top');
24-
await agent.aiAction('swipe down one screen');
25-
await agent.aiAction('swipe up one screen');
24+
await agent.aiAction('scroll list to bottom');
25+
await agent.aiAction('scroll down one screen');
26+
await agent.aiAction('scroll up one screen');
27+
await agent.aiAction('scroll right one screen');
28+
await agent.aiAction('scroll left one screen');
2629
});
2730
},
2831
360 * 1000,

0 commit comments

Comments
 (0)