Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Locator.WaitFor for detached and hidden states #852

Merged
merged 5 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,18 @@ func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptio
return handle, nil
}

func (f *Frame) waitFor(selector string, opts *FrameWaitForSelectorOptions) error {
f.log.Debugf("Frame:waitFor", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)

document, err := f.document()
if err != nil {
return err
}

_, err = document.waitForSelector(f.ctx, selector, opts)
return err
}

// AddScriptTag is not implemented.
func (f *Frame) AddScriptTag(opts goja.Value) {
k6ext.Panic(f.ctx, "Frame.AddScriptTag() has not been implemented yet")
Expand Down
4 changes: 2 additions & 2 deletions common/js/injected_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -889,11 +889,11 @@ class InjectedScript {
case "attached":
return element ? element : continuePolling;
case "detached":
return !element ? undefined : continuePolling;
return !element ? true : continuePolling;
case "visible":
return visible ? element : continuePolling;
case "hidden":
return !visible ? undefined : continuePolling;
return !visible ? element : continuePolling;
}
};

Expand Down
3 changes: 1 addition & 2 deletions common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,5 @@ func (l *Locator) WaitFor(opts goja.Value) {

func (l *Locator) waitFor(opts *FrameWaitForSelectorOptions) error {
opts.Strict = true
_, err := l.frame.waitForSelector(l.selector, opts)
return err
return l.frame.waitFor(l.selector, opts)
}
39 changes: 36 additions & 3 deletions tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import (
// Note:
// We skip adding t.Parallel to subtests because goja or our code might race.

type jsFrameWaitForSelectorOpts struct {
jsFrameBaseOpts

State string
}

func TestLocator(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -173,9 +179,36 @@ func TestLocator(t *testing.T) {
},
},
{
"WaitFor", func(tb *testBrowser, p api.Page) {
timeout := tb.toGojaValue(jsFrameBaseOpts{Timeout: "100"})
require.NotPanics(t, func() { p.Locator("#link", nil).WaitFor(timeout) })
"WaitFor state:visible", func(tb *testBrowser, p api.Page) {
opts := tb.toGojaValue(jsFrameBaseOpts{Timeout: "100"})
require.NotPanics(t, func() { p.Locator("#link", nil).WaitFor(opts) })
},
},
{
"WaitFor state:attached", func(tb *testBrowser, p api.Page) {
opts := tb.toGojaValue(jsFrameWaitForSelectorOpts{
jsFrameBaseOpts: jsFrameBaseOpts{Timeout: "100"},
State: "attached",
})
require.NotPanics(t, func() { p.Locator("#link", nil).WaitFor(opts) })
},
},
{
"WaitFor state:hidden", func(tb *testBrowser, p api.Page) {
opts := tb.toGojaValue(jsFrameWaitForSelectorOpts{
jsFrameBaseOpts: jsFrameBaseOpts{Timeout: "100"},
State: "hidden",
})
require.NotPanics(t, func() { p.Locator("#inputHiddenText", nil).WaitFor(opts) })
},
},
{
"WaitFor state:detached", func(tb *testBrowser, p api.Page) {
opts := tb.toGojaValue(jsFrameWaitForSelectorOpts{
jsFrameBaseOpts: jsFrameBaseOpts{Timeout: "100"},
State: "detached",
})
require.NotPanics(t, func() { p.Locator("#nonExistingElement", nil).WaitFor(opts) })
},
},
}
Expand Down
3 changes: 2 additions & 1 deletion tests/static/locators.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<input id="inputCheckbox" type="checkbox" />
<input type="checkbox" />
<input id="inputText" type="text" value="something" />
<input id="inputHiddenText" type="text" hidden="true">
<div id="divHello"><span>hello</span></div>
<div><span>bye</span></div>
<textarea>text area</textarea>
Expand Down Expand Up @@ -40,4 +41,4 @@
</script>
</body>

</html>
</html>