Beispiel #1
0
  @Test
  @Ignore
  public void test01() throws Exception {
    System.out.println("-------------------------------");
    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    try {
      WebClientOptions options = webClient.getOptions();
      options.setThrowExceptionOnFailingStatusCode(false);
      options.setThrowExceptionOnScriptError(false);
      options.setCssEnabled(false);
      options.setJavaScriptEnabled(true);
      options.setTimeout(50000);
      // webClient.setAjaxController(new NicelyResynchronizingAjaxController());
      HtmlPage pageOrgin = webClient.getPage("http://lvyou.baidu.com/jinhua/jingdian");
      Thread.sleep(5000);
      DomNodeList<DomNode> pageNodes = pageOrgin.querySelectorAll(".pagination");
      HtmlDivision pageDiv = (HtmlDivision) pageNodes.get(0);
      DomNodeList<HtmlElement> liElements = pageDiv.getElementsByTagName("li");
      Integer pageSize = liElements.size() - 1;
      for (int pageNow = 0; pageNow < pageSize; pageNow++) {
        DomNodeList<HtmlElement> pageAnchors = liElements.get(pageNow).getElementsByTagName("a");
        HtmlAnchor pageAnchor = (HtmlAnchor) pageAnchors.get(0);
        HtmlPage page = pageAnchor.click();
        Thread.sleep(10000);
        DomElement jViewDom = page.getElementById("J-view-list-container");
        DomNodeList<HtmlElement> lis = jViewDom.getElementsByTagName("li");
        for (HtmlElement li : lis) {
          DomNodeList<DomNode> titleNodes = li.querySelectorAll(".title");
          HtmlAnchor titleAnchor = (HtmlAnchor) titleNodes.get(0);
          System.out.println("---------------标题----------------");
          log.debug("{}", titleAnchor.asText());

          DomNodeList<DomNode> picNodes = li.querySelectorAll(".pic");
          HtmlAnchor picAnchor = (HtmlAnchor) picNodes.get(0);
          System.out.println("---------------详情URL----------------");
          String detailUrl = "http://lvyou.baidu.com" + picAnchor.getAttribute("href");
          log.debug("{}", detailUrl);
          DomNodeList<HtmlElement> imgEelements = picAnchor.getElementsByTagName("img");
          for (HtmlElement imgEelement : imgEelements) {
            System.out.println("---------------图片----------------");
            log.debug("{}", imgEelement.getAttribute("src"));
          }

          DomNodeList<DomNode> sumNodes = li.querySelectorAll(".view-userSays");
          HtmlDivision sumDiv = (HtmlDivision) sumNodes.get(0);
          DomNodeList<HtmlElement> sumElements = sumDiv.getElementsByTagName("p");
          HtmlParagraph sumPara = (HtmlParagraph) sumElements.get(0);
          System.out.println("---------------摘要----------------");
          log.debug("{}", sumPara.asText());
        }
      }
    } finally {
      webClient.close();
    }
    System.out.println("-------------------------------");
  }
  @Test
  @Ignore
  public void test() throws Exception {
    System.out.println("-------------------------------");

    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setCssEnabled(false);
    webClient.getOptions().setJavaScriptEnabled(false);
    HtmlPage page = webClient.getPage("http://news.163.com/domestic/");
    // DomNodeList<HtmlElement> elements = page.getElementBy

    System.out.println("---------------标题----------------");
    DomNodeList<DomNode> domNodes = page.querySelectorAll(".item-top");
    // log.debug("{}", domNodes);
    for (DomNode domNode : domNodes) {
      HtmlDivision htmlDivision = (HtmlDivision) domNode;
      DomNodeList<HtmlElement> aElements = htmlDivision.getElementsByTagName("a");
      HtmlAnchor htmlAnchor = (HtmlAnchor) aElements.get(0);
      // HTMLHeadingElement htmlHeading2 = (HTMLHeadingElement)
      // htmlDivision.getElementsByTagName("h2");
      // HtmlAnchor htmlAnchor = (HtmlAnchor) htmlDivision.getElementsByTagName("a");
      log.debug("{}", htmlAnchor.asText());
      log.debug("{}", htmlAnchor.getAttribute("href"));

      DomNodeList<HtmlElement> pElements = htmlDivision.getElementsByTagName("p");
      HtmlParagraph htmlParagraph = (HtmlParagraph) pElements.get(0);
      log.debug("{}", htmlParagraph.asText());

      DomNodeList<HtmlElement> iEelements = htmlDivision.getElementsByTagName("img");
      for (HtmlElement iEelement : iEelements) {
        log.debug("{}", iEelement.getAttribute("src"));
      }

      String detailUrl = htmlAnchor.getAttribute("href");
      if (detailUrl.equals("http://news.163.com/15/1215/17/BAT2L8RB00014JB6.html#f=dlist")) {
        HtmlPage detailPage = webClient.getPage(detailUrl);
        System.out.println("---------------正文----------------");
        DomElement endTextElement = detailPage.getElementById("endText");
        log.debug("{}", endTextElement.asText());

        System.out.println("---------------图片----------------");
        DomNodeList<DomNode> imgNodes = endTextElement.querySelectorAll(".f_center");
        for (DomNode imgNode : imgNodes) {
          HtmlParagraph imgpara = (HtmlParagraph) imgNode;
          DomNodeList<HtmlElement> endImgs = imgpara.getElementsByTagName("img");
          for (HtmlElement endImg : endImgs) {
            log.debug("{}", endImg.getAttribute("src"));
          }
        }
      }
    }
    webClient.close();
    System.out.println("-------------------------------");
  }
  /**
   * Test component rendering
   *
   * @throws Exception
   */
  public void testRender() throws Exception {
    HtmlPage page = renderView();
    assertNotNull(page);
    // System.out.println(page.asXml());

    HtmlElement div1 = page.getHtmlElementById(stp1.getClientId(facesContext));
    assertNotNull(div1);
    assertEquals("div", div1.getNodeName());
    assertTrue(div1.getAttributeValue("class").contains("rich-stglpanel"));
    assertEquals("", div1.getAttributeValue("style"));

    HtmlDivision div2 =
        (HtmlDivision) page.getHtmlElementById(stp1.getClientId(facesContext) + "_header");
    assertNotNull(div2);
    assertEquals("div", div2.getNodeName());

    String classAttr2 = div2.getAttributeValue("class");
    assertTrue(classAttr2.contains("rich-stglpanel-header"));

    HtmlElement div3 = page.getHtmlElementById(stp1.getClientId(facesContext) + "_switch_on");
    assertNotNull(div3);
    assertEquals("div", div3.getNodeName());

    HtmlElement div5 = page.getHtmlElementById(stp1.getClientId(facesContext) + "_switch_off");
    assertNotNull(div5);
    assertEquals("div", div5.getNodeName());

    HtmlElement div4 = page.getHtmlElementById(stp1.getClientId(facesContext) + "_body");
    assertNotNull(div4);
    assertEquals("div", div4.getNodeName());
    assertEquals("", div4.getAttributeValue("style"));

    try {
      page.getHtmlElementById(openMarker1.getClientId(facesContext));
      assertTrue(false);
    } catch (Throwable t) {
    }

    HtmlElement f1 = page.getHtmlElementById(closeMarker1.getClientId(facesContext));
    assertNotNull(f1);

    HtmlElement f2 = page.getHtmlElementById(openMarker2.getClientId(facesContext));
    assertNotNull(f2);

    try {
      page.getHtmlElementById(closeMarker2.getClientId(facesContext));
      assertTrue(false);
    } catch (Throwable t) {
    }
  }
