Package ca.forklabs.javaxpcom

Examples of ca.forklabs.javaxpcom.Crawler


   @Test
   public void testGetAllChildren() throws Exception {
      String gre_home = "./tools/xulrunner-1.9.0.13-sdk/bin";
      Crawler.setupXULRunner(gre_home);

      Crawler crawler = null;
      try {
         crawler = new Crawler() { /* nothing */ };

         URL url = SelectorTest.class.getResource("/select.all.children.html");
         crawler.navigateTo(url);

         nsIDOMNode document = crawler.getDocument();

         Selector selector = new Selector(document);
         List<nsIDOMNode> children = selector.getAllChildren(document);

//         for (nsIDOMNode child : children) {
//            XPCOMInspector.inspect(child, false);
//            }

         assertEquals(14, children.size());

      // the document node, has one child <html>
         assertEquals(9, children.get(0).getNodeType());
         assertEquals(1, children.get(0).getChildNodes().getLength());

      // the html node, has two children <head>, <body>
         assertEquals("HTML", children.get(1).getNodeName());
         assertEquals(2, children.get(1).getChildNodes().getLength());

      // the head node, has one empty child #text
         assertEquals("HEAD", children.get(2).getNodeName());
         assertEquals(1, children.get(2).getChildNodes().getLength());

         assertEquals(3, children.get(3).getNodeType());

      // the body node, has three children, #text, <p>, #text
         assertEquals("BODY", children.get(4).getNodeName());
         assertEquals(3, children.get(4).getChildNodes().getLength());

      // body -> #text
         assertEquals(3, children.get(5).getNodeType());

      // body -> p, has four children, #text, <em>, #text, <strong>
         assertEquals("P", children.get(6).getNodeName());
         assertEquals(4, children.get(6).getChildNodes().getLength());

      // body -> p -> #text
         assertEquals(3, children.get(7).getNodeType());

      // body -> p -> em, has one child, #text
         assertEquals("EM", children.get(8).getNodeName());
         assertEquals(1, children.get(8).getChildNodes().getLength());

      // body -> p -> em -> #text
         assertEquals(3, children.get(9).getNodeType());

      // body -> p -> #text
         assertEquals(3, children.get(10).getNodeType());

      // body -> p -> strong, has one child, #text
         assertEquals("STRONG", children.get(11).getNodeName());
         assertEquals(1, children.get(11).getChildNodes().getLength());

      // body -> p -> strong -> #text
         assertEquals(3, children.get(12).getNodeType());
         }
      finally {
         if (null != crawler) {
            crawler.teardown();
            }
         }
      }
View Full Code Here


   @Test
   public void testInputSelectors() throws Exception {
      String gre_home = "./tools/xulrunner-1.9.0.13-sdk/bin";
      Crawler.setupXULRunner(gre_home);

      Crawler crawler = null;
      try {
         crawler = new Crawler() { /* nothing */ };

         URL url = SelectorTest.class.getResource("/selector.input.html");
         crawler.navigateTo(url);

         List<nsIDOMNode> buttons = crawler.selector().add(Filters.button()).list();
         assertEquals(2, buttons.size());
         assertEquals("I am a button", XPCOMConverter.getAttributeValue(buttons.get(0), "value"));
         assertEquals("I am also a button", XPCOMConverter.asPlainText(buttons.get(1)));

         List<nsIDOMNode> checkboxes = crawler.selector().add(Filters.checkbox()).list();
         assertEquals(1, checkboxes.size());
         assertEquals("I am a checkbox", XPCOMConverter.getAttributeValue(checkboxes.get(0), "value"));

         List<nsIDOMNode> files = crawler.selector().add(Filters.file()).list();
         assertEquals(1, files.size());

         List<nsIDOMNode> hiddens = crawler.selector().add(Filters.hidden()).list();
         assertEquals(2, hiddens.size());
         assertEquals("I am hidden", XPCOMConverter.getAttributeValue(hiddens.get(0), "value"));
         assertEquals("I am hidden too", XPCOMConverter.getAttributeValue(hiddens.get(1), "value"));

         List<nsIDOMNode> images = crawler.selector().add(Filters.image()).list();
         assertEquals(1, images.size());

         List<nsIDOMNode> inputs = crawler.selector().add(Filters.input()).list();
         assertEquals(15, inputs.size());

         List<nsIDOMNode> passwords = crawler.selector().add(Filters.password()).list();
         assertEquals(1, passwords.size());
         assertEquals("Guess me!", XPCOMConverter.getAttributeValue(passwords.get(0), "value"));

         List<nsIDOMNode> selects = crawler.selector().add(Filters.select()).list();
         assertEquals(1, selects.size());
         assertEquals(3L, selects.get(0).getChildNodes().getLength());

         List<nsIDOMNode> submits = crawler.selector().add(Filters.submit()).list();
         assertEquals(1, submits.size());
         assertEquals("Send this!", XPCOMConverter.getAttributeValue(submits.get(0), "value"));

         List<nsIDOMNode> texts = crawler.selector().add(Filters.text()).list();
         assertEquals(2, texts.size());
         assertEquals("This is text", XPCOMConverter.getAttributeValue(texts.get(0), "value"));
         assertEquals("This is text too!", XPCOMConverter.getAttributeValue(texts.get(1), "value"));

         List<nsIDOMNode> textareas = crawler.selector().add(Filters.textarea()).list();
         assertEquals(1, textareas.size());
         assertEquals("This is even more text", XPCOMConverter.asPlainText(textareas.get(0)));
         }
      finally {
         if (null != crawler) {
            crawler.teardown();
            }
         }
      }
View Full Code Here

   @Test
   public void testBasicFilters() throws Exception {
      String gre_home = "./tools/xulrunner-1.9.0.13-sdk/bin";
      Crawler.setupXULRunner(gre_home);

      Crawler crawler = null;
      try {
         crawler = new Crawler() { /* nothing */ };

         URL url = SelectorTest.class.getResource("/basic.html");
         crawler.navigateTo(url);

         List<nsIDOMNode> css = crawler.selector().add(Filters.css("target")).list();
         assertEquals(2, css.size());
         assertEquals("H3", css.get(0).getNodeName());
         assertEquals("H6", css.get(1).getNodeName());

         List<nsIDOMNode> headers = crawler.selector().add(Filters.headers()).list();
         assertEquals("H1", headers.get(0).getNodeName());
         assertEquals("H2", headers.get(1).getNodeName());
         assertEquals("H2", headers.get(2).getNodeName());
         assertEquals("H3", headers.get(3).getNodeName());
         assertEquals("H4", headers.get(4).getNodeName());
         assertEquals("H5", headers.get(5).getNodeName());
         assertEquals("H6", headers.get(6).getNodeName());

         List<nsIDOMNode> ids = crawler.selector().add(Filters.id("h2")).list();
         assertEquals(1, ids.size());
         assertEquals("H2", ids.get(0).getNodeName());
         }
      finally {
         if (null != crawler) {
            crawler.teardown();
            }
         }
      }
View Full Code Here

   @Test
   public void testGet() throws Exception {
      String gre_home = "./tools/xulrunner-1.9.0.13-sdk/bin";
      Crawler.setupXULRunner(gre_home);

      Crawler crawler = null;
      try {
         crawler = new Crawler() { /* nothing */ };

         URL url = SelectorTest.class.getResource("/basic.html");
         crawler.navigateTo(url);

         List<nsIDOMNode> css = crawler.selector().add(Filters.css("target")).list();
         assertEquals(2, css.size());
         assertEquals("H3", css.get(0).getNodeName());
         assertEquals("H6", css.get(1).getNodeName());

         nsIDOMNode node = crawler.selector().add(Filters.css("target")).get();
         assertEquals("H3", node.getNodeName());
         }
      finally {
         if (null != crawler) {
            crawler.teardown();
            }
         }
      }
View Full Code Here

TOP

Related Classes of ca.forklabs.javaxpcom.Crawler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.