Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 0ca4685

Browse files
authored
Merge pull request #663 from grafana/feat/655-dismiss-dialog
undefined
2 parents d450671 + 40b4a87 commit 0ca4685

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

common/frame_session.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,34 @@ func (fs *FrameSession) initEvents() {
259259
fs.onAttachedToTarget(ev)
260260
case *target.EventDetachedFromTarget:
261261
fs.onDetachedFromTarget(ev)
262+
case *cdppage.EventJavascriptDialogOpening:
263+
fs.onEventJavascriptDialogOpening(ev)
262264
}
263265
}
264266
}
265267
}()
266268
}
267269

270+
func (fs *FrameSession) onEventJavascriptDialogOpening(event *cdppage.EventJavascriptDialogOpening) {
271+
fs.logger.Debugf("FrameSession:onEventJavascriptDialogOpening",
272+
"sid:%v tid:%v url:%v dialogType:%s",
273+
fs.session.ID(), fs.targetID, event.URL, event.Type)
274+
275+
// Dialog type of beforeunload needs to accept the
276+
// dialog, instead of dismissing it. We're unable to
277+
// dismiss beforeunload dialog boxes at the moment as
278+
// it seems to pause the exec of any other action on
279+
// the page. I believe this is an issue in Chromium.
280+
action := cdppage.HandleJavaScriptDialog(false)
281+
if event.Type == cdppage.DialogTypeBeforeunload {
282+
action = cdppage.HandleJavaScriptDialog(true)
283+
}
284+
285+
if err := action.Do(cdp.WithExecutor(fs.ctx, fs.session)); err != nil {
286+
fs.logger.Errorf("FrameSession:onEventJavascriptDialogOpening", "failed to dismiss dialog box: %v", err)
287+
}
288+
}
289+
268290
func (fs *FrameSession) initFrameTree() error {
269291
fs.logger.Debugf("NewFrameSession:initFrameTree",
270292
"sid:%v tid:%v", fs.session.ID(), fs.targetID)

tests/frame_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package tests
33
import (
44
"testing"
55

6+
"github.com/dop251/goja"
7+
"github.com/stretchr/testify/assert"
68
"github.com/stretchr/testify/require"
79
)
810

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

2224
require.Equal(t, "AbC", f.InputValue("#text1", nil))
2325
}
26+
27+
func TestFrameDismissDialogBox(t *testing.T) {
28+
t.Parallel()
29+
30+
tests := []string{
31+
"alert",
32+
"confirm",
33+
"prompt",
34+
"beforeunload",
35+
}
36+
37+
for _, test := range tests {
38+
t.Run(test, func(t *testing.T) {
39+
t.Parallel()
40+
41+
b := newTestBrowser(t, withFileServer())
42+
43+
p := b.NewPage(nil)
44+
45+
err := b.await(func() error {
46+
opts := b.toGojaValue(struct {
47+
WaitUntil string `js:"waitUntil"`
48+
}{
49+
WaitUntil: "networkidle",
50+
})
51+
pageGoto := p.Goto(
52+
b.staticURL("dialog.html?dialogType="+test),
53+
opts,
54+
)
55+
b.promise(pageGoto).then(func() *goja.Promise {
56+
if test == "beforeunload" {
57+
return p.Click("#clickHere", nil)
58+
}
59+
60+
result := p.TextContent("#textField", nil)
61+
assert.EqualValues(t, test+" dismissed", result)
62+
63+
return nil
64+
}).then(func() {
65+
result := p.TextContent("#textField", nil)
66+
assert.EqualValues(t, test+" dismissed", result)
67+
})
68+
69+
return nil
70+
})
71+
require.NoError(t, err)
72+
})
73+
}
74+
}

tests/static/dialog.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<html lang="en">
2+
<head></head>
3+
<body>
4+
<div id='textField'>Hello World</div>
5+
<br />
6+
<button id="clickHere" onclick="window.location.reload();">click here</button>
7+
8+
<script>
9+
const queryString = window.location.search;
10+
const urlParams = new URLSearchParams(queryString);
11+
const dialogType = urlParams.get('dialogType');
12+
const div = document.getElementById('textField');
13+
14+
switch(dialogType) {
15+
case "alert":
16+
alert("Click accept");
17+
div.textContent = 'alert dismissed';
18+
break;
19+
case "confirm":
20+
confirm("Click accept");
21+
div.textContent = 'confirm dismissed';
22+
break;
23+
case "prompt":
24+
prompt("Add text and then click accept");
25+
div.textContent = 'prompt dismissed';
26+
break;
27+
case "beforeunload":
28+
window.addEventListener('beforeunload', (event) => {
29+
event.returnValue = "Are you sure you want to leave?";
30+
});
31+
div.textContent = 'beforeunload dismissed';
32+
break;
33+
}
34+
</script>
35+
</body>
36+
</html>

0 commit comments

Comments
 (0)