Skip to content

Commit

Permalink
Merge pull request #1918 from tfennelly/htmlunit-fixes
Browse files Browse the repository at this point in the history
[Fixed JENKINS-31631] Handle YUI submit <button> clicks
  • Loading branch information
tfennelly committed Nov 19, 2015
2 parents d00dc71 + 7fdb589 commit 2327618
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
6 changes: 6 additions & 0 deletions test/pom.xml
Expand Up @@ -150,6 +150,12 @@ THE SOFTWARE.
<groupId>org.jvnet.hudson</groupId>
<artifactId>embedded-rhino-debugger</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>org.jvnet.hudson</groupId>
<artifactId>htmlunit-core-js</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<!-- for testing JNLP launch. -->
Expand Down
Expand Up @@ -28,6 +28,9 @@
import com.gargoylesoftware.htmlunit.WebClientUtil;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
* {@link HtmlElement} helper methods.
Expand Down Expand Up @@ -58,4 +61,16 @@ public static Page click(HtmlElement element) throws IOException {
WebClientUtil.waitForJSExec(webClient);
}
}

/**
* Does the supplied element define the specified HTML "class" name.
* @param element The element to check.
* @param className The HTML "class" name to check for.
* @return {@code true} if the element defines the specified class, otherwise {@code false}.
*/
public static boolean hasClassName(HtmlElement element, String className) {
String classAttribute = element.getAttribute("class");
Set<String> classes = new HashSet<>(Arrays.asList(classAttribute.split(" ")));
return classes.contains(className);
}
}
10 changes: 7 additions & 3 deletions test/src/main/java/org/jvnet/hudson/test/JenkinsRule.java
Expand Up @@ -37,6 +37,7 @@
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlElementUtil;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
Expand Down Expand Up @@ -1341,9 +1342,12 @@ public HtmlPage submit(HtmlForm form) throws Exception {
*/
public HtmlPage submit(HtmlForm form, String name) throws Exception {
for( HtmlElement e : form.getHtmlElementsByTagName("button")) {
HtmlElement p = (HtmlElement)e.getParentNode().getParentNode();
if(e instanceof HtmlButton && p.getAttribute("name").equals(name)) {
return (HtmlPage)HtmlFormUtil.submit(form, (HtmlButton) e);
HtmlElement p = (HtmlElement)e.getParentNode().getParentNode();
if (p.getAttribute("name").equals(name) && HtmlElementUtil.hasClassName(p, "yui-submit-button")) {
// For YUI handled submit buttons, just do a click.
return (HtmlPage) HtmlElementUtil.click(e);
} else if (e.getAttribute("name").equals(name)) {
return (HtmlPage) HtmlFormUtil.submit(form, e);
}
}
throw new AssertionError("No such submit button with the name "+name);
Expand Down

0 comments on commit 2327618

Please sign in to comment.