Example #1
0
  private void isMapClick(
      final String imgId,
      final Boolean samePage,
      final String urlSuffixClick,
      final String urlSuffixClickXY)
      throws Exception {

    final String htmlContent =
        "<html><head><title>foo</title></head><body>\n"
            + "<a href='http://server/foo'>\n"
            + "<img id='img1' src='foo.png' ismap>\n"
            + "<img id='img2' src='foo.png'>\n"
            + "</a>\n"
            + "<img id='img3' src='foo.png' ismap>\n"
            + "<img id='img4' src='foo.png'>\n"
            + "</body></html>";
    final HtmlPage page = loadPage(htmlContent);

    final HtmlImage img = page.getHtmlElementById(imgId);

    final Page page2 = img.click();
    Assert.assertEquals("same page after click", samePage, Boolean.valueOf(page == page2));
    if (!samePage.booleanValue()) {
      assertEquals(
          "http://server/foo" + urlSuffixClick, page2.getWebResponse().getWebRequest().getUrl());
    }

    final Page page3 = img.click(25, 30);
    Assert.assertEquals("same page after click(25, 30)", samePage, Boolean.valueOf(page == page3));
    if (!samePage.booleanValue()) {
      assertEquals(
          "http://server/foo" + urlSuffixClickXY, page3.getWebResponse().getWebRequest().getUrl());
    }
  }
Example #2
0
  /** @throws Exception if the test fails */
  @Test
  public void asXml() throws Exception {
    final String content =
        "<html><head><title>foo</title></head><body>\n"
            + "<img id='img1' src='foo.png'>"
            + "<img id='img2' name='testName' src='foo.png' alt='young'>"
            + "<img id='img3' src='foo.png' width='11' height='17px' >"
            + "<img id='img4' src='foo.png' width='11em' height='17%' >"
            + "</body></html>";
    final HtmlPage page = loadPage(content);

    HtmlImage img = page.getHtmlElementById("img1");
    String expected = "<img id=\"img1\" src=\"foo.png\"/>";
    assertEquals(expected, img.asXml().trim());

    img = page.getHtmlElementById("img2");
    expected = "<img id=\"img2\" name=\"testName\" src=\"foo.png\" alt=\"young\"/>";
    assertEquals(expected, img.asXml().trim());

    img = page.getHtmlElementById("img3");
    expected = "<img id=\"img3\" src=\"foo.png\" width=\"11\" height=\"17px\"/>";
    assertEquals(expected, img.asXml().trim());

    img = page.getHtmlElementById("img4");
    expected = "<img id=\"img4\" src=\"foo.png\" width=\"11em\" height=\"17%\"/>";
    assertEquals(expected, img.asXml().trim());
  }
Example #3
0
  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());
  }
Example #4
0
 /** @throws Exception if the test fails */
 @Test
 public void imageFileSize() throws Exception {
   final HtmlImage htmlimage = getHtmlElementToTest("image1");
   Assert.assertEquals(
       "Image filesize",
       140144,
       IOUtils.toByteArray(htmlimage.getWebResponse(true).getContentAsStream()).length);
 }
Example #5
0
 /**
  * The image should be redownloaded when the src attribute changes.
  *
  * @throws Exception if the test fails
  */
 @Test
 public void redownloadOnSrcAttributeChanged() throws Exception {
   final HtmlImage htmlimage = getHtmlElementToTest("image1");
   final ImageReader imagereader = htmlimage.getImageReader();
   htmlimage.setAttribute("src", htmlimage.getAttribute("src") + "#changed");
   Assert.assertFalse(
       "Src attribute changed but ImageReader was not reloaded",
       imagereader.equals(htmlimage.getImageReader()));
 }
Example #6
0
 /** @throws Exception if the test fails */
 @Test
 public void getWebResponse() throws Exception {
   final HtmlImage htmlimage = getHtmlElementToTest("image1");
   final URL url = htmlimage.getPage().getWebResponse().getWebRequest().getUrl();
   Assert.assertNull(htmlimage.getWebResponse(false));
   final WebResponse resp = htmlimage.getWebResponse(true);
   Assert.assertNotNull(resp);
   assertEquals(url.toExternalForm(), resp.getWebRequest().getAdditionalHeaders().get("Referer"));
 }
