Skip to content

Commit

Permalink
Merge pull request #663 from grafana/feat/655-dismiss-dialog
Browse files Browse the repository at this point in the history
undefined
  • Loading branch information
ankur22 authored Nov 30, 2022
2 parents d450671 + 40b4a87 commit 0ca4685
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
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)
}

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>

0 comments on commit 0ca4685

Please sign in to comment.