R
R
Rusikosan2018-03-12 19:07:02
Java
Rusikosan, 2018-03-12 19:07:02

How Selenium + Java add window.onbeforeunload event handler?

Good time of the day gentlemen. A question. I am weak in autotests, but I got autotests that need to be updated. When I run tests, depending on the situation, sometimes the beforeunload event fires in the browser, which does not allow the browser to be closed and the WebBrowser.quit () method cannot be executed completely. On stackoverflow, I found a suggestion that you need to hang the selenium.runScript("window.onbeforeunload = null;") script , but I can't figure out which part.
There is a base class from which classes with tests are already inherited:

public class FormBaseTest {

    @BeforeMethod(alwaysRun = true)
    public void setUpMethod() {
        String testName = getClass().getDeclaredMethods()[0].getName();
        System.out.println(testName + " started");
        getDriver().manage().window().maximize();
        WebBrowser.implicitWait();
    }

    @AfterMethod(alwaysRun = true)
    public void tearDownMethod() {
        String testName = getClass().getDeclaredMethods()[0].getName();
        System.out.println(testName + " ended");
        System.out.println("");
        WebBrowser.quit();
    }

    @AfterTest(alwaysRun = true)

    public void tearDown() {
        WebBrowser.quit();
    }

I will be glad to help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rusikosan, 2018-03-16
@Rusikosan

In general, I overcame this problem like this (part of the code):

@AfterMethod(alwaysRun = true)
    public void tearDownMethod() {
        String testName = getClass().getDeclaredMethods()[0].getName();
        System.out.println(testName + " ended");
        System.out.println("");
        JavascriptExecutor jse = (JavascriptExecutor)getDriver();
        jse.executeScript("window.onbeforeunload = null;");
        WebBrowser.quit();
    }

    @AfterTest(alwaysRun = true)

    public void tearDown() {
        JavascriptExecutor jse = (JavascriptExecutor)getDriver();
        jse.executeScript("window.onbeforeunload = null;");
        WebBrowser.quit();

Maybe someone will help. Good luck

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question