Example #7
0
 /** @throws Exception if the test fails */
 @Test
 public void getImageReaderNoneSupportedImage() throws Exception {
   final HtmlImage htmlimage = getHtmlElementToTest("image1");
   final String url = "/HtmlImageDownloadTest.html";
   htmlimage.setAttribute("src", url);
   try {
     htmlimage.getImageReader();
     Assert.fail("it was not an image!");
   } catch (final IOException ioe) {
     // Correct behavior
   }
 }
Example #8
0
 /**
  * Tests circle radius of percentage value.
  *
  * @throws Exception if the test fails
  */
 @Test
 @NotYetImplemented
 public void useMapClick_CircleRadiusPercentage() throws Exception {
   final String htmlContent =
       "<html><head><title>foo</title></head><body>\n"
           + "<img id='myImg' src='foo.png' usemap='#map1'>\n"
           + "<map name='map1'>\n"
           + "<area href='a.html' shape='rect' coords='5,5,20,20'>\n"
           + "<area href='b.html' shape='circle' coords='25,10,10%'>\n"
           + "</map>\n"
           + "</body></html>";
   final HtmlPage page = loadPage(htmlContent);
   final HtmlImage img = page.getHtmlElementById("myImg");
   img.click(0, 0);
 }
Example #9
0
  /** @throws Exception if the test fails */
  private void useMapClick(final int x, final int y, final String urlSuffix) throws Exception {
    final String htmlContent =
        "<html><head><title>foo</title></head><body>\n"
            + "<img id='myImg' src='foo.png' usemap='#map1'>\n"
            + "<map name='map1'>\n"
            + "<area href='a.html' shape='rect' coords='5,5,20,20'>\n"
            + "<area href='b.html' shape='circle' coords='25,10,10'>\n"
            + "</map>\n"
            + "</body></html>";
    final HtmlPage page = loadPage(htmlContent);

    final HtmlImage img = page.getHtmlElementById("myImg");

    final Page page2 = img.click(x, y);
    final URL url = page2.getWebResponse().getWebRequest().getUrl();
    assertTrue(url.toExternalForm(), url.toExternalForm().endsWith(urlSuffix));
  }
Example #10
0
  public static HtmlPage solveCaptcha(HtmlPage page) throws Exception {
    System.out.println("Solving captcha...");
    String url = page.getUrl().toString();
    page = wc.getPage(url + "?bypass=1");

    HtmlImage captchaImage = (HtmlImage) page.getElementById("recaptcha_image").getFirstChild();
    captchaImage.saveAs(new File("captcha-image.jpg"));

    CaptchaSolver captchaSolver =
        new CaptchaSolver(new SocketClient("trialaccount", "Cappie1!"), "captcha-image.jpg");
    captchaSolver.run();
    Captcha captcha = captchaSolver.getCaptcha();

    HtmlTextInput textField = (HtmlTextInput) page.getElementById("recaptcha_response_field");
    textField.setValueAttribute(captcha.text);
    HtmlElement element = (HtmlElement) page.getElementByName("submit");
    page = element.click();
    System.out.println("Captcha entered.");
    return wc.getPage(url);
  }
Example #11
0
 /** @throws Exception if the test fails */
 @Test
 public void getImageReader() throws Exception {
   final HtmlImage htmlimage = getHtmlElementToTest("image1");
   Assert.assertNotNull("ImageReader should not be null", htmlimage.getImageReader());
 }
Example #12
0
 /** @throws Exception if the test fails */
 @Test
 public void imageWidth() throws Exception {
   final HtmlImage htmlimage = getHtmlElementToTest("image1");
   Assert.assertEquals("Image width", 879, htmlimage.getWidth());
 }
Example #13
0
 /** @throws Exception if the test fails */
 @Test
 public void imageHeight() throws Exception {
   final HtmlImage htmlimage = getHtmlElementToTest("image1");
   Assert.assertEquals("Image height", 612, htmlimage.getHeight());
 }