chore: add timeout test

dev
Pedro Lopez Mareque 2021-10-09 17:30:45 +02:00
parent bd14b0f2ac
commit 4383d8557f
2 changed files with 64 additions and 29 deletions

View File

@ -89,7 +89,7 @@ type requestRule struct {
func (p *Page) WaitVisible(act *Action, out map[string]string) error { func (p *Page) WaitVisible(act *Action, out map[string]string) error {
element, err := p.pageElementBy(act.Data) element, err := p.pageElementBy(act.Data)
if err != nil { if err != nil {
return err return errors.Wrap(err, "could not find element")
} }
if err = element.WaitVisible(); err != nil { if err = element.WaitVisible(); err != nil {
return errors.Wrap(err, "could not wait element") return errors.Wrap(err, "could not wait element")

View File

@ -280,39 +280,74 @@ func TestActionHeadersChange(t *testing.T) {
} }
func TestActionWaitVisible(t *testing.T) { func TestActionWaitVisible(t *testing.T) {
_ = protocolstate.Init(&types.Options{}) t.Run("wait for an element being visible", func(t *testing.T) {
_ = protocolstate.Init(&types.Options{})
browser, err := New(&types.Options{ShowBrowser: false}) browser, err := New(&types.Options{ShowBrowser: false})
require.Nil(t, err, "could not create browser") require.Nil(t, err, "could not create browser")
defer browser.Close() defer browser.Close()
instance, err := browser.NewInstance() instance, err := browser.NewInstance()
require.Nil(t, err, "could not create browser instance") require.Nil(t, err, "could not create browser instance")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, ` fmt.Fprintln(w, `
<html> <html>
<head> <head>
<title>Nuclei Test Page</title> <title>Nuclei Test Page</title>
</head> </head>
<button style="display:none" id="test">Wait for me!</button> <button style="display:none" id="test">Wait for me!</button>
<script> <script>
setTimeout(() => document.querySelector('#test').style.display = '', 1000); setTimeout(() => document.querySelector('#test').style.display = '', 1000);
</script> </script>
</html>`) </html>`)
})) }))
defer ts.Close() defer ts.Close()
parsed, err := url.Parse(ts.URL) parsed, err := url.Parse(ts.URL)
require.Nil(t, err, "could not parse URL") require.Nil(t, err, "could not parse URL")
actions := []*Action{ actions := []*Action{
{ActionType: "navigate", Data: map[string]string{"url": "{{BaseURL}}"}}, {ActionType: "navigate", Data: map[string]string{"url": "{{BaseURL}}"}},
{ActionType: "waitvisible", Data: map[string]string{"by": "x", "xpath": "//button[@id='test']"}}, {ActionType: "waitvisible", Data: map[string]string{"by": "x", "xpath": "//button[@id='test']"}},
} }
_, page, err := instance.Run(parsed, actions, 20*time.Second) _, page, err := instance.Run(parsed, actions, 20*time.Second)
require.Nil(t, err, "could not run page actions") require.Nil(t, err, "could not run page actions")
defer page.Close() defer page.Close()
page.Page().MustElement("button").MustVisible() page.Page().MustElement("button").MustVisible()
})
t.Run("timeout because of element not visible", func(t *testing.T) {
_ = protocolstate.Init(&types.Options{})
browser, err := New(&types.Options{ShowBrowser: false})
require.Nil(t, err, "could not create browser")
defer browser.Close()
instance, err := browser.NewInstance()
require.Nil(t, err, "could not create browser instance")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `
<html>
<head>
<title>Nuclei Test Page</title>
</head>
<button style="display:none" id="test">Wait for me!</button>
</html>`)
}))
defer ts.Close()
parsed, err := url.Parse(ts.URL)
require.Nil(t, err, "could not parse URL")
actions := []*Action{
{ActionType: "navigate", Data: map[string]string{"url": "{{BaseURL}}"}},
{ActionType: "waitvisible", Data: map[string]string{"by": "x", "xpath": "//button[@id='test']"}},
}
_, _, err = instance.Run(parsed, actions, 2*time.Second)
require.Error(t, err)
require.Contains(t, err.Error(), "could not wait element")
})
} }