mirror of https://github.com/daffainfo/nuclei.git
chore: add timeout test
parent
bd14b0f2ac
commit
4383d8557f
|
@ -89,7 +89,7 @@ type requestRule struct {
|
|||
func (p *Page) WaitVisible(act *Action, out map[string]string) error {
|
||||
element, err := p.pageElementBy(act.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.Wrap(err, "could not find element")
|
||||
}
|
||||
if err = element.WaitVisible(); err != nil {
|
||||
return errors.Wrap(err, "could not wait element")
|
||||
|
|
|
@ -280,39 +280,74 @@ func TestActionHeadersChange(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})
|
||||
require.Nil(t, err, "could not create browser")
|
||||
defer browser.Close()
|
||||
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")
|
||||
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, `
|
||||
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>
|
||||
<script>
|
||||
setTimeout(() => document.querySelector('#test').style.display = '', 1000);
|
||||
</script>
|
||||
</html>`)
|
||||
}))
|
||||
defer ts.Close()
|
||||
<head>
|
||||
<title>Nuclei Test Page</title>
|
||||
</head>
|
||||
<button style="display:none" id="test">Wait for me!</button>
|
||||
<script>
|
||||
setTimeout(() => document.querySelector('#test').style.display = '', 1000);
|
||||
</script>
|
||||
</html>`)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
parsed, err := url.Parse(ts.URL)
|
||||
require.Nil(t, err, "could not parse URL")
|
||||
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']"}},
|
||||
}
|
||||
_, page, err := instance.Run(parsed, actions, 20*time.Second)
|
||||
require.Nil(t, err, "could not run page actions")
|
||||
defer page.Close()
|
||||
actions := []*Action{
|
||||
{ActionType: "navigate", Data: map[string]string{"url": "{{BaseURL}}"}},
|
||||
{ActionType: "waitvisible", Data: map[string]string{"by": "x", "xpath": "//button[@id='test']"}},
|
||||
}
|
||||
_, page, err := instance.Run(parsed, actions, 20*time.Second)
|
||||
require.Nil(t, err, "could not run page actions")
|
||||
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")
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue