com.gargoylesoftware.htmlunit.html.HtmlSelect Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlSelect. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Window.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private boolean matches(final Object object) {
    if (object instanceof HtmlEmbed
        || object instanceof HtmlForm
        || object instanceof HtmlImage
        || object instanceof HtmlObject) {
        return true;
    }

    return includeFormFields_
            && (object instanceof HtmlAnchor
                || object instanceof HtmlButton
                || object instanceof HtmlInput
                || object instanceof HtmlMap
                || object instanceof HtmlSelect
                || object instanceof HtmlTextArea);
}
 
Example #2
Source File: AbstractOIDCTest.java    From cxf-fediz with Apache License 2.0 6 votes vote down vote up
private static HtmlPage registerClient(HtmlPage registerPage,
                                        String clientName, String redirectURI,
                                        String clientAudience,
                                        String logoutURI,
                                        boolean confidential) throws IOException {
    final HtmlForm form = registerPage.getForms().get(0);

    // Set new client values
    final HtmlTextInput clientNameInput = form.getInputByName("client_name");
    clientNameInput.setValueAttribute(clientName);
    final HtmlSelect clientTypeSelect = form.getSelectByName("client_type");
    clientTypeSelect.setSelectedAttribute(confidential ? "confidential" : "public", true);
    final HtmlTextInput redirectURIInput = form.getInputByName("client_redirectURI");
    redirectURIInput.setValueAttribute(redirectURI);
    final HtmlTextInput clientAudienceURIInput = form.getInputByName("client_audience");
    clientAudienceURIInput.setValueAttribute(clientAudience);
    final HtmlTextInput clientLogoutURI = form.getInputByName("client_logoutURI");
    clientLogoutURI.setValueAttribute(logoutURI);

    final HtmlButton button = form.getButtonByName("submit_button");
    return button.click();
}
 
Example #3
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private boolean matches(final Object object) {
    if (object instanceof HtmlEmbed
        || object instanceof HtmlForm
        || object instanceof HtmlImage
        || object instanceof HtmlObject) {
        return true;
    }
    if (includeFormFields_ && (
            object instanceof HtmlAnchor
            || object instanceof HtmlButton
            || object instanceof HtmlInput
            || object instanceof HtmlMap
            || object instanceof HtmlSelect
            || object instanceof HtmlTextArea)) {
        return true;
    }
    return false;
}
 
Example #4
Source File: AbstractOIDCTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testEditClient() throws Exception {
    try (WebClient webClient = setupWebClientIDP("alice", "ecila")) {
        HtmlPage registeredClientPage = login(oidcEndpointBuilder("/console/clients/" + publicClientId),
            webClient);

        final HtmlPage editClientPage = registeredClientPage.getAnchorByText("public-client").click();

        final HtmlForm form = editClientPage.getForms().get(0);

        // Set new client values
        final HtmlTextInput clientNameInput = form.getInputByName("client_name");
        final String newClientName = "public-client-modified";
        clientNameInput.setValueAttribute(newClientName);
        final HtmlSelect clientTypeSelect = form.getSelectByName("client_type");
        assertTrue(clientTypeSelect.isDisabled());
        final HtmlTextInput redirectURIInput = form.getInputByName("client_redirectURI");
        assertEquals(REDIRECT_URL, redirectURIInput.getText());
        final HtmlTextInput clientAudienceURIInput = form.getInputByName("client_audience");
        assertEquals("https://ws.apache.org", clientAudienceURIInput.getText());
        final HtmlTextInput clientLogoutURI = form.getInputByName("client_logoutURI");
        assertEquals(LOGOUT_URL, clientLogoutURI.getText());

        registeredClientPage = form.getButtonByName("submit_button").click();
        assertNotNull(registeredClientPage.getAnchorByText(newClientName));

        final HtmlPage registeredClientsPage = registeredClientPage.getAnchorByText("registered Clients").click();

        HtmlTable table = registeredClientsPage.getHtmlElementById("registered_clients");
        assertEquals("2 clients", table.getRows().size(), 3);
        boolean updatedClientFound = false;
        for (final HtmlTableRow row : table.getRows()) {
            if (newClientName.equals(row.getCell(0).asText())) {
                updatedClientFound = true;
                break;
            }
        }
        assertTrue(updatedClientFound);
    }
}
 
Example #5
Source File: HTMLSelectElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the actual value of the selected Option.
 * @return the value
 */
