forked from sclevine/agouti
/
selector.go
86 lines (73 loc) · 1.71 KB
/
selector.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
package target
import (
"fmt"
"github.com/sclevine/agouti/api"
)
type Type string
const (
CSS Type = "CSS: %s"
XPath Type = "XPath: %s"
Link Type = `Link: "%s"`
Label Type = `Label: "%s"`
Button Type = `Button: "%s"`
Name Type = `Name: "%s"`
A11yID Type = "Accessibility ID: %s"
AndroidAut Type = "Android UIAut.: %s"
IOSAut Type = "iOS UIAut.: %s"
Class Type = "Class: %s"
ID Type = "ID: %s"
labelXPath = `//input[@id=(//label[normalize-space()="%s"]/@for)] | //label[normalize-space()="%[1]s"]/input`
buttonXPath = `//input[@type="submit" or @type="button"][normalize-space(@value)="%s"] | //button[normalize-space()="%[1]s"]`
)
func (t Type) format(value string) string {
return fmt.Sprintf(string(t), value)
}
type Selector struct {
Type Type
Value string
Index int
Indexed bool
Single bool
}
func (s Selector) String() string {
var suffix string
if s.Single {
suffix = " [single]"
} else if s.Indexed {
suffix = fmt.Sprintf(" [%d]", s.Index)
}
return s.Type.format(s.Value) + suffix
}
func (s Selector) API() api.Selector {
return api.Selector{Using: s.apiType(), Value: s.value()}
}
func (s Selector) apiType() string {
switch s.Type {
case CSS:
return "css selector"
case Class:
return "class name"
case ID:
return "id"
case Link:
return "link text"
case Name:
return "name"
case A11yID:
return "accessibility id"
case AndroidAut:
return "-android uiautomator"
case IOSAut:
return "-ios uiautomation"
}
return "xpath"
}
func (s Selector) value() string {
switch s.Type {
case Label:
return fmt.Sprintf(labelXPath, s.Value)
case Button:
return fmt.Sprintf(buttonXPath, s.Value)
}
return s.Value
}