Comments (18)
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.
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.
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.
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.
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.
@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.
@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.
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.
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.
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.
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.
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.
@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.
Yes, all the AlertsTest
pass locally. Make a new project with only HtmlUnitDriver
project, or check if you have conflicting dependencies.
from htmlunit-driver.
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.
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.
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.
Thanks for reporting, confirm()
was missing. Now it is fixed, and a new snapshot is available.
from htmlunit-driver.
Related Issues (20)
- Driver not switching tabs and windows HOT 3
- NoSuchMethodError after quitting or closing driver and then re-calling it? HOT 6
- Angular support for htmlunitdriver ? HOT 1
- Breaking API changes in 4.8.1 HOT 9
- getDomProperty("value") does return "null" whereas getAttribute("value") returns a value HOT 9
- Selenium driver HOT 1
- Test HOT 1
- To ignore - Error during JavaScript execution HOT 3
- How to setting proxy authorization with username/passwd in ubuntu-server 18.04 env HOT 8
- org.htmlunit.ScriptException: SyntaxError: Empty JSON string HOT 9
- HtmlUnitDriver needs to provide WebDriverInfo and DriverService service providers for Selenium 4 Grid HOT 4
- Searching with RelativeLocator does not work HOT 3
- Threading problem when using multiple frames
- Implement HasAuthentication to be able to use authentication HOT 1
- elem.getAttribute("src") should return an empty string if no src attribute is present
- elem.getAttribute("xxx") should return null if no 'xxx' attribute is present
- XPath Not Working Via document.evaluate HOT 4
- Rhino not compiling any JavaScript when ScriptException encountered HOT 7
- Runasync causing deadlock?
- Fetching innerHTML HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from htmlunit-driver.