@Override
public String getValue() {
    final HtmlSelect htmlSelect = getHtmlSelect();
    final List<HtmlOption> selectedOptions = htmlSelect.getSelectedOptions();
    if (selectedOptions.isEmpty()) {
        return "";
    }
    return ((HTMLOptionElement) selectedOptions.get(0).getScriptableObject()).getValue();
}
 
Example #6
Source File: HTMLSelectElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the object.
 *
 */
public void initialize() {
    final HtmlSelect htmlSelect = getHtmlSelect();
    htmlSelect.setScriptableObject(this);
    if (optionsArray_ == null) {
        optionsArray_ = new HTMLOptionsCollection(this);
        optionsArray_.initialize(htmlSelect);
    }
}
 
Example #7
Source File: HTMLOptionElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@code index} property.
 * @return the {@code index} property
 */
@JsxGetter
public int getIndex() {
    final HtmlOption optionNode = (HtmlOption) getDomNodeOrNull();
    if (optionNode != null) {
        final HtmlSelect enclosingSelect = optionNode.getEnclosingSelect();
        if (enclosingSelect != null) {
            return enclosingSelect.indexOf(optionNode);
        }
    }
    return 0;
}
 
Example #8
Source File: HTMLOptionElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the value of the {@code selected} property.
 * @param selected the new selected property
 */
@JsxSetter
public void setSelected(final boolean selected) {
    final HtmlOption optionNode = (HtmlOption) getDomNodeOrNull();
    final HtmlSelect enclosingSelect = optionNode.getEnclosingSelect();
    if (!selected && optionNode.isSelected()
            && enclosingSelect != null && !enclosingSelect.isMultipleSelectEnabled()) {
        enclosingSelect.getOption(0).setSelectedFromJavaScript(true);
    }
    else {
        optionNode.setSelectedFromJavaScript(selected);
    }
}
 
Example #9
Source File: HTMLSelectElement2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Test for bug 1159709.
 * @throws Exception if the test fails
 */
@Test
public void rightPageAfterOnchange() throws Exception {
    final String html
        = "<html><body>\n"
        + "<iframe src='fooIFrame.html'></iframe>\n"
        + "<form name='form1' action='http://first' method='post'>\n"
        + "  <select name='select1' onchange='this.form.submit()'>\n"
        + "    <option value='option1' selected='true' name='option1'>One</option>\n"
        + "    <option value='option2' name='option2'>Two</option>\n"
        + "  </select>\n"
        + "</form>\n"
        + "</body></html>";

    final WebClient webClient = getWebClient();
    final MockWebConnection webConnection = new MockWebConnection();

    webConnection.setDefaultResponse("<html><body></body></html>");
    webConnection.setResponse(URL_FIRST, html);
    webClient.setWebConnection(webConnection);

    final HtmlPage page = webClient.getPage(URL_FIRST);
    final HtmlForm form = page.getFormByName("form1");
    final HtmlSelect select = form.getSelectByName("select1");
    final Page page2 = select.setSelectedAttribute("option2", true);
    assertEquals("http://first/", page2.getUrl());
}
 
Example #10
Source File: HTMLSelectElement2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Changes made through JS should not trigger an onchange event.
 * @throws Exception if the test fails
 */
