Giter Site home page Giter Site logo

Comments (18)

asashour avatar asashour commented on July 30, 2024

confirm() and prompt() need interaction with the client code, and this can only be achieved when the client and server run separately. This is not the case with HtmlUnitDriver currently, and this is the most important feature to be implemented.

I am starting a branch for making HtmlUnitDriver a Remote one, any contribution is more than welcome.

from htmlunit-driver.

trumpetx avatar trumpetx commented on July 30, 2024

Here's how I got around it:

    @Before
    public void setup( )
    {
        String driver = System.getProperty("webtest.driver", DEFAULT_WEBDRIVER);
        switch (driver)
        {
            case "firefox":
                webDriver = new org.openqa.selenium.firefox.FirefoxDriver( );
                break;
            case "htmlunit":
            default :
                webDriver = new org.openqa.selenium.htmlunit.HtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER_11, true)
                {
                    @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);
                        AbstractWebTest.this.htmlUnitWebClient = webClient;
                        return webClient;
                    }
                };
        }
    }
    protected void clickLinkAndHandleConfirm(By by,
                                             final boolean acceptConfirm)
            throws IOException
    {
        // Bug in HtmlUnitWebDriver - confirm dialogs do not work
        if (htmlUnitWebClient != null)
        {
            htmlUnitWebClient.setConfirmHandler(new ConfirmHandler( )
            {
                @Override
                public boolean handleConfirm(Page page,
                                             String message)
                {
                    return acceptConfirm;
                }
            });
            clickLink(by);
            htmlUnitWebClient.setConfirmHandler(null);
        }
        else
        {
            clickLink(by);
            Alert alert = waitUntil(ExpectedConditions.alertIsPresent( ));
            if (acceptConfirm)
            {
                alert.accept( );
            }
            else
            {
                alert.dismiss( );
            }
        }
    }

from htmlunit-driver.

asashour avatar asashour commented on July 30, 2024

In remote branch, all cases of AlertsTest now pass, please give it a try, and provide feedback.

The plan is to test the remote branch against HtmlUnit test suite, and when it passes, it will be merged in master

from htmlunit-driver.

stiemannkj1 avatar stiemannkj1 commented on July 30, 2024

For those stuck on HtmlUnitDriver 2.25 and below, a potential workaround is to disable alerts via JavaScript:

JavascriptExecutor javascriptExecutor = (JavascriptExecutor) webDriver;
javascriptExecutor.executeScript("window.alert = function(message){ console.log(message); };" +
    "window.confirm = function(message){ console.log(message); return true; };");

For more info, see my StackOverflow answer.

from htmlunit-driver.

sangeethmani avatar sangeethmani commented on July 30, 2024

Do we have the functionality to driver.switchTo().window(ChildWindow) available in the HtmlUnit driver? Is it possible to get the branch for the same if available?

from htmlunit-driver.

asashour avatar asashour commented on July 30, 2024

@sangeethmani: for different question, please create a new issue.

Yes, the method is available, use latest version, and provide more details if you encounter an issue.

from htmlunit-driver.

barrypitman avatar barrypitman commented on July 30, 2024

@asashour Just checking in - has the remote branch been merged into master? I'm struggling to find a release that I can use which addresses this issue. The latest available release is 2.27. Should I rather build my own version from the master branch? Thanks

from htmlunit-driver.

sangeethmani avatar sangeethmani commented on July 30, 2024

I cannot find the version 2.27 in the maven repository. Why arn't the latest versions available in the maven repos?

from htmlunit-driver.

sangeethmani avatar sangeethmani commented on July 30, 2024

sorry has the artifact id changed from selenium-htmlunit-driver->htmlunit-driver.. so that's the difference between the 2.52 and the 2.27 version?

https://github.com/SeleniumHQ/htmlunit-driver

from htmlunit-driver.

asashour avatar asashour commented on July 30, 2024

The remote branch was merged, but the removed for performance reasons, however the master supports the same function, without the intermediate server.

