Skip to content

Commit 9e86b1a

Browse files
committed
chore(playwright): add tests
1 parent 6057106 commit 9e86b1a

File tree

3 files changed

+92
-14
lines changed

3 files changed

+92
-14
lines changed

executors/playwright/actions.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type ActionFunc func(page playwrightgo.Page, element string, target ...any) erro
1313
var actionMap = map[string]ActionFunc{
1414
"Click": ClickAction,
1515
"DoubleClick": DoubleClickAction,
16+
"Doubleclick": DoubleClickAction,
1617
"Tap": TapAction,
1718
"Focus": FocusAction,
1819
"Blur": BlurAction,

executors/playwright/playwright.go

+32-14
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
103103
return nil, fmt.Errorf("could not goto: %w", err)
104104
}
105105

106-
err = performActions(page, e.Actions)
106+
err = performActions(ctx, page, e.Actions)
107107
if err != nil {
108108
return nil, err
109109
}
@@ -112,7 +112,6 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
112112
if err != nil {
113113
return nil, fmt.Errorf("could not goto: %w", err)
114114
}
115-
// TODO: run the assertions in here ...
116115

117116
err = browser.Close()
118117
if err != nil {
@@ -135,25 +134,44 @@ func (Executor) Run(ctx context.Context, step venom.TestStep) (interface{}, erro
135134
}, nil
136135
}
137136

138-
func performActions(page playwrightgo.Page, actions []string) error {
137+
// parseActionLine parses a line containing an Action expression and returns
138+
// the actionName, the arguments (rest of the line), the actionFunc or an error
139+
func parseActionLine(actionLine string) (string, string, ActionFunc, error) {
140+
if actionLine == "" {
141+
return "", "", nil, fmt.Errorf("action line MUST not be empty")
142+
}
143+
parts := strings.SplitN(strings.TrimSpace(actionLine), " ", 2)
144+
if len(parts) < 2 {
145+
return "", "", nil, fmt.Errorf("action line MUST have atleast two arguments: ACTION <selector>")
146+
}
147+
actionName, arguments := parts[0], parts[1]
148+
actionFunc, ok := actionMap[actionName]
149+
if !ok {
150+
return "", "", nil, fmt.Errorf("invalid or unsupported action specified '%s'", actionName)
151+
}
152+
return actionName, arguments, actionFunc, nil
153+
}
154+
155+
func performActions(ctx context.Context, page playwrightgo.Page, actions []string) error {
139156
for _, action := range actions {
140-
fmt.Println("perform action step", action)
141-
parts := strings.SplitN(strings.TrimSpace(action), " ", 2)
142-
actionName, arguments := parts[0], parts[1]
143-
actionFunc, ok := actionMap[actionName]
144-
if !ok {
145-
return fmt.Errorf("invalid or unsupported action specified '%s'", actionName)
157+
actionName, arguments, actionFunc, err := parseActionLine(action)
158+
if err != nil {
159+
return err
146160
}
161+
162+
venom.Debug(ctx, fmt.Sprintf("perform action '%s' with arguments '%v'\n", actionName, arguments))
163+
147164
selectorAndArgs := strings.SplitN(strings.TrimSpace(arguments), " ", 2)
148165
selector := removeQuotes(selectorAndArgs[0])
149-
var err error
166+
167+
var actErr error
150168
if len(selectorAndArgs) <= 1 {
151-
err = actionFunc(page, selector, nil)
169+
actErr = actionFunc(page, selector, nil)
152170
} else {
153-
err = actionFunc(page, selector, removeQuotes(selectorAndArgs[1]))
171+
actErr = actionFunc(page, selector, removeQuotes(selectorAndArgs[1]))
154172
}
155-
if err != nil {
156-
return err
173+
if actErr != nil {
174+
return actErr
157175
}
158176
}
159177
return nil
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package playwright
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestParseValidActionLines(t *testing.T) {
8+
testCases := []struct {
9+
ActionLine string
10+
Action string // expected action
11+
}{
12+
{ActionLine: `Click "some-element"`, Action: "Click"},
13+
{ActionLine: `DoubleClick "some-element"`, Action: "DoubleClick"},
14+
{ActionLine: `Doubleclick "some-element"`, Action: "Doubleclick"},
15+
{ActionLine: `Tap "some-element"`, Action: "Tap"},
16+
{ActionLine: `Focus "some-element"`, Action: "Focus"},
17+
{ActionLine: `Blur "some-element"`, Action: "Blur"},
18+
{ActionLine: `Clear "some-element"`, Action: "Clear"},
19+
{ActionLine: `Check "some-element"`, Action: "Check"},
20+
{ActionLine: `Uncheck "some-element"`, Action: "Uncheck"},
21+
{ActionLine: `Fill "some-element" "some text"`, Action: "Fill"},
22+
{ActionLine: `Press "some-element"`, Action: "Press"},
23+
{ActionLine: `Type "some-element"`, Action: "Type"},
24+
}
25+
26+
for _, tc := range testCases {
27+
if action, _, _, err := parseActionLine(tc.ActionLine); err != nil {
28+
t.Errorf("failed to parse action, got error '%v'", err)
29+
} else {
30+
if action != tc.Action {
31+
t.Errorf("failed to parse action, expected '%s' but got '%s'", tc.Action, action)
32+
}
33+
}
34+
}
35+
}
36+
37+
func TestParseInvalidActionLines(t *testing.T) {
38+
testCases := []string{
39+
``,
40+
`Click`,
41+
`Clickit "some-element"`,
42+
`Double-Click "some-element"`,
43+
`Double click "some-element"`,
44+
`Taps"some-element"`,
45+
`Focuses "some-element"`,
46+
`Blurs "some-element"`,
47+
`Cleare "some-element"`,
48+
`Checked "some-element"`,
49+
`Unchecked "some-element"`,
50+
`Pressed "some-element"`,
51+
`Typed "some-element"`,
52+
}
53+
54+
for _, tc := range testCases {
55+
if _, _, _, err := parseActionLine(tc); err == nil {
56+
t.Errorf("failed to parse action, expected error but got none for '%s'", tc)
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)