/**
   * 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);
    }
  }
Exemple #2
0
  @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"));
  }
 @Override
 protected void doSomething(HtmlElement element, HtmlOption option) {
   if (option.isSelected()) {
     option.setSelected(false);
   }
 }