feat(driver): add EraseText to the device driver surface

This commit is contained in:
pj committed 2026-06-05 17:40:09 +05:30
1 parent 72055e5d9f
commit 41511a2a32
4 files changed
+49 -13

No files matched your search

+17
View File
@@ -193,6 +193,23 @@ func (d *Driver) InputText(_ context.Context, text string) error {
)
}
// EraseText clears the focused field. InputText above already replaces via
// select-all, so the character count is not needed to bound the deletion.
func (d *Driver) EraseText(_ context.Context, _ int) error {
return chromedp.Run(d.tabCtx,
chromedp.ActionFunc(func(ctx context.Context) error {
if err := chromedp.Evaluate(`
(function() {
const el = document.activeElement;
if (el && typeof el.select === 'function') el.select();
})()`, nil).Do(ctx); err != nil {
return err
}
return input.InsertText("").Do(ctx)
}),
)
}
func (d *Driver) Swipe(_ context.Context, fromX, fromY, toX, toY int, duration time.Duration) error {
millis := max(duration.Milliseconds(), 50)
script := fmt.Sprintf(`
+4
View File
@@ -20,6 +20,10 @@ type DeviceDriver interface {
DoubleTap(ctx context.Context, x, y int) error
DoubleTapSelector(ctx context.Context, selector string) error
InputText(ctx context.Context, text string) error
// EraseText deletes characterCount characters from the focused field.
// The runner calls it before InputText so the verb replaces existing
// content instead of appending to it.
EraseText(ctx context.Context, characterCount int) error
Swipe(ctx context.Context, fromX, fromY, toX, toY int, duration time.Duration) error
PressKey(ctx context.Context, key string) error
LongPress(ctx context.Context, x, y int) error
+23 -13
View File
@@ -19,6 +19,7 @@ const (
ActionDoubleTap ActionKind = "double_tap"
ActionDoubleTapSelector ActionKind = "double_tap_selector"
ActionInputText ActionKind = "input_text"
ActionEraseText ActionKind = "erase_text"
ActionSwipe ActionKind = "swipe"
ActionPressKey ActionKind = "press_key"
ActionLongPress ActionKind = "long_press"
@@ -32,19 +33,20 @@ const (
)
type Action struct {
Kind ActionKind
BundleID string
ClearState bool
X, Y int
FromX, FromY int
ToX, ToY int
Duration time.Duration
Selector string
Text string
Key string
LogLevel string
LogSince time.Time
Idle time.Duration
Kind ActionKind
BundleID string
ClearState bool
X, Y int
FromX, FromY int
ToX, ToY int
Duration time.Duration
Selector string
Text string
CharacterCount int
Key string
LogLevel string
LogSince time.Time
Idle time.Duration
}
// Driver is an in-memory Driver implementation for unit tests.
@@ -210,6 +212,14 @@ func (d *Driver) InputText(ctx context.Context, text string) error {
return nil
}
func (d *Driver) EraseText(ctx context.Context, characterCount int) error {
if err := d.failure(ActionEraseText); err != nil {
return err
}
d.record(Action{Kind: ActionEraseText, CharacterCount: characterCount})
return nil
}
func (d *Driver) Swipe(ctx context.Context, fromX, fromY, toX, toY int, duration time.Duration) error {
if err := d.failure(ActionSwipe); err != nil {
return err
+5
View File
@@ -137,6 +137,11 @@ func (c *Client) InputText(ctx context.Context, text string) error {
return err
}
func (c *Client) EraseText(ctx context.Context, characterCount int) error {
_, err := c.stub.EraseText(ctx, &driverpb.EraseTextRequest{CharacterCount: int32(characterCount)})
return err
}
func (c *Client) Swipe(ctx context.Context, fromX, fromY, toX, toY int, duration time.Duration) error {
_, err := c.stub.Swipe(ctx, &driverpb.SwipeRequest{
From: &driverpb.Point{X: int32(fromX), Y: int32(fromY)},