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

Commit 5bbfb66

Browse files
committed
Add a beforeunload dialog test
This test will ensure that the beforeunload dialog box is accepted when we try to reload the page. Unlike other alert boxes which appear when the page loads, beforeunload boxes appear when the page unloads.
1 parent 0592242 commit 5bbfb66

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

tests/frame_test.go

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

6+
"github.com/dop251/goja"
67
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
89
)
@@ -24,7 +25,6 @@ func TestFramePress(t *testing.T) {
2425
}
2526

2627
func TestFrameDismissDialogBox(t *testing.T) {
27-
2828
t.Parallel()
2929

3030
tests := []struct {
@@ -39,6 +39,9 @@ func TestFrameDismissDialogBox(t *testing.T) {
3939
{
4040
name: "prompt",
4141
},
42+
{
43+
name: "beforeunload",
44+
},
4245
}
4346

4447
for _, tt := range tests {
@@ -55,7 +58,16 @@ func TestFrameDismissDialogBox(t *testing.T) {
5558
}{
5659
WaitUntil: "networkidle",
5760
})
58-
b.promise(p.Goto(b.staticURL("dialog.html?dialogType="+tt.name), opts)).then(func() {
61+
b.promise(p.Goto(b.staticURL("dialog.html?dialogType="+tt.name), opts)).then(func() *goja.Promise {
62+
if tt.name == "beforeunload" {
63+
return p.Click("#clickHere", nil)
64+
}
65+
66+
result := p.TextContent("#textField", nil)
67+
assert.EqualValues(t, tt.name+" dismissed", result)
68+
69+
return nil
70+
}).then(func() {
5971
result := p.TextContent("#textField", nil)
6072
assert.EqualValues(t, tt.name+" dismissed", result)
6173
})

tests/static/dialog.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<body>
44
<div id='textField'>Hello World</div>
55
<br />
6-
<a href="/">click here</a>
6+
<button id="clickHere" onclick="window.location.reload();">click here</button>
77

88
<script>
99
const queryString = window.location.search;
@@ -24,6 +24,12 @@
2424
prompt("Add text and then click accept");
2525
div.textContent = 'prompt dismissed';
2626
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;
2733
}
2834
</script>
2935
</body>

0 commit comments

Comments
 (0)