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());
}
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");
}
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;
}
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();
}
}
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());
}
Aggregations