Skip to content

Commit e9a2230

Browse files
committed
Add test for getByTestId
1 parent a35d920 commit e9a2230

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

internal/js/modules/k6/browser/tests/get_by_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,3 +1099,90 @@ func TestGetByTitleSuccess(t *testing.T) {
10991099
})
11001100
}
11011101
}
1102+
1103+
func TestGetByTestIDSuccess(t *testing.T) {
1104+
t.Parallel()
1105+
1106+
tests := []struct {
1107+
name string
1108+
testID string
1109+
expected int
1110+
expectedText string
1111+
}{
1112+
{
1113+
"submit_button",
1114+
"'submit-button'",
1115+
1,
1116+
"Submit",
1117+
},
1118+
{
1119+
"username_input",
1120+
"'username-input'",
1121+
1,
1122+
"",
1123+
},
1124+
{
1125+
"info_box",
1126+
"'info-box'",
1127+
1,
1128+
"Information",
1129+
},
1130+
{
1131+
"regex_match",
1132+
`/^[a-z0-9]+$/`,
1133+
1,
1134+
"Test span",
1135+
},
1136+
{
1137+
"link_testid",
1138+
"'my-link'",
1139+
1,
1140+
"Link",
1141+
},
1142+
{
1143+
"non_existent_testid",
1144+
"'does-not-exist'",
1145+
0,
1146+
"",
1147+
},
1148+
{
1149+
"missing_testid",
1150+
"",
1151+
0,
1152+
"",
1153+
},
1154+
{
1155+
"empty_string",
1156+
"''",
1157+
0,
1158+
"",
1159+
},
1160+
}
1161+
for _, tt := range tests {
1162+
t.Run(tt.name, func(t *testing.T) {
1163+
t.Parallel()
1164+
1165+
tb := newTestBrowser(t, withFileServer())
1166+
p := tb.NewPage(nil)
1167+
opts := &common.FrameGotoOptions{
1168+
Timeout: common.DefaultTimeout,
1169+
}
1170+
_, err := p.Goto(
1171+
tb.staticURL("get_by_testid.html"),
1172+
opts,
1173+
)
1174+
require.NoError(t, err)
1175+
1176+
l := p.GetByTestID(tt.testID)
1177+
c, err := l.Count()
1178+
assert.NoError(t, err)
1179+
assert.Equal(t, tt.expected, c)
1180+
1181+
if tt.expected > 0 && tt.expectedText != "" {
1182+
text, err := l.InnerText(sobek.Undefined())
1183+
assert.NoError(t, err)
1184+
assert.Equal(t, tt.expectedText, text)
1185+
}
1186+
})
1187+
}
1188+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<title>Test ID Selector Engine Test Suite</title>
4+
</head>
5+
<body>
6+
<button data-testid="submit-button">Submit</button>
7+
<input type="text" data-testid="username-input" placeholder="Username">
8+
<div data-testid="info-box">Information</div>
9+
<span data-testid="abc123">Test span</span>
10+
<a href="#" data-testid="my-link">Link</a>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)