forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (69 loc) · 2.96 KB
/
main.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
//go:build ignore
// +build ignore
package main
import (
"fmt"
"log"
"reflect"
"github.com/playwright-community/playwright-go"
)
func assertErrorToNilf(message string, err error) {
if err != nil {
log.Fatalf(message, err)
}
}
func assertEqual(expected, actual interface{}) {
if !reflect.DeepEqual(expected, actual) {
panic(fmt.Sprintf("%v does not equal %v", actual, expected))
}
}
const todoName = "Bake a cake"
func main() {
pw, err := playwright.Run()
assertErrorToNilf("could not launch playwright: %w", err)
browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
Headless: playwright.Bool(false),
})
assertErrorToNilf("could not launch Chromium: %w", err)
context, err := browser.NewContext()
assertErrorToNilf("could not create context: %w", err)
page, err := context.NewPage()
assertErrorToNilf("could not create page: %w", err)
_, err = page.Goto("http://todomvc.com/examples/react/")
assertErrorToNilf("could not goto: %w", err)
// Helper function to get the amount of todos on the page
assertCountOfTodos := func(shouldBeCount int) {
count, err := page.EvalOnSelectorAll("ul.todo-list > li", "el => el.length")
assertErrorToNilf("could not determine todo list count: %w", err)
assertEqual(shouldBeCount, count)
}
// Initially there should be 0 entries
assertCountOfTodos(0)
// Adding a todo entry (click in the input, enter the todo title and press the Enter key)
assertErrorToNilf("could not click: %v", page.Click("input.new-todo"))
assertErrorToNilf("could not type: %v", page.Type("input.new-todo", todoName))
assertErrorToNilf("could not press: %v", page.Press("input.new-todo", "Enter"))
// After adding 1 there should be 1 entry in the list
assertCountOfTodos(1)
// Here we get the text in the first todo item to see if it"s the same which the user has entered
textContentOfFirstTodoEntry, err := page.EvalOnSelector("ul.todo-list > li:nth-child(1) label", "el => el.textContent")
assertErrorToNilf("could not get first todo entry: %w", err)
assertEqual(todoName, textContentOfFirstTodoEntry)
// The todo list should be persistent. Here we reload the page and see if the entry is still there
_, err = page.Reload()
assertErrorToNilf("could not reload: %w", err)
assertCountOfTodos(1)
// Set the entry to completed
assertErrorToNilf("could not click: %v", page.Click("input.toggle"))
// Filter for active entries. There should be 0, because we have completed the entry already
assertErrorToNilf("could not click: %v", page.Click("text=Active"))
assertCountOfTodos(0)
// If we filter now for completed entries, there should be 1
assertErrorToNilf("could not click: %v", page.Click("text=Completed"))
assertCountOfTodos(1)
// Clear the list of completed entries, then it should be again 0
assertErrorToNilf("could not click: %v", page.Click("text=Clear completed"))
assertCountOfTodos(0)
assertErrorToNilf("could not close browser: %w", browser.Close())
assertErrorToNilf("could not stop Playwright: %w", pw.Stop())
}