aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/application/ConfirmBrowserTabClose.java
blob: e0144cf7ccc97ea21737db62ec225a0c01c60828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.vaadin.tests.application;

import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUIWithLog;
import com.vaadin.ui.Button;
import com.vaadin.ui.JavaScript;

public class ConfirmBrowserTabClose extends AbstractTestUIWithLog {

    @Override
    protected void setup(VaadinRequest request) {
        // To test the behavior, do
        // 1. Open the test in the browser
        // 2. Close the browser tab/window
        // 3. Choose to stay on the page after all
        // 4. Click the button
        // There should be no error
        Button b = new Button("Say hello", event -> log("Hello"));
        addComponent(b);
        JavaScript
                .eval("window.addEventListener('beforeunload', function (e) {"
                        + "var confirmationMessage = 'Please stay!';"
                        + "e.returnValue = confirmationMessage;"
                        + "return confirmationMessage;" + "});");
    }
}