public void testImageInsideCollection() throws Exception { execute("CRUD.new"); execute("Collection.new", "viewObject=xava_view_section0_ingredients"); execute("ImageEditor.changeImage", "newImageProperty=image"); assertNoErrors(); assertAction("LoadImage.loadImage"); String imageUrl = System.getProperty("user.dir") + "/test-images/cake.gif"; setFileValue("newImage", imageUrl); execute("LoadImage.loadImage"); assertNoErrors(); HtmlPage page = (HtmlPage) getWebClient().getCurrentWindow().getEnclosedPage(); URL url = page.getWebResponse().getRequestSettings().getUrl(); String urlPrefix = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort(); HtmlImage image = (HtmlImage) page.getElementsByName(decorateId("image")).get(0); String imageURL = null; if (image.getSrcAttribute().startsWith("/")) { imageURL = urlPrefix + image.getSrcAttribute(); } else { String urlBase = Strings.noLastToken(url.getPath(), "/"); imageURL = urlPrefix + urlBase + image.getSrcAttribute(); } WebResponse response = getWebClient().getPage(imageURL).getWebResponse(); assertTrue("Image not obtained", response.getContentAsString().length() > 0); assertEquals("Result is not an image", "image", response.getContentType()); }
/** Calls the wrapped webconnection and save the received response. {@inheritDoc} */ @Override public WebResponse getResponse(final WebRequest request) throws IOException { WebResponse response = wrappedWebConnection_.getResponse(request); if (isUncompressJavaScript() && isJavaScript(response.getContentType())) { response = uncompressJavaScript(response); } saveResponse(response, request); return response; }
/** * Saves the response content in the temp dir and adds it to the summary page. * * @param response the response to save * @param request the request used to get the response * @throws IOException if a problem occurs writing the file */ protected void saveResponse(final WebResponse response, final WebRequest request) throws IOException { counter_++; final String extension = chooseExtension(response.getContentType()); final File f = createFile(request.getUrl(), extension); final InputStream input = response.getContentAsStream(); final OutputStream output = new FileOutputStream(f); int length = 0; try { length = IOUtils.copy(input, output); } catch (final EOFException e) { // ignore } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } final URL url = response.getWebRequest().getUrl(); LOG.info("Created file " + f.getAbsolutePath() + " for response " + counter_ + ": " + url); final StringBuilder buffer = new StringBuilder(); buffer.append("tab[tab.length] = {code: " + response.getStatusCode() + ", "); buffer.append("fileName: '" + f.getName() + "', "); buffer.append("contentType: '" + response.getContentType() + "', "); buffer.append("method: '" + request.getHttpMethod().name() + "', "); if (request.getHttpMethod() == HttpMethod.POST && request.getEncodingType() == FormEncodingType.URL_ENCODED) { buffer.append( "postParameters: " + nameValueListToJsMap(request.getRequestParameters()) + ", "); } buffer.append("url: '" + escapeJSString(url.toString()) + "', "); buffer.append("loadTime: " + response.getLoadTime() + ", "); buffer.append("responseSize: " + length + ", "); buffer.append("responseHeaders: " + nameValueListToJsMap(response.getResponseHeaders())); buffer.append("};\n"); appendToJSFile(buffer.toString()); }
public void testChangeImage() throws Exception { addImage(); HtmlPage page = (HtmlPage) getWebClient().getCurrentWindow().getEnclosedPage(); URL url = page.getWebResponse().getRequestSettings().getUrl(); String urlPrefix = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort(); HtmlImage image = (HtmlImage) page.getElementsByName(decorateId("photo")).get(0); String imageURL = null; if (image.getSrcAttribute().startsWith("/")) { imageURL = urlPrefix + image.getSrcAttribute(); } else { String urlBase = Strings.noLastToken(url.getPath(), "/"); imageURL = urlPrefix + urlBase + image.getSrcAttribute(); } WebResponse response = getWebClient().getPage(imageURL).getWebResponse(); assertTrue("Image not obtained", response.getContentAsString().length() > 0); assertEquals("Result is not an image", "image", response.getContentType()); }