-
-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathpage_assertions.go
More file actions
131 lines (117 loc) · 3.13 KB
/
page_assertions.go
File metadata and controls
131 lines (117 loc) · 3.13 KB
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
package playwright
import (
"fmt"
"net/url"
"path"
"strings"
)
type pageAssertionsImpl struct {
assertionsBase
actualPage Page
}
func newPageAssertions(page Page, isNot bool, defaultTimeout *float64) *pageAssertionsImpl {
return &pageAssertionsImpl{
assertionsBase: assertionsBase{
actualLocator: page.Locator(":root"),
isNot: isNot,
defaultTimeout: defaultTimeout,
},
actualPage: page,
}
}
// expectOnFrame calls the frame's expect method directly without a selector.
// This is needed for page-level assertions like ToHaveTitle and ToHaveURL
// which should not be bound to a specific element.
func (pa *pageAssertionsImpl) expectOnFrame(
expression string,
options frameExpectOptions,
expected any,
message string,
) error {
options.IsNot = pa.isNot
if options.Timeout == nil {
options.Timeout = pa.defaultTimeout
}
if options.IsNot {
message = strings.ReplaceAll(message, "expected to", "expected not to")
}
frame := pa.actualPage.MainFrame().(*frameImpl)
overrides := map[string]any{
"expression": expression,
}
result, err := frame.channel.SendReturnAsDict("expect", options, overrides)
if err != nil {
return err
}
var (
received any
matches bool
log []string
)
if v, ok := result["received"]; ok {
received = parseResult(v)
}
if v, ok := result["matches"]; ok {
matches = v.(bool)
}
if v, ok := result["log"]; ok {
for _, l := range v.([]any) {
log = append(log, l.(string))
}
}
if matches == pa.isNot {
actual := received
logStr := strings.Join(log, "\n")
if logStr != "" {
logStr = "\nCall log:\n" + logStr
}
if expected != nil {
return fmt.Errorf("%s '%v'\nActual value: %v %s", message, expected, actual, logStr)
}
return fmt.Errorf("%s\nActual value: %v %s", message, actual, logStr)
}
return nil
}
func (pa *pageAssertionsImpl) ToHaveTitle(titleOrRegExp any, options ...PageAssertionsToHaveTitleOptions) error {
var timeout *float64
if len(options) == 1 {
timeout = options[0].Timeout
}
expectedValues, err := toExpectedTextValues([]any{titleOrRegExp}, false, true, nil)
if err != nil {
return err
}
return pa.expectOnFrame(
"to.have.title",
frameExpectOptions{ExpectedText: expectedValues, Timeout: timeout},
titleOrRegExp,
"Page title expected to be",
)
}
func (pa *pageAssertionsImpl) ToHaveURL(urlOrRegExp any, options ...PageAssertionsToHaveURLOptions) error {
var timeout *float64
var ignoreCase *bool
if len(options) == 1 {
timeout = options[0].Timeout
ignoreCase = options[0].IgnoreCase
}
baseURL := pa.actualPage.Context().(*browserContextImpl).options.BaseURL
if urlPath, ok := urlOrRegExp.(string); ok && baseURL != nil {
u, _ := url.Parse(*baseURL)
u.Path = path.Join(u.Path, urlPath)
urlOrRegExp = u.String()
}
expectedValues, err := toExpectedTextValues([]any{urlOrRegExp}, false, false, ignoreCase)
if err != nil {
return err
}
return pa.expectOnFrame(
"to.have.url",
frameExpectOptions{ExpectedText: expectedValues, Timeout: timeout},
urlOrRegExp,
"Page URL expected to be",
)
}
func (pa *pageAssertionsImpl) Not() PageAssertions {
return newPageAssertions(pa.actualPage, true, pa.defaultTimeout)
}