-
Notifications
You must be signed in to change notification settings - Fork 0
/
uistrategy_test.go
158 lines (151 loc) · 4.89 KB
/
uistrategy_test.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
package uistrategy_test
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
log "github.com/dnitsch/simplelog"
"github.com/dnitsch/uistrategy"
"github.com/dnitsch/uistrategy/internal/util"
)
func Test_NoAuthSimulate(t *testing.T) {
l := log.New(&bytes.Buffer{}, log.DebugLvl)
tests := map[string]struct {
name string
auth *uistrategy.Auth
actions []*uistrategy.ViewAction
handler func(t *testing.T) http.Handler
baseConf func(t *testing.T, url string) uistrategy.BaseConfig
expect string
}{
"happy path - stop on error": {
auth: nil,
handler: func(t *testing.T) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/route", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(pocketBaseStyle)
})
return mux
},
baseConf: func(t *testing.T, url string) uistrategy.BaseConfig {
return uistrategy.BaseConfig{BaseUrl: url, ContinueOnError: false, LauncherConfig: &uistrategy.WebConfig{Headless: true}}
},
actions: []*uistrategy.ViewAction{
{
Name: "test route",
Navigate: `/route`,
ElementActions: []*uistrategy.ElementAction{
{
Name: "Click Button",
Assert: true,
Element: uistrategy.Element{
Selector: util.Str(`//*[@id="app"]/div/div/aside/footer/button/./span[text() = 'New collection']`),
},
},
{
Name: "click test collection - just in case",
Element: uistrategy.Element{
Selector: util.Str(`//*[@class='sidebar-content']/*[contains(., 'test')]/span`),
},
},
{
Name: "assert field testField1 is created",
Element: uistrategy.Element{
Selector: util.Str(`//*[@class='page-wrapper']//span[contains(., 'testField1')]`),
},
Assert: true,
},
},
}},
},
"error on network": {
auth: nil,
handler: func(t *testing.T) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`<html><body><div>Error Occured</div></body></html>`))
})
return mux
},
baseConf: func(t *testing.T, url string) uistrategy.BaseConfig {
return uistrategy.BaseConfig{BaseUrl: url, ContinueOnError: false, LauncherConfig: &uistrategy.WebConfig{Headless: true}}
},
actions: []*uistrategy.ViewAction{{
Name: "navigate to error",
Navigate: `/error`,
ElementActions: []*uistrategy.ElementAction{
{
Name: "asset collection is created and present in sidebar",
Assert: true,
Element: uistrategy.Element{
Selector: util.Str(`//*[@class='sidebar-content']/*[contains(., 'test')]/span`),
},
},
}},
},
expect: "following errors occured:\n\n\tin view: asset collection is created and present in sidebar, performing action: //*[@class='sidebar-content']/*[contains(., 'test')]/span, failed on: element not found",
},
"not found but continueOnError": {
auth: nil,
handler: func(t *testing.T) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/route", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(pocketBaseStyle)
})
return mux
},
baseConf: func(t *testing.T, url string) uistrategy.BaseConfig {
return uistrategy.BaseConfig{BaseUrl: url, ContinueOnError: true, LauncherConfig: &uistrategy.WebConfig{Headless: true}}
},
actions: []*uistrategy.ViewAction{
{
Name: "test route",
Navigate: `/route`,
ElementActions: []*uistrategy.ElementAction{
{
Name: "Found element",
Assert: true,
Element: uistrategy.Element{
Selector: util.Str(`//*[@id="app"]/div/div/aside/footer/button/./span[text() = 'New collection']`),
},
},
{
Name: "not found id",
Assert: true,
Element: uistrategy.Element{
Selector: util.Str(`#notfound`),
},
},
{
Name: "assert field testField1 is created",
Element: uistrategy.Element{
Selector: util.Str(`//*[@class='page-wrapper']//span[contains(., 'testField1')]`),
},
Assert: true,
},
},
}},
expect: "following errors occured:\n\n\tin view: not found id, performing action: #notfound, failed on: element not found",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
ts := httptest.NewServer(tt.handler(t))
defer ts.Close()
ui := uistrategy.New(tt.baseConf(t, ts.URL)).WithLogger(l)
err := ui.Drive(context.TODO(), tt.auth, tt.actions)
if err != nil {
if err.Error() != tt.expect {
t.Errorf("got: %v\n\nwant: %v", err, nil)
}
// t.Logf("error: %s \n\nmatches the expected \n\noutput: %s", err.Error(), tt.expect)
return
}
})
}
}