Beispiel #4
0
  public void testCompositeInsertChildrenNested02() throws Exception {

    HtmlPage page = getPage("/faces/composite/compositeInsertChildrenNesting02.xhtml");
    List<HtmlDivision> divs = new ArrayList<HtmlDivision>(1);
    getAllElementsOfGivenClass(page, divs, HtmlDivision.class);
    assertTrue(Integer.toString(divs.size()), divs.size() == 1);
    HtmlDivision div = divs.get(0);
    int count = 0;
    Class[] expectedElements = {HtmlAnchor.class, HtmlBreak.class};
    for (HtmlElement element : div.getChildElements()) {
      if (count > 2) {
        fail("Expected two children of the div");
      }
      assertTrue(element.getClass().equals(expectedElements[count]));
      count++;
    }
  }
  /** @throws Exception if test fails */
  @Test
  public void changingAttributes() throws Exception {
    final String content =
        "<html><head><title>foo</title></head>\n"
            + "<body>\n"
            + "<div id='testDiv' title='foo'></div>\n"
            + "</body></html>";

    final HtmlPage page = loadPage(content);
    final HtmlDivision div = page.getHtmlElementById("testDiv");

    Assert.assertSame(div, page.getFirstByXPath("//*[@title = 'foo']"));
    assertNull(page.getFirstByXPath("//*[@class = 'design']"));

    div.setAttribute("class", "design");
    Assert.assertSame(div, page.getFirstByXPath("//*[@class = 'design']"));
  }
Beispiel #6
0
 /**
  * Verifies that in IE, the <tt>shiftKey</tt>, <tt>ctrlKey</tt> and <tt>altKey</tt> event
  * attributes are defined for all events, but <tt>metaKey</tt> is not defined for any events.<br>
  * Verifies that in FF, the <tt>shiftKey</tt>, <tt>ctrlKey</tt>, <tt>altKey</tt> and
  * <tt>metaKey</tt> attributes are defined for mouse events only.
  *
  * @throws Exception if an error occurs
  */
 @Test
 @Alerts(
     FF = {
       "object",
       "undefined",
       "undefined",
       "undefined",
       "undefined",
       "object",
       "false",
       "false",
       "false",
       "false"
     },
     IE = {
       "object",
       "false",
       "false",
       "false",
       "undefined",
       "object",
       "false",
       "false",
       "false",
       "undefined"
     })
 public void testKeys() throws Exception {
   final String html =
       "<html><body onload='test(event)'><script>\n"
           + "    function test(e) {\n"
           + "        alert(typeof e);\n"
           + "        alert(e.shiftKey);\n"
           + "        alert(e.ctrlKey);\n"
           + "        alert(e.altKey);\n"
           + "        alert(e.metaKey);\n"
           + "    }\n"
           + "</script>\n"
           + "<div id='div' onclick='test(event)'>abc</div>\n"
           + "</body></html>";
   final List<String> actual = new ArrayList<String>();
   final HtmlPage page = loadPage(getBrowserVersion(), html, actual);
   final HtmlDivision div = page.getHtmlElementById("div");
   div.click();
   assertEquals(Arrays.toString(getExpectedAlerts()), actual.toString());
 }