Search in sources :

Example 6 with HtmlOption

use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.

the class HTMLSelectElement2Test method noOnchangeFromJS.

/**
 * 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.asNormalizedText());
    final HtmlSelect selectB = page.getFormByName("myForm").getSelectByName("b");
    assertEquals(1, selectB.getSelectedOptions().size());
    assertEquals("red", selectB.getSelectedOptions().get(0).asNormalizedText());
    // changed selection in first select
    optionA2.setSelected(true);
    assertTrue(optionA2.isSelected());
    assertEquals(1, selectB.getSelectedOptions().size());
    assertEquals("green", selectB.getSelectedOptions().get(0).asNormalizedText());
    assertEquals(Collections.EMPTY_LIST, collectedAlerts);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) HtmlSelect(com.gargoylesoftware.htmlunit.html.HtmlSelect) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) Test(org.junit.Test)

Example 7 with HtmlOption

use of com.gargoylesoftware.htmlunit.html.HtmlOption in project htmlunit by HtmlUnit.

the class CSSStyleSheet method selectsPseudoClass.

private static boolean selectsPseudoClass(final BrowserVersion browserVersion, final Condition condition, final DomElement element, final boolean fromQuerySelectorAll) {
    if (browserVersion.hasFeature(QUERYSELECTORALL_NOT_IN_QUIRKS)) {
        final Object sobj = element.getPage().getScriptableObject();
        if (sobj instanceof HTMLDocument && ((HTMLDocument) sobj).getDocumentMode() < 8) {
            return false;
        }
    }
    final String value = condition.getValue();
    switch(value) {
        case "root":
            return element == element.getPage().getDocumentElement();
        case "enabled":
            return element instanceof DisabledElement && !((DisabledElement) element).isDisabled();
        case "disabled":
            return element instanceof DisabledElement && ((DisabledElement) element).isDisabled();
        case "focus":
            final HtmlPage htmlPage = element.getHtmlPageOrNull();
            if (htmlPage != null) {
                final DomElement focus = htmlPage.getFocusedElement();
                return element == focus;
            }
            return false;
        case "checked":
            return (element instanceof HtmlCheckBoxInput && ((HtmlCheckBoxInput) element).isChecked()) || (element instanceof HtmlRadioButtonInput && ((HtmlRadioButtonInput) element).isChecked() || (element instanceof HtmlOption && ((HtmlOption) element).isSelected()));
        case "required":
            return element instanceof HtmlElement && ((HtmlElement) element).isRequired();
        case "optional":
            return element instanceof HtmlElement && ((HtmlElement) element).isOptional();
        case "first-child":
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            return true;
        case "last-child":
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            return true;
        case "first-of-type":
            final String firstType = element.getNodeName();
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(firstType)) {
                    return false;
                }
            }
            return true;
        case "last-of-type":
            final String lastType = element.getNodeName();
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(lastType)) {
                    return false;
                }
            }
            return true;
        case "only-child":
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            return true;
        case "only-of-type":
            final String type = element.getNodeName();
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(type)) {
                    return false;
                }
            }
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(type)) {
                    return false;
                }
            }
            return true;
        case "valid":
            return element instanceof HtmlElement && ((HtmlElement) element).isValid();
        case "invalid":
            return element instanceof HtmlElement && !((HtmlElement) element).isValid();
        case "empty":
            return isEmpty(element);
        case "target":
            final String ref = element.getPage().getUrl().getRef();
            return StringUtils.isNotBlank(ref) && ref.equals(element.getId());
        case "hover":
            return element.isMouseOver();
        case "placeholder-shown":
            if (browserVersion.hasFeature(CSS_PSEUDO_SELECTOR_PLACEHOLDER_SHOWN)) {
                return element instanceof HtmlInput && StringUtils.isEmpty(((HtmlInput) element).getValueAttribute()) && StringUtils.isNotEmpty(((HtmlInput) element).getPlaceholder());
            }
        case "-ms-input-placeholder":
            if (browserVersion.hasFeature(CSS_PSEUDO_SELECTOR_MS_PLACEHHOLDER)) {
                return element instanceof HtmlInput && StringUtils.isEmpty(((HtmlInput) element).getValueAttribute()) && StringUtils.isNotEmpty(((HtmlInput) element).getPlaceholder());
            }
        default:
            if (value.startsWith("nth-child(")) {
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (DomNode n = element; n != null; n = n.getPreviousSibling()) {
                    if (n instanceof DomElement) {
                        index++;
                    }
                }
                return getNth(nth, index);
            } else if (value.startsWith("nth-last-child(")) {
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (DomNode n = element; n != null; n = n.getNextSibling()) {
                    if (n instanceof DomElement) {
                        index++;
                    }
                }
                return getNth(nth, index);
            } else if (value.startsWith("nth-of-type(")) {
                final String nthType = element.getNodeName();
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (DomNode n = element; n != null; n = n.getPreviousSibling()) {
                    if (n instanceof DomElement && n.getNodeName().equals(nthType)) {
                        index++;
                    }
                }
                return getNth(nth, index);
            } else if (value.startsWith("nth-last-of-type(")) {
                final String nthLastType = element.getNodeName();
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (DomNode n = element; n != null; n = n.getNextSibling()) {
                    if (n instanceof DomElement && n.getNodeName().equals(nthLastType)) {
                        index++;
                    }
                }
                return getNth(nth, index);
            } else if (value.startsWith("not(")) {
                final String selectors = value.substring(value.indexOf('(') + 1, value.length() - 1);
                final AtomicBoolean errorOccured = new AtomicBoolean(false);
                final CSSErrorHandler errorHandler = new CSSErrorHandler() {

                    @Override
                    public void warning(final CSSParseException exception) throws CSSException {
                    // ignore
                    }

                    @Override
                    public void fatalError(final CSSParseException exception) throws CSSException {
                        errorOccured.set(true);
                    }

                    @Override
                    public void error(final CSSParseException exception) throws CSSException {
                        errorOccured.set(true);
                    }
                };
                final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
                parser.setErrorHandler(errorHandler);
                try {
                    final SelectorList selectorList = parser.parseSelectors(selectors);
                    if (errorOccured.get() || selectorList == null || selectorList.size() != 1) {
                        throw new CSSException("Invalid selectors: " + selectors);
                    }
                    validateSelectors(selectorList, 9, element);
                    return !selects(browserVersion, selectorList.get(0), element, null, fromQuerySelectorAll);
                } catch (final IOException e) {
                    throw new CSSException("Error parsing CSS selectors from '" + selectors + "': " + e.getMessage());
                }
            }
            return false;
    }
}
Also used : DisabledElement(com.gargoylesoftware.htmlunit.html.DisabledElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) IOException(java.io.IOException) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) HtmlCheckBoxInput(com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) CSSException(com.gargoylesoftware.css.parser.CSSException) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) HtmlRadioButtonInput(com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput)

Example 8 with HtmlOption

use of com.gargoylesoftware.htmlunit.html.HtmlOption in project jenkins by jenkinsci.

the class ListScmBrowsersTest method check.

private void check(Item p) throws IOException, SAXException {
    HtmlPage page = j.createWebClient().getPage(p, "configure");
    List<HtmlSelect> selects = DomNodeUtil.selectNodes(page, "//select");
    assertTrue(selects.size() > 0);
    for (HtmlSelect select : selects) {
        Set<String> title = new HashSet<>();
        for (HtmlOption o : select.getOptions()) {
            assertTrue("Duplicate entry: " + o.getText(), title.add(o.getText()));
        }
    }
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlSelect(com.gargoylesoftware.htmlunit.html.HtmlSelect) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) HashSet(java.util.HashSet)

Example 9 with HtmlOption

use of com.gargoylesoftware.htmlunit.html.HtmlOption in project jenkins by jenkinsci.

the class RowVisibilityGroupTest method test2.

/**
 * optional block inside the dropdownDescriptorSelector
 */
@Test
public void test2() throws Exception {
    HtmlPage p = j.createWebClient().goTo("self/test2");
    HtmlSelect s = (HtmlSelect) DomNodeUtil.selectSingleNode(p, "//SELECT");
    List<HtmlOption> opts = s.getOptions();
    // those first selections will load additional HTMLs
    s.setSelectedAttribute(opts.get(0), true);
    s.setSelectedAttribute(opts.get(1), true);
    // now select back what's already loaded, to cause the existing elements to be displayed
    s.setSelectedAttribute(opts.get(0), true);
    // make sure that the inner control is still hidden
    List<HtmlInput> textboxes = DomNodeUtil.selectNodes(p, "//INPUT[@name='_.textbox2']");
    assertEquals(2, textboxes.size());
    for (HtmlInput e : textboxes) assertFalse(e.isDisplayed());
    // reveal the text box
    List<HtmlInput> checkboxes = DomNodeUtil.selectNodes(p, "//INPUT[@name='inner']");
    assertEquals(2, checkboxes.size());
    checkboxes.get(0).click();
    assertTrue(textboxes.get(0).isDisplayed());
    textboxes.get(0).type("Budweiser");
    // toggle the selection again
    s.setSelectedAttribute(opts.get(1), true);
    s.setSelectedAttribute(opts.get(0), true);
    // make sure it's still displayed this time
    assertTrue(checkboxes.get(0).isChecked());
    assertTrue(textboxes.get(0).isDisplayed());
    // make sure we get what we expect
    j.submit(p.getFormByName("config"));
    RootActionImpl rootAction = ExtensionList.lookupSingleton(RootActionImpl.class);
    j.assertEqualDataBoundBeans(rootAction.beer, new Beer("", new Nested("Budweiser")));
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlSelect(com.gargoylesoftware.htmlunit.html.HtmlSelect) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) Test(org.junit.Test)

Example 10 with HtmlOption

use of com.gargoylesoftware.htmlunit.html.HtmlOption in project faces by jakartaee.

the class Spec1563IT 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());
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption)

Aggregations

HtmlOption (com.gargoylesoftware.htmlunit.html.HtmlOption)17 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)8 HtmlSelect (com.gargoylesoftware.htmlunit.html.HtmlSelect)8 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)3 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)3 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)3 Test (org.junit.Test)3 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)2 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)2 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)2 HashSet (java.util.HashSet)2 CSSErrorHandler (com.gargoylesoftware.css.parser.CSSErrorHandler)1 CSSException (com.gargoylesoftware.css.parser.CSSException)1 CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)1 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)1 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)1 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)1 ElementNotFoundException (com.gargoylesoftware.htmlunit.ElementNotFoundException)1 DisabledElement (com.gargoylesoftware.htmlunit.html.DisabledElement)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1