The latest release is

 <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>htmlunit-driver</artifactId>
    <version>2.27</version>
</dependency>

And there is 2.28-snapshot in here

from htmlunit-driver.

barrypitman avatar barrypitman commented on July 30, 2024

I tried just tried the AlertTest (at the top of this issue) using the latest snapshot (htmlunit-driver-2.28-20171004.075130-9), but its still failing with the same error message, 'NoAlertPresentException: No alert was present'

from htmlunit-driver.

asashour avatar asashour commented on July 30, 2024

I had a local error, ClassNotFoundException due to the jetty upgrade of HtmlUnit, now it is fixed and a new snapshot is now deployed.

Our reference is the build server, and it was green, please check your environment.

from htmlunit-driver.

barrypitman avatar barrypitman commented on July 30, 2024

@asashour are you able to get the AlertTest to pass? I've tried with the latest snapshots and still failing, not sure if its because I don't have the right artifacts or not. Thanks!

from htmlunit-driver.

asashour avatar asashour commented on July 30, 2024

Yes, all the AlertsTest pass locally. Make a new project with only HtmlUnitDriver project, or check if you have conflicting dependencies.

from htmlunit-driver.

barrypitman avatar barrypitman commented on July 30, 2024

I'm attaching a simple maven project which uses htmlunit-driver v. 2.28-SNAPSHOT. I've added https://oss.sonatype.org/content/repositories/snapshots as a repository, its pulling down htmlunit-driver-2.28-20171019.070134-10.jar but when I run the AlertTest, it still fails with "No alert was present"
htmlunit-alerts.zip

from htmlunit-driver.

asashour avatar asashour commented on July 30, 2024

There is an error, which needs investigations at HtmlUnit side, please read this

Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot read property "Kf" from undefined
(https://www.google.de/xjs/_/js/k=xjs.s.de.b7c4UjVSNxc.O/m=sx,c,sb,cdos,cr,elog,hsm,jsa,r,d,csi/am=wCLS4wUPQP4_BELqCmGBbGAy0A/rt=j/d=1/t=zcms/rs=ACT90oHaBoshtx_DEpWvv2n0SW6kAJwrpw#767)
	at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4130)
	at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4108)

from htmlunit-driver.

barrypitman avatar barrypitman commented on July 30, 2024

Hi @asashour, I think that error might be related to the javascript executed by the www.google.com page that is loaded during the test that I provided previously.

To eliminate that as the cause, I modified the test to run using locally generated content only. I still get the test failing with the same error message:

/**
 * 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) throws IOException {
        File tempFile = File.createTempFile("fragment", ".html");
        tempFile.deleteOnExit();
        String htmlTemplate = "<html><body><script type='text/javascript'>%s</script></body></html>";

        // alerts are implemented
        String alertMessage = "error!";
        FileUtils.writeStringToFile(tempFile, String.format(htmlTemplate, "alert('" + alertMessage + "');"), Charset.defaultCharset());
        driver.get(tempFile.toURI().toURL().toString());
        assertThat(driver.switchTo().alert().getText(), equalTo(alertMessage));
        driver.switchTo().alert().accept(); // dismiss the alert

        // but confirm messages aren't
        String confirmMessage = "are you sure?";
        FileUtils.writeStringToFile(tempFile, String.format(htmlTemplate, "confirm('" + confirmMessage + "')"), Charset.defaultCharset());
        driver.get(tempFile.toURI().toURL().toString());

        // "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();
    }
}

I do notice this warning message on the console: WARNING: window.confirm("are you sure?") no confirm handler installed, simulating the OK button. However, even if I do install a ConfirmHandler on the WebClient, I don't see how I'd prevent web driver API from throwing a NoAlertPresentException

from htmlunit-driver.

asashour avatar asashour commented on July 30, 2024

Thanks for reporting, confirm() was missing. Now it is fixed, and a new snapshot is available.

from htmlunit-driver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.