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

Automatically dismiss dialog boxes #663

Merged
merged 10 commits into from
Nov 30, 2022
22 changes: 22 additions & 0 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,34 @@ func (fs *FrameSession) initEvents() {
fs.onAttachedToTarget(ev)
case *target.EventDetachedFromTarget:
fs.onDetachedFromTarget(ev)
case *cdppage.EventJavascriptDialogOpening:
fs.onEventJavascriptDialogOpening(ev)
}
}
}
}()
}

func (fs *FrameSession) onEventJavascriptDialogOpening(event *cdppage.EventJavascriptDialogOpening) {
fs.logger.Debugf("FrameSession:onEventJavascriptDialogOpening",
"sid:%v tid:%v url:%v dialogType:%s",
fs.session.ID(), fs.targetID, event.URL, event.Type)

// Dialog type of beforeunload needs to accept the
// dialog, instead of dismissing it. We're unable to
// dismiss beforeunload dialog boxes at the moment as
// it seems to pause the exec of any other action on
// the page. I believe this is an issue in Chromium.
action := cdppage.HandleJavaScriptDialog(false)
if event.Type == cdppage.DialogTypeBeforeunload {
action = cdppage.HandleJavaScriptDialog(true)
}
inancgumus marked this conversation as resolved.
Show resolved Hide resolved

if err := action.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil {
fs.logger.Errorf("FrameSession:onEventJavascriptDialogOpening", "failed to dismiss dialog box: %v", err)
}
}

func (fs *FrameSession) initFrameTree() error {
fs.logger.Debugf("NewFrameSession:initFrameTree",
"sid:%v tid:%v", fs.session.ID(), fs.targetID)
Expand Down
51 changes: 51 additions & 0 deletions tests/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tests
import (
"testing"

"github.com/dop251/goja"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -21,3 +23,52 @@ func TestFramePress(t *testing.T) {

require.Equal(t, "AbC", f.InputValue("#text1", nil))
}

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

tests := []string{
"alert",
"confirm",
"prompt",
"beforeunload",
}

for _, test := range tests {
t.Run(test, func(t *testing.T) {
t.Parallel()

b := newTestBrowser(t, withFileServer())

p := b.NewPage(nil)

err := b.await(func() error {
opts := b.toGojaValue(struct {
WaitUntil string `js:"waitUntil"`
}{
WaitUntil: "networkidle",
})
pageGoto := p.Goto(
b.staticURL("dialog.html?dialogType="+test),
opts,
)
b.promise(pageGoto).then(func() *goja.Promise {
if test == "beforeunload" {
return p.Click("#clickHere", nil)
}

result := p.TextContent("#textField", nil)
assert.EqualValues(t, test+" dismissed", result)

return nil
}).then(func() {
result := p.TextContent("#textField", nil)
assert.EqualValues(t, test+" dismissed", result)
})

return nil
})
require.NoError(t, err)
})
}
}
36 changes: 36 additions & 0 deletions tests/static/dialog.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<html lang="en">
<head></head>
<body>
<div id='textField'>Hello World</div>
<br />
<button id="clickHere" onclick="window.location.reload();">click here</button>

<script>
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const dialogType = urlParams.get('dialogType');
const div = document.getElementById('textField');

switch(dialogType) {
case "alert":
alert("Click accept");
div.textContent = 'alert dismissed';
break;
case "confirm":
confirm("Click accept");
div.textContent = 'confirm dismissed';
break;
case "prompt":
prompt("Add text and then click accept");
div.textContent = 'prompt dismissed';
break;
case "beforeunload":
window.addEventListener('beforeunload', (event) => {
event.returnValue = "Are you sure you want to leave?";
});
div.textContent = 'beforeunload dismissed';
break;
}
</script>
</body>
</html>