-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
ui_test.go
267 lines (221 loc) · 6.2 KB
/
ui_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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//go:build test_ui
package main
import (
"fmt"
"os"
"testing"
"time"
"github.com/playwright-community/playwright-go"
)
const (
url = "http://localhost:8089/chat"
nameInput = `//input[@id="name"]`
joinButton = `//input[@id="join"]`
msgOuputDiv = `//div[@id="messages"]`
msgInputTextarea = `//textarea[@id="msg"]`
sendInput = `//input[@id="send"]`
nameSpan = `//span[contains(@class, "name") and text()="%s"]`
nameColorSpan = `//span[contains(@class, "name") and text()="%s" and @style="color:%s"]`
msgSpan = `//span[contains(@class, "msg") and text()="%s"]`
meCmdSpan = `//span[contains(@class, "cmdme") and text()="%s"]`
commandSpan = `//span[contains(@class, "command")]`
)
var (
pw *playwright.Playwright
browsers []playwright.BrowserType
)
func openChat(t *testing.T, browser playwright.Browser, username string) (playwright.Page, error) {
page, err := browser.NewPage()
if err != nil {
return nil, fmt.Errorf("could not open new browser page: %w", err)
}
_, err = page.Goto(url)
if err != nil {
return nil, fmt.Errorf("could not open chat page: %w", err)
}
_, err = page.WaitForSelector(nameInput)
if err != nil {
return nil, fmt.Errorf("could not find name input: %w", err)
}
err = page.Type(nameInput, username)
if err != nil {
return nil, fmt.Errorf("an error occured when typing the username: %w", err)
}
err = page.Click(joinButton)
if err != nil {
return nil, fmt.Errorf("could not click join button: %w", err)
}
_, err = page.WaitForSelector(msgOuputDiv)
if err != nil {
return nil, fmt.Errorf("chat window did not show: %w", err)
}
_, err = page.WaitForSelector(fmt.Sprintf(nameSpan, username))
if err != nil {
return nil, fmt.Errorf("could not get join message span: %w", err)
}
return page, nil
}
func sendMessage(page playwright.Page, msg string) error {
err := page.Type(msgInputTextarea, msg)
if err != nil {
return fmt.Errorf("could not find msg box: %w", err)
}
err = page.Click(sendInput)
if err != nil {
return fmt.Errorf("could not send message: %w", err)
}
return nil
}
func runTests(t *testing.T, f func(t *testing.T, browser playwright.Browser)) {
for _, bt := range browsers {
browser, err := bt.Launch()
if err != nil {
t.Fatalf("could not launch browser: %v", err)
}
defer func() {
if browser.IsConnected() {
browser.Close()
}
}()
t.Run(bt.Name(), func(t *testing.T) {
f(t, browser)
browser.Close()
})
}
}
func TestMain(m *testing.M) {
// Start running the servers, but sleep for only 500ms
// because it should just take a couple ms for startup
// and install and run will take longer
go run(args{ConfigFile: "./settings.json"})
time.Sleep(500 * time.Millisecond)
err := playwright.Install()
if err != nil {
fmt.Printf("could not install playwright: %v\n", err)
os.Exit(1)
}
pw, err = playwright.Run()
if err != nil {
fmt.Printf("could not run playwright: %v\n", err)
os.Exit(1)
}
browsers = append(browsers, pw.Firefox)
browsers = append(browsers, pw.Chromium)
code := m.Run()
os.Exit(code)
}
func TestAccess(t *testing.T) {
runTests(t, func(t *testing.T, browser playwright.Browser) {
_, err := openChat(t, browser, "testUser")
if err != nil {
t.Fatal(err)
}
})
}
func TestChatMessage(t *testing.T) {
const msg = "testing 1 2 3"
runTests(t, func(t *testing.T, browser playwright.Browser) {
page1, err := openChat(t, browser, "testUser1")
if err != nil {
t.Fatalf("could not open chat for user 1: %v", err)
}
page2, err := openChat(t, browser, "testUser2")
if err != nil {
t.Fatalf("could not open chat for user 2: %v", err)
}
err = sendMessage(page1, msg)
if err != nil {
t.Fatal(err)
}
_, err = page2.WaitForSelector(fmt.Sprintf(msgSpan, msg))
if err != nil {
t.Fatalf("did not find testing msg: %v", err)
}
})
}
func TestCommandNick(t *testing.T) {
const nick = "newNick"
runTests(t, func(t *testing.T, browser playwright.Browser) {
page, err := openChat(t, browser, "testUser")
if err != nil {
t.Fatal(err)
}
err = sendMessage(page, fmt.Sprintf("/nick %s", nick))
if err != nil {
t.Fatal(err)
}
_, err = page.WaitForSelector(fmt.Sprintf(nameSpan, nick))
if err != nil {
t.Fatalf("could not find nick change message: %v", err)
}
})
}
func TestCommandMe(t *testing.T) {
const msg = "this is a me message"
runTests(t, func(t *testing.T, browser playwright.Browser) {
page, err := openChat(t, browser, "testUser")
if err != nil {
t.Fatal(err)
}
err = sendMessage(page, fmt.Sprintf("/me %s", msg))
if err != nil {
t.Fatal(err)
}
_, err = page.WaitForSelector(fmt.Sprintf(meCmdSpan, msg))
if err != nil {
t.Fatalf("could not find user me message: %v", err)
}
})
}
func TestCommandColor(t *testing.T) {
const (
name = "testUser"
color = "red"
)
runTests(t, func(t *testing.T, browser playwright.Browser) {
page, err := openChat(t, browser, name)
if err != nil {
t.Fatal(err)
}
err = sendMessage(page, fmt.Sprintf("/color %s", color))
if err != nil {
t.Fatalf("failed to send /color command: %v", err)
}
_, err = page.WaitForSelector(`//span[contains(@class, "command") and contains(text(),"Color changed successfully")]`)
if err != nil {
t.Fatalf("could not find color change notification: %v", err)
}
err = sendMessage(page, "test")
if err != nil {
t.Fatal(err)
}
_, err = page.WaitForSelector(fmt.Sprintf(nameColorSpan, name, color))
if err != nil {
t.Fatalf("could not find user message with new color: %v", err)
}
})
}
func TestGenericCommands(t *testing.T) {
wrapFunc := func(command string) func(*testing.T) {
return func(t *testing.T) {
runTests(t, func(t *testing.T, browser playwright.Browser) {
page, err := openChat(t, browser, "testUser")
if err != nil {
t.Fatal(err)
}
err = sendMessage(page, command)
if err != nil {
t.Fatalf("failed to send %s command: %v", command, err)
}
_, err = page.WaitForSelector(commandSpan)
if err != nil {
t.Fatalf("could not find command message: %v", err)
}
})
}
}
t.Run("pin", wrapFunc("/pin"))
t.Run("stats", wrapFunc("/stats"))
t.Run("users", wrapFunc("/users"))
t.Run("whoami", wrapFunc("/whoami"))
}