/
touchaction.go
187 lines (159 loc) · 4.42 KB
/
touchaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package appium
import (
"fmt"
"strings"
"github.com/sclevine/agouti"
"github.com/sclevine/agouti/api"
"github.com/sclevine/agouti/api/mobile"
"github.com/sclevine/agouti/internal/element"
"github.com/sclevine/agouti/internal/target"
)
type TouchAction struct {
actions []action
session mobileSession
}
type action struct {
mobile.Action
elements elementRepository
}
func NewTouchAction(session mobileSession) *TouchAction {
return &TouchAction{
session: session,
}
}
func (a *action) Elements() (out []string) {
if a.elements == nil {
return
}
for _, sel := range a.elements.(*element.Repository).Selectors {
out = append(out, sel.String())
}
return out
}
func (a *action) String() string {
out := []string{}
opts := a.Options
if a.elements != nil {
els := a.Elements()
if len(els) != 0 {
out = append(out, fmt.Sprintf(`element=%q`, els))
}
}
if opts.X != 0 {
out = append(out, fmt.Sprintf("x=%d", opts.X))
}
if opts.Y != 0 {
out = append(out, fmt.Sprintf("y=%d", opts.Y))
}
if opts.Millisecond != 0 {
out = append(out, fmt.Sprintf("ms=%d", opts.Millisecond))
}
if opts.Count != 0 {
out = append(out, fmt.Sprintf("count=%d", opts.Count))
}
if opts.Duration != 0 {
out = append(out, fmt.Sprintf("duration=%d", opts.Duration))
}
return fmt.Sprintf("%s(%s)", a.Action.Action, strings.Join(out, ", "))
}
func (t *TouchAction) append(actionObj mobile.Action, selectors agouti.Selectors) *TouchAction {
newAction := action{
Action: actionObj,
}
if selectors != nil {
newAction.elements = &element.Repository{Client: t.session, Selectors: selectors.(target.Selectors)}
}
touchAction := NewTouchAction(t.session)
touchAction.actions = append(t.actions, newAction)
return touchAction
}
func (t *TouchAction) TapElement(selection *agouti.Selection, count int) *TouchAction {
action := mobile.Action{
Action: "tap",
Options: mobile.ActionOptions{Count: count},
}
return t.append(action, selection.Selectors())
}
func (t *TouchAction) TapPosition(x, y, count int) *TouchAction {
action := mobile.Action{
Action: "tap",
Options: mobile.ActionOptions{Count: count, X: x, Y: y},
}
return t.append(action, nil)
}
func (t *TouchAction) PressPosition(x, y int) *TouchAction {
action := mobile.Action{
Action: "press",
Options: mobile.ActionOptions{X: x, Y: y},
}
return t.append(action, nil)
}
func (t *TouchAction) PressElement(selection *agouti.Selection) *TouchAction {
action := mobile.Action{Action: "press"}
return t.append(action, selection.Selectors())
}
func (t *TouchAction) LongPressPosition(x, y, duration int) *TouchAction {
action := mobile.Action{
Action: "longPress",
Options: mobile.ActionOptions{
X: x,
Y: y,
Duration: duration,
},
}
return t.append(action, nil)
}
func (t *TouchAction) LongPressElement(selection *agouti.Selection, duration int) *TouchAction {
action := mobile.Action{
Action: "longPress",
Options: mobile.ActionOptions{Duration: duration},
}
return t.append(action, selection.Selectors())
}
func (t *TouchAction) Release() *TouchAction {
action := mobile.Action{Action: "release"}
return t.append(action, nil)
}
func (t *TouchAction) Wait(ms int) *TouchAction {
action := mobile.Action{
Action: "wait",
Options: mobile.ActionOptions{Millisecond: ms},
}
return t.append(action, nil)
}
func (t *TouchAction) MoveToPosition(x, y int) *TouchAction {
action := mobile.Action{
Action: "moveTo",
Options: mobile.ActionOptions{X: x, Y: y},
}
return t.append(action, nil)
}
func (t *TouchAction) MoveToElement(selection *agouti.Selection) *TouchAction {
action := mobile.Action{Action: "moveTo"}
return t.append(action, selection.Selectors())
}
func (t *TouchAction) Perform() error {
var actions []mobile.Action
for _, action := range t.actions {
// resolve elements if present
if action.elements != nil {
selectedElement, err := action.elements.GetExactlyOne()
if err != nil {
return fmt.Errorf("failed to retrieve element for selection %q: %s", action.Elements(), err)
}
action.Options.Element = selectedElement.(*api.Element).ID
}
actions = append(actions, action.Action)
}
if err := t.session.PerformTouch(actions); err != nil {
return fmt.Errorf("error performing touch actions '%s': %s", t, err)
}
return nil
}
func (ma *TouchAction) String() string {
var actions []string
for _, act := range ma.actions {
actions = append(actions, act.String())
}
return strings.Join(actions, " -> ")
}