예제 #1
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());
  }
예제 #2
0
  public static void main(String[] args) throws Exception {

    //    	WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24, "54.186.230.121", 3128);
    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);

    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.setJavaScriptTimeout(10000);
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    webClient.getOptions().setTimeout(10000);

    //    	webClient.getOptions().setJavaScriptEnabled(false);
    //		webClient.getOptions().setAppletEnabled(false);
    //		webClient.getOptions().setCssEnabled(false);

    //		webClient.getOptions().setThrowExceptionOnScriptError(false);
    //		webClient.setJavaScriptTimeout(10000);
    //		webClient.getOptions().setJavaScriptEnabled(true);
    //		webClient.setAjaxController(new NicelyResynchronizingAjaxController());
    //		webClient.getOptions().setTimeout(10000);

    //		webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    //		webClient.getOptions().setThrowExceptionOnScriptError(false);

    HtmlPage currentPage = webClient.getPage("http://www.yandex.ru/");
    //    	HtmlPage currentPage = webClient.getPage("http://www.google.ru");

    //    	HtmlDivision div = currentPage.getHtmlElementById("del_competitors-1_42");

    //    	HtmlElement clickable = (HtmlElement)
    // currentPage.getHtmlElementById("del_competitors-1_42");
    //    	currentPage = (HtmlPage) clickable.click();

    HtmlAnchor advancedSearchAn = currentPage.getAnchorByText("Завести ящик");
    currentPage = advancedSearchAn.click();

    HtmlImage image = currentPage.<HtmlImage>getFirstByXPath("//img[@src='images/ash2008.jpg']");
    currentPage = (HtmlPage) image.click();

    System.out.println(currentPage.asXml());

    //    	HtmlImage image =
    // currentPage.<HtmlImage>getFirstByXPath("//img[@src='images/ash2008.jpg']");
    //    	currentPage = (HtmlPage) image.click();

    //    	HtmlImage imagetosave =
    // currentPage.<HtmlImage>getFirstByXPath("//img[@src='//yastatic.net/www/1.977/yaru/i/logo.png']");

    //    	HtmlImage image = currentPage.<HtmlImage>getHtmlElementById("add_competitors-1_3");
    //    	currentPage = (HtmlPage) image.click();

    //    	File imageFile = new File("test_new.jpg");
    //    	image.saveAs(imageFile);

    //    	System.out.println(currentPage.asXml());

    System.out.println("It is done.");

    webClient.closeAllWindows();
  }
 @Test
 public void checkGeneratedHtmlImageLinks() {
   // Test generated HTML image link
   final List<?> images = getHtmlImageTags();
   assertEquals(1, images.size());
   final HtmlImage img = (HtmlImage) images.get(0);
   Utils.assertGeneratedLinkEquals(
       getUrlPrefix() + "/cbfc517da02d6a64a68e5fea9a5de472f1/img/appIcons/application.png",
       img.getSrcAttribute());
 }
예제 #4
0
 /**
  * @param root
  * @param src
  * @return
  */
 public static HtmlImage getImageBySrc(final HtmlElement root, final String src) {
   final List<HtmlElement> elems = root.getElementsByTagName(HtmlImage.TAG_NAME);
   for (final HtmlElement elem : elems) {
     final HtmlImage img = (HtmlImage) elem;
     if (src.equalsIgnoreCase(img.getSrcAttribute())) {
       return img;
     }
   }
   return null;
 }
예제 #5
0
  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());
  }
예제 #6
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);
  }
예제 #7
0
  public void testDeleteImage() throws Exception {
    addImage();

    execute("ImageEditor.deleteImage", "newImageProperty=photo");
    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("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 obtained", response.getContentAsString().length() == 0);
  }
예제 #8
0
  /**
   * Create a new HTML element with the given tag name.
   *
   * @param tagName the tag name
   * @return the new HTML element, or NOT_FOUND if the tag is not supported
   */
  @JsxFunction
  public Object createElement(String tagName) {
    Object result = NOT_FOUND;
    try {
      final BrowserVersion browserVersion = getBrowserVersion();

      // FF3.6 supports document.createElement('div') or supports document.createElement('<div>')
      // but not document.createElement('<div name="test">')
      // IE9- supports also document.createElement('<div name="test">')
      // FF4+ and IE11 don't support document.createElement('<div>')
      if (browserVersion.hasFeature(BrowserVersionFeatures.JS_DOCUMENT_CREATE_ELEMENT_STRICT)
          && (tagName.contains("<") || tagName.contains(">"))) {
        LOG.info(
            "createElement: Provided string '"
                + tagName
                + "' contains an invalid character; '<' and '>' are not allowed");
        throw Context.reportRuntimeError("String contains an invalid character");
      } else if (!browserVersion.hasFeature(JS_DOCUMENT_CREATE_ELEMENT_EXTENDED_SYNTAX)
          && tagName.startsWith("<")
          && tagName.endsWith(">")) {
        tagName = tagName.substring(1, tagName.length() - 1);

        final Matcher matcher = TAG_NAME_PATTERN.matcher(tagName);
        if (!matcher.matches()) {
          LOG.info(
              "createElement: Provided string '" + tagName + "' contains an invalid character");
          throw Context.reportRuntimeError("String contains an invalid character");
        }
      }

      final SgmlPage page = getPage();
      final org.w3c.dom.Node element;
      if ("comment".equalsIgnoreCase(tagName)
          && browserVersion.hasFeature(JS_DOCUMENT_CREATE_ELEMENT_COMMENT)) {
        element = new DomComment(page, "");
      } else {
        element = page.createElement(tagName);
        if (element instanceof BaseFrameElement) {
          ((BaseFrameElement) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlInput) {
          ((HtmlInput) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlImage) {
          ((HtmlImage) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlKeygen) {
          ((HtmlKeygen) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlRp) {
          ((HtmlRp) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlRt) {
          ((HtmlRt) element).markAsCreatedByJavascript();
        } else if (element instanceof HtmlUnknownElement) {
          ((HtmlUnknownElement) element).markAsCreatedByJavascript();
        }
      }
      final Object jsElement;
      if ("event".equalsIgnoreCase(tagName)
          && browserVersion.hasFeature(JS_DOCUMENT_CREATE_ELEMENT_COMMENT)) {
        jsElement = new SimpleScriptable();
        ((SimpleScriptable) jsElement).setClassName("Object");
        ((SimpleScriptable) jsElement).setParentScope(window_);
      } else {
        jsElement = getScriptableFor(element);
      }

      if (jsElement == NOT_FOUND) {
        if (LOG.isDebugEnabled()) {
          LOG.debug(
              "createElement("
                  + tagName
                  + ") cannot return a result as there isn't a JavaScript object for the element "
                  + element.getClass().getName());
        }
      } else {
        result = jsElement;
      }
    } catch (final ElementNotFoundException e) {
      // Just fall through - result is already set to NOT_FOUND
    }
    return result;
  }