@Test
public void noOnchangeFromJS() throws Exception {
    final String html = "<html><head><title>Test infinite loop on js onchange</title></head>\n"
        + "<body><form name='myForm'>\n"
        + "<select name='a' onchange='this.form.b.selectedIndex=0'>\n"
        + "<option value='1'>one</option>\n"
        + "<option value='2'>two</option>\n"
        + "</select>\n"
        + "<select name='b' onchange='alert(\"b changed\")'>\n"
        + "<option value='G'>green</option>\n"
        + "<option value='R' selected>red</option>\n"
        + "</select>\n"
        + "</form>\n"
        + "</body>\n"
        + "</html>";
    final List<String> collectedAlerts = new ArrayList<>();
    final HtmlPage page = loadPage(html, collectedAlerts);
    final HtmlSelect selectA = page.getFormByName("myForm").getSelectByName("a");
    final HtmlOption optionA2 = selectA.getOption(1);

    assertEquals("two", optionA2.asText());

    final HtmlSelect selectB = page.getFormByName("myForm").getSelectByName("b");
    assertEquals(1, selectB.getSelectedOptions().size());
    assertEquals("red", selectB.getSelectedOptions().get(0).asText());

     // changed selection in first select
    optionA2.setSelected(true);
    assertTrue(optionA2.isSelected());
    assertEquals(1, selectB.getSelectedOptions().size());
    assertEquals("green", selectB.getSelectedOptions().get(0).asText());

    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
 
Example #11
Source File: HTMLOptionElementTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts({"value1", "text1", "label1", "value2", "text2", "text2"})
public void label() throws Exception {
    final String html
        = "<html><head><title>foo</title><script>\n"
        + "function test() {\n"
        + "  var s = document.getElementById('testSelect');\n"
        + "  var lastIndex = s.length;\n"
        + "  s.length += 1;\n"
        + "  s[lastIndex].value = 'value2';\n"
        + "  s[lastIndex].text  = 'text2';\n"
        + "  alert(s[0].value);\n"
        + "  alert(s[0].text);\n"
        + "  alert(s[0].label);\n"
        + "  alert(s[1].value);\n"
        + "  alert(s[1].text);\n"
        + "  alert(s[1].label);\n"
        + "}\n"
        + "</script></head><body onload='test()'>\n"
        + "  <select id='testSelect'>\n"
        + "    <option value='value1' label='label1'>text1</option>\n"
        + "  </select>\n"
        + "</form>\n"
        + "</body></html>";

    final HtmlPage page = loadPageWithAlerts(html);
    final HtmlSelect select = page.getHtmlElementById("testSelect");
    assertEquals("value1", select.getOption(0).getValueAttribute());
    assertEquals("text1", select.getOption(0).getTextContent());
    assertEquals("label1", select.getOption(0).getLabelAttribute());
    assertEquals("value2", select.getOption(1).getValueAttribute());
    assertEquals("text2", select.getOption(1).getTextContent());
    assertEquals("text2", select.getOption(1).getLabelAttribute());
}
 
Example #12
Source File: HTMLSelectElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the actual value of the selected Option.
 * @return the value
 */
@Override
@JsxGetter
public String getValue() {
    final HtmlSelect htmlSelect = getHtmlSelect();
    final List<HtmlOption> selectedOptions = htmlSelect.getSelectedOptions();
    if (selectedOptions.isEmpty()) {
        return "";
    }
    return ((HTMLOptionElement) selectedOptions.get(0).getScriptableObject()).getValue();
}
 
Example #13
Source File: HTMLSelectElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the object.
 *
 */
public void initialize() {
    final HtmlSelect htmlSelect = getHtmlSelect();
    htmlSelect.setScriptableObject(this);
    if (optionsArray_ == null) {
        optionsArray_ = new HTMLOptionsCollection(this);
        optionsArray_.initialize(htmlSelect);
    }
}
 
Example #14
Source File: HTMLOptionElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@code index} property.
 * @return the {@code index} property
 */
@JsxGetter
public int getIndex() {
    final HtmlOption optionNode = (HtmlOption) getDomNodeOrNull();
    if (optionNode != null) {
        final HtmlSelect enclosingSelect = optionNode.getEnclosingSelect();
        if (enclosingSelect != null) {
            return enclosingSelect.indexOf(optionNode);
        }
    }
    return 0;
}
 
Example #15
Source File: HTMLOptionElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the value of the {@code selected} property.
 * @param selected the new selected property
 */
@JsxSetter
public void setSelected(final boolean selected) {
    final HtmlOption optionNode = (HtmlOption) getDomNodeOrNull();
    final HtmlSelect enclosingSelect = optionNode.getEnclosingSelect();
    if (!selected && optionNode.isSelected()
            && enclosingSelect != null && !enclosingSelect.isMultipleSelectEnabled()) {
        enclosingSelect.getOption(0).setSelectedFromJavaScript(true);
    }
    else {
        optionNode.setSelectedFromJavaScript(selected);
    }
}
 
Example #16
Source File: HTMLFormElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the value of the property {@code elements}.
 * @return the value of this property
 */
@JsxGetter
public HTMLCollection getElements() {
    final HtmlForm htmlForm = getHtmlForm();

    return new HTMLCollection(htmlForm, false) {
        private boolean filterChildrenOfNestedForms_;

        @Override
        protected List<DomNode> computeElements() {
            final List<DomNode> response = super.computeElements();
            // it would be more performant to avoid iterating through
            // nested forms but as it is a corner case of ill formed HTML
            // the needed refactoring would take too much time
            // => filter here and not in isMatching as it won't be needed in most
            // of the cases
            if (filterChildrenOfNestedForms_) {
                for (final Iterator<DomNode> iter = response.iterator(); iter.hasNext();) {
                    final HtmlElement field = (HtmlElement) iter.next();
                    if (field.getEnclosingForm() != htmlForm) {
                        iter.remove();
                    }
                }
            }
            response.addAll(htmlForm.getLostChildren());
            return response;
        }

        @Override
        protected Object getWithPreemption(final String name) {
            return HTMLFormElement.this.getWithPreemption(name);
        }

        @Override
        public EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
            return EffectOnCache.NONE;
        }

        @Override
        protected boolean isMatching(final DomNode node) {
            if (node instanceof HtmlForm) {
                filterChildrenOfNestedForms_ = true;
                return false;
            }

            return node instanceof HtmlInput || node instanceof HtmlButton
                    || node instanceof HtmlTextArea || node instanceof HtmlSelect;
        }
    };
}
 
