Skip to content

Commit 2a31b74

Browse files
committed
fix(android): enhance scrolling methods to support startPoint parameter and improve swipe duration handling
1 parent 70ffb6a commit 2a31b74

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
@@ -521,11 +521,14 @@ ${Object.keys(size)
521521
get mouse() {
522522
return {
523523
click: (x: number, y: number) => this.mouseClick(x, y),
524-
wheel: (deltaX: number, deltaY: number) =>
525-
this.mouseWheel(deltaX, deltaY),
524+
wheel: (deltaX: number, deltaY: number, duration?: number) =>
525+
this.mouseWheel(deltaX, deltaY, duration),
526526
move: (x: number, y: number) => this.mouseMove(x, y),
527-
drag: (from: { x: number; y: number }, to: { x: number; y: number }) =>
528-
this.mouseDrag(from, to),
527+
drag: (
528+
from: { x: number; y: number },
529+
to: { x: number; y: number },
530+
duration?: number,
531+
) => this.mouseDrag(from, to, duration),
529532
};
530533
}
531534

@@ -579,30 +582,76 @@ ${Object.keys(size)
579582
return '';
580583
}
581584

582-
async scrollUntilTop(): Promise<void> {
585+
async scrollUntilTop(startPoint?: Point): Promise<void> {
586+
if (startPoint) {
587+
const { height } = await this.size();
588+
const start = { x: startPoint.left, y: startPoint.top };
589+
const end = { x: start.x, y: height };
590+
591+
await repeat(defaultScrollUntilTimes, () =>
592+
this.mouseDrag(start, end, defaultFastScrollDuration),
593+
);
594+
await sleep(1000);
595+
return;
596+
}
597+
583598
await repeat(defaultScrollUntilTimes, () =>
584-
this.mouseWheel(0, 9999999, defaultFastScrollDuration),
599+
this.mouseWheel(0, -9999999, defaultFastScrollDuration),
585600
);
586601
await sleep(1000);
587602
}
588603

589-
async scrollUntilBottom(): Promise<void> {
604+
async scrollUntilBottom(startPoint?: Point): Promise<void> {
605+
if (startPoint) {
606+
const start = { x: startPoint.left, y: startPoint.top };
607+
const end = { x: start.x, y: 0 };
608+
609+
await repeat(defaultScrollUntilTimes, () =>
610+
this.mouseDrag(start, end, defaultFastScrollDuration),
611+
);
612+
await sleep(1000);
613+
return;
614+
}
615+
590616
await repeat(defaultScrollUntilTimes, () =>
591-
this.mouseWheel(0, -9999999, defaultFastScrollDuration),
617+
this.mouseWheel(0, 9999999, defaultFastScrollDuration),
592618
);
593619
await sleep(1000);
594620
}
595621

596-
async scrollUntilLeft(): Promise<void> {
622+
async scrollUntilLeft(startPoint?: Point): Promise<void> {
623+
if (startPoint) {
624+
const { width } = await this.size();
625+
const start = { x: startPoint.left, y: startPoint.top };
626+
const end = { x: width, y: start.y };
627+
628+
await repeat(defaultScrollUntilTimes, () =>
629+
this.mouseDrag(start, end, defaultFastScrollDuration),
630+
);
631+
await sleep(1000);
632+
return;
633+
}
634+
597635
await repeat(defaultScrollUntilTimes, () =>
598-
this.mouseWheel(9999999, 0, defaultFastScrollDuration),
636+
this.mouseWheel(-9999999, 0, defaultFastScrollDuration),
599637
);
600638
await sleep(1000);
601639
}
602640

603-
async scrollUntilRight(): Promise<void> {
641+
async scrollUntilRight(startPoint?: Point): Promise<void> {
642+
if (startPoint) {
643+
const start = { x: startPoint.left, y: startPoint.top };
644+
const end = { x: 0, y: start.y };
645+
646+
await repeat(defaultScrollUntilTimes, () =>
647+
this.mouseDrag(start, end, defaultFastScrollDuration),
648+
);
649+
await sleep(1000);
650+
return;
651+
}
652+
604653
await repeat(defaultScrollUntilTimes, () =>
605-
this.mouseWheel(-9999999, 0, defaultFastScrollDuration),
654+
this.mouseWheel(9999999, 0, defaultFastScrollDuration),
606655
);
607656
await sleep(1000);
608657
}
@@ -613,13 +662,13 @@ ${Object.keys(size)
613662

614663
if (startPoint) {
615664
const start = { x: startPoint.left, y: startPoint.top };
616-
const endY = Math.max(0, start.y - scrollDistance);
665+
const endY = Math.min(height, start.y + scrollDistance);
617666
const end = { x: start.x, y: endY };
618667
await this.mouseDrag(start, end);
619668
return;
620669
}
621670

622-
await this.mouseWheel(0, scrollDistance);
671+
await this.mouseWheel(0, -scrollDistance);
623672
}
624673

625674
async scrollDown(distance?: number, startPoint?: Point): Promise<void> {
@@ -628,13 +677,13 @@ ${Object.keys(size)
628677

629678
if (startPoint) {
630679
const start = { x: startPoint.left, y: startPoint.top };
631-
const endY = Math.min(height, start.y + scrollDistance);
680+
const endY = Math.max(0, start.y - scrollDistance);
632681
const end = { x: start.x, y: endY };
633682
await this.mouseDrag(start, end);
634683
return;
635684
}
636685

637-
await this.mouseWheel(0, -scrollDistance);
686+
await this.mouseWheel(0, scrollDistance);
638687
}
639688

640689
async scrollLeft(distance?: number, startPoint?: Point): Promise<void> {
@@ -643,13 +692,13 @@ ${Object.keys(size)
643692

644693
if (startPoint) {
645694
const start = { x: startPoint.left, y: startPoint.top };
646-
const endX = Math.max(0, start.x - scrollDistance);
695+
const endX = Math.min(width, start.x + scrollDistance);
647696
const end = { x: endX, y: start.y };
648697
await this.mouseDrag(start, end);
649698
return;
650699
}
651700

652-
await this.mouseWheel(scrollDistance, 0);
701+
await this.mouseWheel(-scrollDistance, 0);
653702
}
654703

655704
async scrollRight(distance?: number, startPoint?: Point): Promise<void> {
@@ -658,13 +707,13 @@ ${Object.keys(size)
658707

659708
if (startPoint) {
660709
const start = { x: startPoint.left, y: startPoint.top };
661-
const endX = Math.min(width, start.x + scrollDistance);
710+
const endX = Math.max(0, start.x - scrollDistance);
662711
const end = { x: endX, y: start.y };
663712
await this.mouseDrag(start, end);
664713
return;
665714
}
666715

667-
await this.mouseWheel(-scrollDistance, 0);
716+
await this.mouseWheel(scrollDistance, 0);
668717
}
669718

670719
private async ensureYadb() {
@@ -773,20 +822,26 @@ ${Object.keys(size)
773822
private async mouseDrag(
774823
from: { x: number; y: number },
775824
to: { x: number; y: number },
825+
duration?: number,
776826
): Promise<void> {
777827
const adb = await this.getAdb();
778828

779829
// Use adjusted coordinates
780830
const { x: fromX, y: fromY } = this.adjustCoordinates(from.x, from.y);
781831
const { x: toX, y: toY } = this.adjustCoordinates(to.x, to.y);
782832

783-
await adb.shell(`input swipe ${fromX} ${fromY} ${toX} ${toY} 300`);
833+
// Ensure duration has a default value
834+
const swipeDuration = duration ?? 300;
835+
836+
await adb.shell(
837+
`input swipe ${fromX} ${fromY} ${toX} ${toY} ${swipeDuration}`,
838+
);
784839
}
785840

786841
private async mouseWheel(
787842
deltaX: number,
788843
deltaY: number,
789-
duration = defaultNormalScrollDuration,
844+
duration?: number,
790845
): Promise<void> {
791846
const { width, height } = await this.size();
792847

@@ -808,8 +863,11 @@ ${Object.keys(size)
808863
deltaY = Math.max(-maxNegativeDeltaY, Math.min(deltaY, maxPositiveDeltaY));
809864

810865
// Calculate the end coordinates
811-
const endX = startX + deltaX;
812-
const endY = startY + deltaY;
866+
// Note: For swipe, we need to reverse the delta direction
867+
// because positive deltaY should scroll up (show top content),
868+
// which requires swiping from bottom to top (decreasing Y)
869+
const endX = startX - deltaX;
870+
const endY = startY - deltaY;
813871

814872
// Adjust coordinates to fit device ratio
815873
const { x: adjustedStartX, y: adjustedStartY } = this.adjustCoordinates(
@@ -823,9 +881,12 @@ ${Object.keys(size)
823881

824882
const adb = await this.getAdb();
825883

884+
// Ensure duration has a default value
885+
const swipeDuration = duration ?? defaultNormalScrollDuration;
886+
826887
// Execute the swipe operation
827888
await adb.shell(
828-
`input swipe ${adjustedStartX} ${adjustedStartY} ${adjustedEndX} ${adjustedEndY} ${duration}`,
889+
`input swipe ${adjustedStartX} ${adjustedStartY} ${adjustedEndX} ${adjustedEndY} ${swipeDuration}`,
829890
);
830891
}
831892

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)