Skip to content

Commit

Permalink
[JENKINS-31631] Handle YUI submit <button> clicks
Browse files Browse the repository at this point in the history
(cherry picked from commit 7fdb589)
  • Loading branch information
tfennelly authored and olivergondza committed Nov 24, 2015
1 parent 24785d5 commit 5defc34
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
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 @@ -1342,9 +1343,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 5defc34

Please sign in to comment.