/** * JavaScript constructor. This must be declared in every JavaScript file because the rhino engine * won't walk up the hierarchy looking for constructors. * * @param newText the text * @param newValue the value * @param defaultSelected Whether the option is initially selected * @param selected the current selection state of the option */ public void jsConstructor( final String newText, final String newValue, final boolean defaultSelected, final boolean selected) { final HtmlPage page = (HtmlPage) getWindow().getWebWindow().getEnclosedPage(); AttributesImpl attributes = null; if (defaultSelected) { attributes = new AttributesImpl(); attributes.addAttribute(null, "selected", "selected", null, "selected"); } final HtmlOption htmlOption = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME) .createElement(page, HtmlOption.TAG_NAME, attributes); htmlOption.setSelected(selected); setDomNode(htmlOption); if (newText != null && !newText.equals("undefined")) { htmlOption.appendChild(new DomText(page, newText)); } if (newValue != null && !newValue.equals("undefined")) { htmlOption.setValueAttribute(newValue); } }
public static List<String> getOptionTexts(final HtmlSelect select) { final List<HtmlOption> options = select.getOptions(); final List<String> ret = new ArrayList<String>(options.size()); for (final HtmlOption option : options) { ret.add(option.getText()); } return ret; }
/** * @param select * @param text * @return */ public static HtmlOption getOptionByTextValue(final HtmlSelect select, final String text) { final List<HtmlOption> options = select.getOptions(); for (final HtmlOption option : options) { if (text.equalsIgnoreCase(option.getText())) { return option; } } return null; }
@Test public void errorDropdownIsPresentAndIsNotEmpty() throws Exception { JenkinsRule.WebClient wc = j.createWebClient(); wc.login("user1", "user1"); HtmlPage page = wc.goTo("job/x/" + build.getNumber()); page.getElementById("claim").click(); HtmlForm form = page.getFormByName("claim"); HtmlSelect select = form.getSelectByName("_.errors"); HashSet<String> set = new HashSet<String>(); for (HtmlOption option : select.getOptions()) { set.add(option.getValueAttribute()); } assertTrue(set.contains("Default")); assertTrue(set.contains(CAUSE_NAME_2)); assertTrue(set.contains(CAUSE_NAME_1)); }
@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(); wc.setThrowExceptionOnFailingStatusCode(false); HtmlPage page = wc.goTo("job/" + project.getName() + "/build?delay=0sec"); HtmlForm form = page.getFormByName("parameters"); HtmlElement element = (HtmlElement) form.selectSingleNode(".//tr[td/div/input/@value='choice']"); assertNotNull(element); assertEquals( "choice description", ((HtmlElement) element .getNextSibling() .getNextSibling() .selectSingleNode("td[@class='setting-description']")) .getTextContent()); assertEquals( "choice", ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent()); HtmlOption opt = (HtmlOption) element.selectSingleNode("td/div/select/option[@value='Choice <2>']"); assertNotNull(opt); assertEquals("Choice <2>", opt.asText()); opt.setSelected(true); j.submit(form); Queue.Item q = j.jenkins.getQueue().getItem(project); if (q != null) q.getFuture().get(); else Thread.sleep(1000); assertNotNull(builder.getEnvVars()); assertEquals("Choice <2>", builder.getEnvVars().get("CHOICE")); }
/** * Adds the option (and create the associated DOM node if needed) before the specified one or at * the end if the specified one in null. * * @param newOptionObject the new option to add * @param beforeOption the option that should be after the option to add */ protected void addBefore(final HTMLOptionElement newOptionObject, final HtmlOption beforeOption) { final HtmlSelect select = getHtmlSelect(); HtmlOption htmlOption = newOptionObject.getDomNodeOrNull(); if (htmlOption == null) { htmlOption = (HtmlOption) HTMLParser.getFactory(HtmlOption.TAG_NAME) .createElement(select.getPage(), HtmlOption.TAG_NAME, null); } if (beforeOption == null) { select.appendChild(htmlOption); } else { beforeOption.insertBefore(htmlOption); } }
@Override protected void doSomething(HtmlElement element, HtmlOption option) { if (option.isSelected()) { option.setSelected(false); } }