use of com.gargoylesoftware.htmlunit.html.HtmlOption in project jenkins by jenkinsci.
the class ParametersTest method choiceWithLTGT.
@Test
public void choiceWithLTGT() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
ParametersDefinitionProperty pdp = new ParametersDefinitionProperty(new ChoiceParameterDefinition("choice", "Choice 1\nChoice <2>", "choice description"));
project.addProperty(pdp);
CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
project.getBuildersList().add(builder);
WebClient wc = j.createWebClient().withThrowExceptionOnFailingStatusCode(false);
HtmlPage page = wc.goTo("job/" + project.getName() + "/build?delay=0sec");
HtmlForm form = page.getFormByName("parameters");
HtmlElement element = (HtmlElement) (form.getElementsByAttribute("input", "name", "name")).get(0).getParentNode();
assertNotNull(element);
assertEquals("choice description", ((HtmlElement) DomNodeUtil.selectSingleNode(form, "//div[contains(@class, 'jenkins-form-description')]")).getTextContent());
assertEquals("choice", ((HtmlElement) DomNodeUtil.selectSingleNode(form, "//div[contains(@class, 'jenkins-form-label')]")).getTextContent());
HtmlSelect choiceSelect = (HtmlSelect) form.getElementsByAttribute("select", "name", "value").get(0);
HtmlOption opt = DomNodeUtil.selectSingleNode(choiceSelect, "option[@value='Choice <2>']");
assertNotNull(opt);
assertEquals("Choice <2>", opt.asNormalizedText());
opt.setSelected(true);
j.submit(form);
j.waitUntilNoActivity();
assertNotNull(builder.getEnvVars());
assertEquals("Choice <2>", builder.getEnvVars().get("CHOICE"));
}
use of com.gargoylesoftware.htmlunit.html.HtmlOption in project jenkins by jenkinsci.
the class OptionTest method callPageAndCheckIfResultContainsExpected.
private void callPageAndCheckIfResultContainsExpected(String url, String bodyContainsExpected, String valueContainsExpected, boolean checkExactCharacters) throws Exception {
HtmlPage page = (HtmlPage) j.createWebClient().goTo(url, null);
String responseContent = page.getWebResponse().getContentAsString();
if (checkExactCharacters) {
// in this mode, we check the data directly received by the response,
// without any un-escaping done by HtmlElement
// first value shown as value
int indexOfValue = responseContent.indexOf(valueContainsExpected);
assertNotEquals(-1, indexOfValue);
// second as body
int indexOfBody = responseContent.indexOf(bodyContainsExpected, indexOfValue + 1);
assertNotEquals(-1, indexOfBody);
// also check there is no "<script>" present in the answer
int indexOfScript = responseContent.indexOf("<script>");
assertEquals(-1, indexOfScript);
} else {
// in this mode, we check the content as displayed to the user, converting all the escaped characters to
// their un-escaped equivalent, done by com.gargoylesoftware.htmlunit.html.HtmlSerializer#cleanUp(String)
HtmlElement document = page.getDocumentElement();
DomNodeList<HtmlElement> elements = document.getElementsByTagName("option");
assertEquals(1, elements.size());
HtmlOption option = (HtmlOption) elements.get(0);
// without that check, the getValueAttribute could return getText if the value is not present
assertNotEquals(DomElement.ATTRIBUTE_NOT_DEFINED, option.getAttribute("value"));
assertTrue("Value attribute does not contain the expected value", option.getValueAttribute().contains(valueContainsExpected));
assertTrue("Body content of the option does not contain the expected value", option.getText().contains(bodyContainsExpected));
}
}
use of com.gargoylesoftware.htmlunit.html.HtmlOption in project faces by jakartaee.
the class Spec1559IT method assertValidMarkup.
private static void assertValidMarkup(HtmlSelect select) {
assertEquals("select has 2 children", 2, select.getChildElementCount());
for (DomElement child : select.getChildElements()) {
assertEquals("child element is an optgroup", "optgroup", child.getNodeName());
assertEquals("child has in turn 3 grandchildren", 3, child.getChildElementCount());
for (DomElement grandchild : child.getChildElements()) {
assertEquals("grandchild element is an option", "option", grandchild.getNodeName());
}
}
assertEquals("select element has 6 options", 6, select.getOptions().size());
HtmlOption option2 = select.getOptionByValue("2");
assertEquals("2nd option is 'Cat'", "Cat", option2.getText());
HtmlOption option5 = select.getOptionByValue("5");
assertEquals("5th option is 'Audi'", "Audi", option5.getText());
}
use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.
the class HTMLOptionsCollection method add.
/**
* Adds a new item to the option collection.
*
* <p><b><i>Implementation Note:</i></b> The specification for the JavaScript add() method
* actually calls for the optional newIndex parameter to be an integer. However, the
* newIndex parameter is specified as an Object here rather than an int because of the
* way Rhino and HtmlUnit process optional parameters for the JavaScript method calls.
* If the newIndex parameter were specified as an int, then the Undefined value for an
* integer is specified as NaN (Not A Number, which is a Double value), but Rhino
* translates this value into 0 (perhaps correctly?) when converting NaN into an int.
* As a result, when the newIndex parameter is not specified, it is impossible to make
* a distinction between a caller of the form add(someObject) and add (someObject, 0).
* Since the behavior of these two call forms is different, the newIndex parameter is
* specified as an Object. If the newIndex parameter is not specified by the actual
* JavaScript code being run, then newIndex is of type net.sourceforge.htmlunit.corejs.javascript.Undefined.
* If the newIndex parameter is specified, then it should be of type java.lang.Number and
* can be converted into an integer value.</p>
*
* <p>This method will call the {@link #put(int, Scriptable, Object)} method for actually
* adding the element to the collection.</p>
*
* <p>According to <a href="http://msdn.microsoft.com/en-us/library/ms535921.aspx">the
* Microsoft DHTML reference page for the JavaScript add() method of the options collection</a>,
* the index parameter is specified as follows:
* <p>
* <i>Optional. Integer that specifies the index position in the collection where the element is
* placed. If no value is given, the method places the element at the end of the collection.</i>
*
* @param newOptionObject the DomNode to insert in the collection
* @param beforeOptionObject An optional parameter which specifies the index position in the
* collection where the element is placed. If no value is given, the method places
* the element at the end of the collection.
*
* @see #put(int, Scriptable, Object)
*/
@JsxFunction
public void add(final Object newOptionObject, final Object beforeOptionObject) {
final HtmlOption htmlOption = (HtmlOption) ((HTMLOptionElement) newOptionObject).getDomNodeOrNull();
HtmlOption beforeOption = null;
// If newIndex was specified, then use it
if (beforeOptionObject instanceof Number) {
final int index = ((Integer) Context.jsToJava(beforeOptionObject, Integer.class)).intValue();
if (index < 0 || index >= getLength()) {
// Add a new option at the end.
htmlSelect_.appendOption(htmlOption);
return;
}
beforeOption = (HtmlOption) ((HTMLOptionElement) item(index)).getDomNodeOrDie();
} else if (beforeOptionObject instanceof HTMLOptionElement) {
beforeOption = (HtmlOption) ((HTMLOptionElement) beforeOptionObject).getDomNodeOrDie();
if (beforeOption.getParentNode() != htmlSelect_) {
throw new EvaluatorException("Unknown option.");
}
}
if (null == beforeOption) {
htmlSelect_.appendOption(htmlOption);
return;
}
beforeOption.insertBefore(htmlOption);
}
use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.
the class HtmlSerializerNormalizedText method appendSelect.
/**
* Process {@link HtmlSelect}.
*
* @param builder the StringBuilder to add to
* @param htmlSelect the target to process
*/
protected void appendSelect(final HtmlSerializerTextBuilder builder, final HtmlSelect htmlSelect) {
final List<HtmlOption> options = htmlSelect.getSelectedOptions();
for (final Iterator<HtmlOption> i = options.iterator(); i.hasNext(); ) {
final HtmlOption currentOption = i.next();
appendChildren(builder, currentOption);
if (i.hasNext()) {
builder.appendBlockSeparator();
}
}
}
Aggregations