Example #17
Source File: ComputedCSSStyleDeclaration.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
 * elements.
 *
 * @return the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
 *         elements
 */
private int getEmptyHeight() {
    if (height2_ != null) {
        return height2_.intValue();
    }

    final DomNode node = getElement().getDomNodeOrDie();
    if (!node.mayBeDisplayed()) {
        height2_ = Integer.valueOf(0);
        return 0;
    }

    if ("none".equals(getDisplay())) {
        height2_ = Integer.valueOf(0);
        return 0;
    }

    final Element elem = getElement();
    final int windowHeight = elem.getWindow().getWebWindow().getInnerHeight();

    if (elem instanceof HTMLBodyElement) {
        height2_ = windowHeight;
        return windowHeight;
    }

    final boolean explicitHeightSpecified = !super.getHeight().isEmpty();

    int defaultHeight;
    if (node instanceof HtmlDivision && StringUtils.isBlank(node.getTextContent())) {
        defaultHeight = 0;
    }
    else if (elem.getFirstChild() == null) {
        if (node instanceof HtmlRadioButtonInput || node instanceof HtmlCheckBoxInput) {
            defaultHeight = 13;
        }
        else if (node instanceof HtmlButton) {
            defaultHeight = 20;
        }
        else if (node instanceof HtmlInput && !(node instanceof HtmlHiddenInput)) {
            if (getBrowserVersion().hasFeature(JS_CLIENTHIGHT_INPUT_17)) {
                defaultHeight = 17;
            }
            else {
                defaultHeight = 21;
            }
        }
        else if (node instanceof HtmlSelect) {
            defaultHeight = 20;
        }
        else if (node instanceof HtmlTextArea) {
            defaultHeight = 49;
        }
        else if (node instanceof HtmlInlineFrame) {
            defaultHeight = 154;
        }
        else {
            defaultHeight = 0;
        }
    }
    else {
        defaultHeight = getBrowserVersion().getFontHeight(getFontSize());
        if (node instanceof HtmlDivision) {
            defaultHeight *= StringUtils.countMatches(node.asText(), '\n') + 1;
        }
    }

    final int defaultWindowHeight = elem instanceof HTMLCanvasElement ? 150 : windowHeight;

    int height = pixelValue(elem, new CssValue(defaultHeight, defaultWindowHeight) {
        @Override public String get(final ComputedCSSStyleDeclaration style) {
            final Element element = style.getElement();
            if (element instanceof HTMLBodyElement) {
                return String.valueOf(element.getWindow().getWebWindow().getInnerHeight());
            }
            return style.getStyleAttribute(HEIGHT, true);
        }
    });

    if (height == 0 && !explicitHeightSpecified) {
        height = defaultHeight;
    }

    height2_ = Integer.valueOf(height);
    return height;
}
 
Example #18
Source File: HTMLOptionsCollection.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes this object.
 * @param select the HtmlSelect that this object will retrieve elements from
 */
public void initialize(final HtmlSelect select) {
    WebAssert.notNull("select", select);
    htmlSelect_ = select;
}
 
Example #19
Source File: HTMLOptionsCollection.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes this object.
 * @param select the HtmlSelect that this object will retrieve elements from
 */
public void initialize(final HtmlSelect select) {
    WebAssert.notNull("select", select);
    htmlSelect_ = select;
}
 
Example #20
Source File: HTMLFormElement.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the value of the property {@code elements}.
 * @return the value of this property
 */
