Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HtmlUnitDriver alert handling doesn't handle confirms or prompts #1373

Closed
barrypitman opened this issue Dec 13, 2015 · 3 comments
Closed

HtmlUnitDriver alert handling doesn't handle confirms or prompts #1373

barrypitman opened this issue Dec 13, 2015 · 3 comments

Comments

@barrypitman
Copy link

The new alert handling functionality in HtmlUnitDriver (driver.switchTo().alert()) only handles alert boxes, not other modal dialogs such as confirm and prompt. Other drivers handle confirm, alert and prompt. The test case below illustrates this.

I know that I can install a confirmHandler for the HtmlUnit WebClient, but that requires modifying the underlying driver and messages are difficult to retrieve.

import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

/**
 * Handling of javascript popups is implemented inconsistently for HtmlUnitDriver, since v2.47.0
 *
 * @author barry
 * @since 2015/12/13
 */
public class AlertTest {

    @Test
    public void testAlerts() throws Exception {
        testAlertsWithDriver(new ChromeDriver());
        testAlertsWithDriver(new FirefoxDriver());
        testAlertsWithDriver(new HtmlUnitDriver(true)); // test fails for HtmlUnitDriver
    }

    private void testAlertsWithDriver(WebDriver driver) {
        driver.get("https://www.google.com");
        JavascriptExecutor executor = (JavascriptExecutor) driver;

        // alerts are implemented
        String alertMessage = "error!";
        executor.executeScript("alert('" + alertMessage + "')");
        assertThat(driver.switchTo().alert().getText(), equalTo(alertMessage));
        driver.switchTo().alert().accept(); // dismiss the alert

        // but confirm messages aren't
        String confirmMessage = "are you sure?";
        executor.executeScript("confirm('" + confirmMessage + "')");

        // "NoAlertPresentException: No alert was present" thrown for HtmlUnitDriver only
        assertThat(driver.switchTo().alert().getText(), equalTo(confirmMessage));

        driver.switchTo().alert().accept(); // dismiss the confirm

        driver.quit();
    }
}
@tloist
Copy link

tloist commented Mar 22, 2016

I can confirm this, running with selenium-java 2.53.0, htmlunit 2.20 and selenium-htmlunit-driver 2.52.0 however downgrading selenium-htmlunit-driver to 2.46.0 did not work for me.

My test run does something along these lines:

deletionButton.click();
wait.until(ExpectedConditions.alertIsPresent()).accept();

which works fine for Firefox, but not for HtmlUnit, where it gives me the following error:

om.gargoylesoftware.htmlunit.javascript.host.Window confirm
WARNUNG: window.confirm("Are you sure you want to delete this entry?
") no confirm handler installed, simulating the OK button

org.openqa.selenium.TimeoutException: Timed out after 2 seconds waiting for alert to be present

which seems logically, assuming that it auto closes the alert (see log "simulating the OK button") and then waits for the alert it just auto-closed unsuccessfully.

@trumpetx
Copy link

trumpetx commented May 26, 2016

I can confirm on v.2.52.0 - HTMLUnit not handling .accept(). Found this issue when researching.

Here is my very brute force workaround:

public abstract class MyAbstractTest {
    protected WebDriver webDriver;
    private WebClient webClient;

    // ...

    @Before
    public void setup( )
    {
        webDriver = new org.openqa.selenium.htmlunit.HtmlUnitDriver(){
            @Override
            protected WebClient modifyWebClient(WebClient webClient){
                org.apache.commons.logging.LogFactory.getFactory( ).setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
                java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
                java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(java.util.logging.Level.OFF);
                AbstractHtmlUnitTest.this.webClient = webClient;
                return webClient;
            }
        };
        ((org.openqa.selenium.htmlunit.HtmlUnitDriver) webDriver).setJavascriptEnabled(true);

        //webDriver = new org.openqa.selenium.firefox.FirefoxDriver( );
    }

    // ...

    protected void clickLinkAndHandleConfirm(By by, final boolean acceptConfirm)
            throws IOException {
        // Firefox, or other driver
        if (webClient == null) {
            clickLink(by);
            Alert alert = new WebDriverWait(webDriver, DEFAULT_WAIT_SECONDS).until(ExpectedConditions.alertIsPresent());
            if (acceptConfirm) {
                alert.accept();
            } else {
                alert.dismiss();
            }
        } 
        // HTMLUnit Driver
        else {
            webClient.setConfirmHandler(new ConfirmHandler() {
                @Override
                public boolean handleConfirm(Page page, String message) {
                    return acceptConfirm;
                }
            });
            clickLink(by);
            webClient.setConfirmHandler(null);
        }
    }

    // ...
}
``

@lukeis
Copy link
Member

lukeis commented May 26, 2016

HtmlUnitDriver issues should now be tracked on the separate project:
https://github.com/SeleniumHQ/htmlunit-driver

please log a new issue there

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants