Search in sources :

Example 1 with HtmlFileInput

use of com.gargoylesoftware.htmlunit.html.HtmlFileInput in project fcrepo by fcrepo.

the class FedoraHtmlResponsesIT method testCreateNewDatastream.

@Test
public void testCreateNewDatastream() throws Exception {
    // can't do this with javascript, because HTMLUnit doesn't speak the HTML5 file api
    final HtmlPage page = javascriptlessWebClient.getPage(serverAddress);
    final HtmlSelect type = (HtmlSelect) page.getElementById("new_mixin");
    type.getOptionByValue("binary").setSelected(true);
    final HtmlFileInput fileInput = (HtmlFileInput) page.getElementById("binary_payload");
    fileInput.setData("abcdef".getBytes());
    fileInput.setContentType("application/pdf");
    final HtmlButton button = (HtmlButton) page.getElementById("btn_action_create");
    button.click();
    // Without Javascript you end up at a blank page with just the newly generated URI as text.
    final Page resultPage = javascriptlessWebClient.getCurrentWindow().getEnclosedPage();
    final String newUri = resultPage.getWebResponse().getContentAsString();
    final Page page1 = javascriptlessWebClient.getPage(newUri + "/" + FCR_METADATA);
    assertTrue(page1.isHtmlPage());
    assertEquals(newUri, ((HtmlPage) page1).getTitleText());
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlSelect(com.gargoylesoftware.htmlunit.html.HtmlSelect) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) HtmlButton(com.gargoylesoftware.htmlunit.html.HtmlButton) Test(org.junit.Test)

Example 2 with HtmlFileInput

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

the class HTMLInputElement method getValue.

/**
 * Returns the value of the JavaScript attribute {@code value}.
 *
 * @return the value of this attribute
 */
@JsxGetter
@Override
public String getValue() {
    final HtmlInput htmlInput = getDomNodeOrDie();
    if (htmlInput instanceof HtmlFileInput) {
        final File[] files = ((HtmlFileInput) htmlInput).getFiles();
        if (files == null || files.length == 0) {
            return ATTRIBUTE_NOT_DEFINED;
        }
        final File first = files[0];
        final String name = first.getName();
        if (name.isEmpty()) {
            return name;
        }
        return "C:\\fakepath\\" + name;
    }
    if (htmlInput instanceof HtmlNumberInput) {
        final HtmlNumberInput htmlNumberInput = (HtmlNumberInput) htmlInput;
        final String valueAttr = htmlInput.getAttributeDirect("value");
        if (!valueAttr.isEmpty()) {
            if ("-".equals(valueAttr) || "+".equals(valueAttr)) {
                return "";
            }
            final int lastPos = valueAttr.length() - 1;
            if (lastPos >= 0 && valueAttr.charAt(lastPos) == '.') {
                if (htmlNumberInput.hasFeature(JS_INPUT_NUMBER_DOT_AT_END_IS_DOUBLE)) {
                    return "";
                }
            }
            try {
                Double.parseDouble(valueAttr);
            } catch (final NumberFormatException e) {
                return "";
            }
        }
    }
    return htmlInput.getAttributeDirect("value");
}
Also used : HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) HtmlNumberInput(com.gargoylesoftware.htmlunit.html.HtmlNumberInput) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) File(java.io.File) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 3 with HtmlFileInput

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

the class ComputedCSSStyleDeclaration method getCalculatedWidth.

private int getCalculatedWidth() {
    if (width_ != null) {
        return width_.intValue();
    }
    final Element element = getElement();
    final DomNode node = element.getDomNodeOrDie();
    if (!node.mayBeDisplayed()) {
        width_ = Integer.valueOf(0);
        return 0;
    }
    final String display = getDisplay();
    if (NONE.equals(display)) {
        width_ = Integer.valueOf(0);
        return 0;
    }
    final int width;
    final String styleWidth = super.getWidth();
    final DomNode parent = node.getParentNode();
    // width is ignored for inline elements
    if (("inline".equals(display) || StringUtils.isEmpty(styleWidth)) && parent instanceof HtmlElement) {
        // hack: TODO find a way to specify default values for different tags
        if (element instanceof HTMLCanvasElement) {
            return 300;
        }
        // Width not explicitly set.
        final String cssFloat = getCssFloat();
        if ("right".equals(cssFloat) || "left".equals(cssFloat) || ABSOLUTE.equals(getStyleAttribute(POSITION, true))) {
            // We're floating; simplistic approximation: text content * pixels per character.
            width = node.getVisibleText().length() * getBrowserVersion().getPixesPerChar();
        } else if (BLOCK.equals(display)) {
            final int windowWidth = element.getWindow().getWebWindow().getInnerWidth();
            if (element instanceof HTMLBodyElement) {
                width = windowWidth - 16;
            } else {
                // Block elements take up 100% of the parent's width.
                final HTMLElement parentJS = parent.getScriptableObject();
                width = pixelValue(parentJS, new CssValue(0, windowWidth) {

                    @Override
                    public String get(final ComputedCSSStyleDeclaration style) {
                        return style.getWidth();
                    }
                }) - (getBorderHorizontal() + getPaddingHorizontal());
            }
        } else if (node instanceof HtmlSubmitInput || node instanceof HtmlResetInput || node instanceof HtmlButtonInput || node instanceof HtmlButton || node instanceof HtmlFileInput) {
            // use asNormalizedText() here because getVisibleText() returns an empty string
            // for submit and reset buttons
            final String text = node.asNormalizedText();
            // default font for buttons is a bit smaller than the body font size
            width = 10 + (int) (text.length() * getBrowserVersion().getPixesPerChar() * 0.9);
        } else if (node instanceof HtmlTextInput || node instanceof HtmlPasswordInput) {
            final BrowserVersion browserVersion = getBrowserVersion();
            if (browserVersion.hasFeature(JS_CLIENTWIDTH_INPUT_TEXT_143)) {
                return 143;
            }
            if (browserVersion.hasFeature(JS_CLIENTWIDTH_INPUT_TEXT_173)) {
                return 173;
            }
            // FF
            width = 145;
        } else if (node instanceof HtmlRadioButtonInput || node instanceof HtmlCheckBoxInput) {
            final BrowserVersion browserVersion = getBrowserVersion();
            if (browserVersion.hasFeature(JS_CLIENTWIDTH_RADIO_CHECKBOX_10)) {
                width = 10;
            } else {
                width = 13;
            }
        } else if (node instanceof HtmlTextArea) {
            // wild guess
            width = 100;
        } else if (node instanceof HtmlImage) {
            width = ((HtmlImage) node).getWidthOrDefault();
        } else {
            // Inline elements take up however much space is required by their children.
            width = getContentWidth();
        }
    } else if (AUTO.equals(styleWidth)) {
        width = element.getWindow().getWebWindow().getInnerWidth();
    } else {
        // Width explicitly set in the style attribute, or there was no parent to provide guidance.
        width = pixelValue(element, new CssValue(0, element.getWindow().getWebWindow().getInnerWidth()) {

            @Override
            public String get(final ComputedCSSStyleDeclaration style) {
                return style.getStyleAttribute(WIDTH, true);
            }
        });
    }
    width_ = Integer.valueOf(width);
    return width;
}
Also used : HtmlImage(com.gargoylesoftware.htmlunit.html.HtmlImage) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HtmlTextInput(com.gargoylesoftware.htmlunit.html.HtmlTextInput) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) Element(com.gargoylesoftware.htmlunit.javascript.host.Element) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HTMLDataElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDataElement) HTMLDivElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDivElement) HTMLSlotElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLSlotElement) HTMLImageElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLImageElement) HTMLUnknownElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLUnknownElement) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) StyleElement(com.gargoylesoftware.htmlunit.css.StyleElement) HTMLTimeElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLTimeElement) HTMLIFrameElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLIFrameElement) HTMLOutputElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLOutputElement) HTMLLegendElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLLegendElement) HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) HtmlPasswordInput(com.gargoylesoftware.htmlunit.html.HtmlPasswordInput) AttributedString(java.text.AttributedString) HtmlResetInput(com.gargoylesoftware.htmlunit.html.HtmlResetInput) HtmlButtonInput(com.gargoylesoftware.htmlunit.html.HtmlButtonInput) HtmlButton(com.gargoylesoftware.htmlunit.html.HtmlButton) HtmlCheckBoxInput(com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlSubmitInput(com.gargoylesoftware.htmlunit.html.HtmlSubmitInput) HTMLBodyElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLBodyElement) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) HTMLCanvasElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCanvasElement) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) HtmlRadioButtonInput(com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput) HtmlTextArea(com.gargoylesoftware.htmlunit.html.HtmlTextArea)

Example 4 with HtmlFileInput

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

the class QueueTest method fileItemPersistence.

@Test
public void fileItemPersistence() throws Exception {
    // TODO: write a synchronous connector?
    byte[] testData = new byte[1024];
    for (int i = 0; i < testData.length; i++) testData[i] = (byte) i;
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    server.addConnector(connector);
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(new ServletHolder(new FileItemPersistenceTestServlet()), "/");
    server.setHandler(handler);
    server.start();
    try {
        JenkinsRule.WebClient wc = r.createWebClient();
        @SuppressWarnings("deprecation") HtmlPage p = (HtmlPage) wc.getPage("http://localhost:" + connector.getLocalPort() + '/');
        HtmlForm f = p.getFormByName("main");
        HtmlFileInput input = (HtmlFileInput) f.getInputByName("test");
        input.setData(testData);
        HtmlFormUtil.submit(f);
    } finally {
        server.stop();
    }
}
Also used : ServletHandler(org.eclipse.jetty.servlet.ServletHandler) Server(org.eclipse.jetty.server.Server) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) ServerConnector(org.eclipse.jetty.server.ServerConnector) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) Test(org.junit.Test)

Example 5 with HtmlFileInput

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

the class Spec1555IT method testMultipleSelection.

private void testMultipleSelection(String form) throws Exception {
    HtmlPage page = webClient.getPage(webUrl + "spec1555IT.xhtml");
    HtmlFileInput input = page.getHtmlElementById(form + ":input");
    assertEquals("Multiple attribute is set", "multiple", input.getAttribute("multiple"));
    File file1 = generateTempFile("file1", "bin", 123);
    File file2 = generateTempFile("file2", "bin", 234);
    File file3 = generateTempFile("file3", "bin", 345);
    input.setValueAttribute(file1.getAbsolutePath());
    addValueAttribute(input, file2.getAbsolutePath());
    addValueAttribute(input, file3.getAbsolutePath());
    page = page.getHtmlElementById(form + ":submit").click();
    assertEquals("Value attribute is NOT set", "", page.getHtmlElementById(form + ":input").getAttribute("value"));
    HtmlElement messages = page.getHtmlElementById("messages");
    assertEquals("There are 3 messages", 3, messages.getChildElementCount());
    Iterator<DomElement> iterator = messages.getChildElements().iterator();
    DomElement message1 = iterator.next();
    DomElement message2 = iterator.next();
    DomElement message3 = iterator.next();
    assertEquals("First uploaded file has been received", "field: multipleSelection, name: " + file1.getName() + ", size: " + file1.length(), message1.asNormalizedText());
    assertEquals("Second uploaded file has been received", "field: multipleSelection, name: " + file2.getName() + ", size: " + file2.length(), message2.asNormalizedText());
    assertEquals("Third uploaded file has been received", "field: multipleSelection, name: " + file3.getName() + ", size: " + file3.length(), message3.asNormalizedText());
}
Also used : DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) HtmlFileInput(com.gargoylesoftware.htmlunit.html.HtmlFileInput) HtmlInputFile(jakarta.faces.component.html.HtmlInputFile) File(java.io.File)

Aggregations

HtmlFileInput (com.gargoylesoftware.htmlunit.html.HtmlFileInput)11 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)7 Test (org.junit.Test)5 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)4 File (java.io.File)4 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)3 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)3 HtmlButton (com.gargoylesoftware.htmlunit.html.HtmlButton)2 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)2 HtmlSubmitInput (com.gargoylesoftware.htmlunit.html.HtmlSubmitInput)2 HtmlTextInput (com.gargoylesoftware.htmlunit.html.HtmlTextInput)2 JsxGetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)2 HtmlInputFile (jakarta.faces.component.html.HtmlInputFile)2 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)1 Page (com.gargoylesoftware.htmlunit.Page)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 StyleElement (com.gargoylesoftware.htmlunit.css.StyleElement)1 BaseFrameElement (com.gargoylesoftware.htmlunit.html.BaseFrameElement)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlButtonInput (com.gargoylesoftware.htmlunit.html.HtmlButtonInput)1