@JsxGetter
public HTMLCollection getElements() {
    final HtmlForm htmlForm = getHtmlForm();

    return new HTMLCollection(htmlForm, false) {
        private boolean filterChildrenOfNestedForms_;

        @Override
        protected List<DomNode> computeElements() {
            final List<DomNode> response = super.computeElements();
            // it would be more performant to avoid iterating through
            // nested forms but as it is a corner case of ill formed HTML
            // the needed refactoring would take too much time
            // => filter here and not in isMatching as it won't be needed in most
            // of the cases
            if (filterChildrenOfNestedForms_) {
                for (final Iterator<DomNode> iter = response.iterator(); iter.hasNext();) {
                    final HtmlElement field = (HtmlElement) iter.next();
                    if (field.getEnclosingForm() != htmlForm) {
                        iter.remove();
                    }
                }
            }
            response.addAll(htmlForm.getLostChildren());
            return response;
        }

        @Override
        protected Object getWithPreemption(final String name) {
            return HTMLFormElement.this.getWithPreemption(name);
        }

        @Override
        public EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
            return EffectOnCache.NONE;
        }

        @Override
        protected boolean isMatching(final DomNode node) {
            if (node instanceof HtmlForm) {
                filterChildrenOfNestedForms_ = true;
                return false;
            }

            return node instanceof HtmlInput || node instanceof HtmlButton
                    || node instanceof HtmlTextArea || node instanceof HtmlSelect;
        }
    };
}
 
Example #21
Source File: ComputedCSSStyleDeclaration.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
 * elements.
 *
 * @return the element's calculated height taking relevant CSS into account, but <b>not</b> the element's child
 *         elements
 */
private int getEmptyHeight() {
    if (height2_ != null) {
        return height2_.intValue();
    }

    final DomNode node = getElement().getDomNodeOrDie();
    if (!node.mayBeDisplayed()) {
        height2_ = Integer.valueOf(0);
        return 0;
    }

    if (NONE.equals(getDisplay())) {
        height2_ = Integer.valueOf(0);
        return 0;
    }

    final Element elem = getElement();
    final int windowHeight = elem.getWindow().getWebWindow().getInnerHeight();

    if (elem instanceof HTMLBodyElement) {
        height2_ = windowHeight;
        return windowHeight;
    }

    final boolean explicitHeightSpecified = !super.getHeight().isEmpty();

    int defaultHeight;
    if (node instanceof HtmlDivision && StringUtils.isBlank(node.getTextContent())) {
        defaultHeight = 0;
    }
    else if (elem.getFirstChild() == null) {
        if (node instanceof HtmlRadioButtonInput || node instanceof HtmlCheckBoxInput) {
            defaultHeight = 13;
        }
        else if (node instanceof HtmlButton) {
            defaultHeight = 20;
        }
        else if (node instanceof HtmlInput && !(node instanceof HtmlHiddenInput)) {
            final BrowserVersion browser = getBrowserVersion();
            if (browser.hasFeature(JS_CLIENTHIGHT_INPUT_17)) {
                defaultHeight = 17;
            }
            else if (browser.hasFeature(JS_CLIENTHIGHT_INPUT_21)) {
                defaultHeight = 21;
            }
            else {
                defaultHeight = 20;
            }
        }
        else if (node instanceof HtmlSelect) {
            defaultHeight = 20;
        }
        else if (node instanceof HtmlTextArea) {
            defaultHeight = 49;
        }
        else if (node instanceof HtmlInlineFrame) {
            defaultHeight = 154;
        }
        else {
            defaultHeight = 0;
        }
    }
    else {
        defaultHeight = getBrowserVersion().getFontHeight(getFontSize());
        if (node instanceof HtmlDivision) {
            defaultHeight *= StringUtils.countMatches(node.asText(), '\n') + 1;
        }
    }

    final int defaultWindowHeight = elem instanceof HTMLCanvasElement ? 150 : windowHeight;

    int height = pixelValue(elem, new CssValue(defaultHeight, defaultWindowHeight) {
        @Override public String get(final ComputedCSSStyleDeclaration style) {
            final Element element = style.getElement();
            if (element instanceof HTMLBodyElement) {
                return String.valueOf(element.getWindow().getWebWindow().getInnerHeight());
            }
            return style.getStyleAttribute(HEIGHT, true);
        }
    });

    if (height == 0 && !explicitHeightSpecified) {
        height = defaultHeight;
    }

    height2_ = Integer.valueOf(height);
    return height;
}
 
Example #22
Source File: HTMLSelectElement.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the HTML select object.
 * @return the HTML select object
 */
private HtmlSelect getHtmlSelect() {
    return (HtmlSelect) getDomNodeOrDie();
}
 
Example #23
Source File: HTMLSelectElement.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the HTML select object.
 * @return the HTML select object
 */
private HtmlSelect getHtmlSelect() {
    return (HtmlSelect) getDomNodeOrDie();
}