-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
locator_helpers.go
133 lines (116 loc) · 3.66 KB
/
locator_helpers.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
package playwright
import (
"encoding/json"
"fmt"
"regexp"
"strings"
)
func convertRegexp(reg *regexp.Regexp) (pattern, flags string) {
matches := regexp.MustCompile(`\(\?([imsU]+)\)(.+)`).FindStringSubmatch(reg.String())
if len(matches) == 3 {
pattern = matches[2]
flags = matches[1]
} else {
pattern = reg.String()
}
return
}
func escapeForAttributeSelector(text interface{}, exact bool) string {
switch text := text.(type) {
case *regexp.Regexp:
return escapeRegexForSelector(text)
default:
suffix := "i"
if exact {
suffix = "s"
}
return fmt.Sprintf(`"%s"%s`, strings.Replace(strings.Replace(text.(string), `\`, `\\`, -1), `"`, `\"`, -1), suffix)
}
}
func escapeForTextSelector(text interface{}, exact bool) string {
switch text := text.(type) {
case *regexp.Regexp:
return escapeRegexForSelector(text)
default:
if exact {
return fmt.Sprintf(`%ss`, escapeText(text.(string)))
}
return fmt.Sprintf(`%si`, escapeText(text.(string)))
}
}
func escapeRegexForSelector(re *regexp.Regexp) string {
pattern, flag := convertRegexp(re)
return fmt.Sprintf(`/%s/%s`, strings.ReplaceAll(pattern, `>>`, `\>\>`), flag)
}
func escapeText(s string) string {
builder := &strings.Builder{}
encoder := json.NewEncoder(builder)
encoder.SetEscapeHTML(false)
_ = encoder.Encode(s)
return strings.TrimSpace(builder.String())
}
func getByAltTextSelector(text interface{}, exact bool) string {
return getByAttributeTextSelector("alt", text, exact)
}
func getByAttributeTextSelector(attrName string, text interface{}, exact bool) string {
return fmt.Sprintf(`internal:attr=[%s=%s]`, attrName, escapeForAttributeSelector(text, exact))
}
func getByLabelSelector(text interface{}, exact bool) string {
return fmt.Sprintf(`internal:label=%s`, escapeForTextSelector(text, exact))
}
func getByPlaceholderSelector(text interface{}, exact bool) string {
return getByAttributeTextSelector("placeholder", text, exact)
}
func getByRoleSelector(role AriaRole, options ...LocatorGetByRoleOptions) string {
props := make(map[string]string)
if len(options) == 1 {
if options[0].Checked != nil {
props["checked"] = fmt.Sprintf("%t", *options[0].Checked)
}
if options[0].Disabled != nil {
props["disabled"] = fmt.Sprintf("%t", *options[0].Disabled)
}
if options[0].Selected != nil {
props["selected"] = fmt.Sprintf("%t", *options[0].Selected)
}
if options[0].Expanded != nil {
props["expanded"] = fmt.Sprintf("%t", *options[0].Expanded)
}
if options[0].IncludeHidden != nil {
props["include-hidden"] = fmt.Sprintf("%t", *options[0].IncludeHidden)
}
if options[0].Level != nil {
props["level"] = fmt.Sprintf("%d", *options[0].Level)
}
if options[0].Name != nil {
exact := false
if options[0].Exact != nil {
exact = *options[0].Exact
}
props["name"] = escapeForAttributeSelector(options[0].Name, exact)
}
if options[0].Pressed != nil {
props["pressed"] = fmt.Sprintf("%t", *options[0].Pressed)
}
}
propsStr := ""
for k, v := range props {
propsStr += "[" + k + "=" + v + "]"
}
return fmt.Sprintf("internal:role=%s%s", role, propsStr)
}
func getByTextSelector(text interface{}, exact bool) string {
return fmt.Sprintf(`internal:text=%s`, escapeForTextSelector(text, exact))
}
func getByTestIdSelector(testIdAttributeName string, testId interface{}) string {
return fmt.Sprintf(`internal:testid=[%s=%s]`, testIdAttributeName, escapeForAttributeSelector(testId, true))
}
func getByTitleSelector(text interface{}, exact bool) string {
return getByAttributeTextSelector("title", text, exact)
}
func getTestIdAttributeName() string {
return testIdAttributeName
}
func setTestIdAttributeName(name string) {
testIdAttributeName = name
}