@Test
  public void testExceptionPhaseListener() throws Exception {
    WebClient client = new WebClient();
    client.setThrowExceptionOnFailingStatusCode(false);

    // First, try a transient conversation

    // Access a page that throws an exception
    client.getPage(getPath("/thunderstorm.jsf"));

    // Then access another page that doesn't and check the contexts are ok
    HtmlPage cloud = client.getPage(getPath("/cloud.jsf"));
    String cloudName = getFirstMatchingElement(cloud, HtmlSpan.class, "cloudName").getTextContent();
    assertEquals(Cloud.NAME, cloudName);

    // Now start a conversation and access the page that throws an exception again
    HtmlPage thunderstorm =
        getFirstMatchingElement(cloud, HtmlSubmitInput.class, "beginConversation").click();
    // This page will error

    String cid = getCid(thunderstorm);

    cloud = client.getPage(getPath("/cloud.jsf", cid));

    // And navigate to another page, checking the conversation exists by verifying that state is
    // maintained
    cloudName = getFirstMatchingElement(cloud, HtmlSpan.class, "cloudName").getTextContent();
    assertEquals("gavin", cloudName);
  }
예제 #2
0
  /**
   * The request context is destroyed at the end of the servlet request, after the service() method
   * and all doFilter() methods, and all requestDestroyed() notifications return.
   */
  @Test
  @SpecAssertions({
    @SpecAssertion(section = REQUEST_CONTEXT_EE, id = "ba"),
    @SpecAssertion(section = REQUEST_CONTEXT_EE, id = "bb"),
    @SpecAssertion(section = REQUEST_CONTEXT_EE, id = "bc")
  })
  public void testRequestScopeIsDestroyedAfterServletRequest() throws Exception {

    WebClient webClient = new WebClient();
    webClient.setThrowExceptionOnFailingStatusCode(true);

    // First request - response content contains SimpleRequestBean id
    TextPage firstRequestResult = webClient.getPage(contextPath + "introspect");
    assertNotNull(firstRequestResult.getContent());
    // Make a second request and make sure the same context is not there (compare SimpleRequestBean
    // ids)
    TextPage secondRequestResult = webClient.getPage(contextPath + "introspect");
    assertNotNull(secondRequestResult.getContent());
    assertNotEquals(
        secondRequestResult.getContent().trim(), firstRequestResult.getContent().trim());

    // Make sure request context is destroyed after service(), doFilter(), requestDestroyed()
    webClient.getPage(contextPath + "introspect?mode=collect");
    ActionSequence correctSequence =
        new ActionSequence()
            .add(IntrospectServlet.class.getName())
            .add(IntrospectFilter.class.getName())
            .add(TestServletRequestListener.class.getName())
            .add(ContextDestructionObserver.class.getName());
    TextPage destroyRequestResult = webClient.getPage(contextPath + "introspect?mode=verify");
    assertNotNull(destroyRequestResult.getContent());
    assertEquals(destroyRequestResult.getContent(), correctSequence.toString());
  }
예제 #3
0
 @Test
 public void testInvalidatedSession() throws Exception {
   HtmlPage page = webClient.getPage(webUrl + "faces/invalidatedSession.xhtml");
   assertTrue(page.asText().indexOf("This is from the @PostConstruct") != -1);
   webClient.getPage(webUrl + "faces/invalidatedPerform.xhtml");
   page = webClient.getPage(webUrl + "faces/invalidatedVerify.xhtml");
   assertTrue(page.asText().indexOf("true") != -1);
 }
예제 #4
0
  @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("-------------------------------");
  }
예제 #5
0
 /**
  * Test to validate empty fields.
  *
  * <p>Note: this test is excluded on Tomcat because the included EL parser requires a System
  * property for this test to work, which would cause problems with other tests. See
  * http://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html and look for COERCE_TO_ZERO
  */
 @Test
 public void testValidateEmptyFields() throws Exception {
   HtmlPage page = webClient.getPage(webUrl + "faces/index.xhtml");
   if (page.getWebResponse().getResponseHeaderValue("Server") == null
       || !page.getWebResponse().getResponseHeaderValue("Server").startsWith("Apache-Coyote")) {
     page = webClient.getPage(webUrl + "faces/validateEmptyFields.xhtml");
     HtmlSubmitInput button = (HtmlSubmitInput) page.getElementById("form:submitButton");
     page = button.click();
     assertTrue(page.asXml().contains("We got called!"));
   }
 }
예제 #6
0
  @Test
  public void testGlobalReturn() throws Exception {
    HtmlPage page = webClient.getPage(webUrl);

    HtmlSubmitInput button = (HtmlSubmitInput) page.getElementById("flow-with-templates");
    page = button.click();

    String pageText = page.getBody().asText();
    assertTrue(pageText.indexOf("Bottom From Template") != -1);
    assertTrue(pageText.indexOf("issue2997Bean") != -1);

    button = (HtmlSubmitInput) page.getElementById("issue2997Home");
    page = button.click();

    pageText = page.getBody().asText();
    assertTrue(pageText.indexOf("Issue2997Home") != -1);
    assertTrue(pageText.indexOf("flow-with-templates") != -1);
    assertTrue(pageText.indexOf("issue2997Bean") != -1);

    page = webClient.getPage(webUrl);

    button = (HtmlSubmitInput) page.getElementById("flow-with-templates");
    page = button.click();

    pageText = page.getBody().asText();
    assertTrue(pageText.indexOf("Bottom From Template") != -1);

    button = (HtmlSubmitInput) page.getElementById("issue2997UserList");
    page = button.click();

    pageText = page.getBody().asText();
    assertTrue(pageText.indexOf("Issue2997UserList") != -1);
    assertTrue(pageText.indexOf("flow-with-templates") != -1);
    assertTrue(pageText.indexOf("issue2997Bean") != -1);

    page = webClient.getPage(webUrl);

    button = (HtmlSubmitInput) page.getElementById("flow-with-templates");
    page = button.click();

    pageText = page.getBody().asText();
    assertTrue(pageText.indexOf("Bottom From Template") != -1);

    button = (HtmlSubmitInput) page.getElementById("issue2997PageInFacesConfig");
    page = button.click();

    pageText = page.getBody().asText();
    assertTrue(pageText.indexOf("Issue2997PageInFacesConfig") != -1);
    assertTrue(pageText.indexOf("flow-with-templates") != -1);
    assertTrue(pageText.indexOf("issue2997Bean") != -1);
  }
예제 #7
0
  /**
   * Static method for <code>Login</code>. Uses dependencies <b>HttpUnit</b> in connecting to Keats.
   *
   * @see com.gargoylesoftware.htmlunit
   * @param parent The parent window. instanceof<code>Scrape</code>, to set relative locations to
   * @param link The url of which to retrieve information from.
   * @param username The username to log in KEATS with.
   * @param password The password to log in KEATS with.
   * @return results.asText() Returns the content of the url if login successful. Returns null
   *     otherwise.
   */
  public static String login(Scrape parent, String link, String username, String password) {
    try {
      WebClient client = new WebClient();

      // Settings
      client.getOptions().setThrowExceptionOnScriptError(false);
      client.getOptions().setThrowExceptionOnScriptError(false);
      client.getOptions().setThrowExceptionOnFailingStatusCode(false);
      client.getOptions().setJavaScriptEnabled(false);
      client.getOptions().setCssEnabled(false);
      client.getOptions().setRedirectEnabled(true);
      client.getOptions().setUseInsecureSSL(true);
      client.getCookieManager().setCookiesEnabled(true);

      HtmlPage page = client.getPage("https://login-keats.kcl.ac.uk/");

      HtmlForm form =
          page.getFirstByXPath("//form[@action='https://keats.kcl.ac.uk/login/index.php']");

      HtmlInput usernameInput = form.getInputByName("username");
      usernameInput.setValueAttribute(username);
      HtmlInput passwordInput = form.getInputByName("password");
      passwordInput.setValueAttribute(password);

      page = form.getInputByValue("Log in").click();

      HtmlPage results = client.getPage(link);

      client.closeAllWindows();
      return results.asText();
    } catch (MalformedURLException e) {
      JOptionPane.showMessageDialog(
          parent,
          "The URL you have provided is not recognised. Please double check your "
              + "input and try again.",
          "MalformedURLException found.",
          JOptionPane.ERROR_MESSAGE);
      e.printStackTrace();
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          parent,
          "An error has occurred when attempting to read information from the "
              + "server. The input could be interrupted by external processes.",
          "IOException found.",
          JOptionPane.ERROR_MESSAGE);
      e.printStackTrace();
    }

    return null;
  }
예제 #8
0
  /** Validates the value of REMOTE_USER for authenticated user. */
  @Test(
      groups = {
        "ldapv3",
        "ldapv3_sec",
        "s1ds",
        "s1ds_sec",
        "ad",
        "ad_sec",
        "amsdk",
        "amsdk_sec",
        "jdbc",
        "jdbc_sec"
      })
  public void evaluateRemoteUser() throws Exception {
    entering("evaluetRemoteUser", null);

    webClient = new WebClient();
    try {
      page = consoleLogin(webClient, resourceProtected, "generalagenttests", "generalagenttests");
      page = (HtmlPage) webClient.getPage(url);
      iIdx = -1;
      iIdx = getHtmlPageStringIndex(page, "REMOTE_USER:generalagenttests");
      assert (iIdx != -1);
    } catch (Exception e) {
      log(Level.SEVERE, "evaluetRemoteUser", e.getMessage());
      e.printStackTrace();
      throw e;
    } finally {
      consoleLogout(webClient, logoutURL);
    }
    exiting("evaluateRemoteUser");
  }
예제 #9
0
  /**
   * Tests asynchronous use of XMLHttpRequest, using Mozilla style object creation.
   *
   * @throws Exception if the test fails
   */
  @Test
  public void testAsyncUse() throws Exception {
    final URL secondUrl = new URL(FIRST_URL, "/second/");

    final String html =
        "<html>\n"
            + "  <head>\n"
            + "    <title>XMLHttpRequest Test</title>\n"
            + "    <script>\n"
            + "      var request;\n"
            + "      function testAsync() {\n"
            + "        if (window.XMLHttpRequest)\n"
            + "          request = new XMLHttpRequest();\n"
            + "        else if (window.ActiveXObject)\n"
            + "          request = new ActiveXObject('Microsoft.XMLHTTP');\n"
            + "        request.onreadystatechange = onReadyStateChange;\n"
            + "        alert(request.readyState);\n"
            + "        request.open('GET', '/second/', true);\n"
            + "        request.send('');\n"
            + "      }\n"
            + "      function onReadyStateChange() {\n"
            + "        alert(request.readyState);\n"
            + "        if (request.readyState == 4)\n"
            + "          alert(request.responseText);\n"
            + "      }\n"
            + "    </script>\n"
            + "  </head>\n"
            + "  <body onload='testAsync()'>\n"
            + "  </body>\n"
            + "</html>";

    final String xml =
        "<xml2>\n"
            + "<content2>sdgxsdgx</content2>\n"
            + "<content2>sdgxsdgx2</content2>\n"
            + "</xml2>";

    final WebClient client = new WebClient();
    final List<String> collectedAlerts = new ArrayList<String>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(FIRST_URL, html);
    conn.setResponse(secondUrl, xml, "text/xml");
    client.setWebConnection(conn);

    client.getPage(FIRST_URL);

    final int executedJobs = client.getJavaScriptEngine().pumpEventLoop(1000);
    final String[] alerts = {
      String.valueOf(XMLHttpRequest.STATE_UNINITIALIZED),
      String.valueOf(XMLHttpRequest.STATE_LOADING),
      String.valueOf(XMLHttpRequest.STATE_LOADING),
      String.valueOf(XMLHttpRequest.STATE_LOADED),
      String.valueOf(XMLHttpRequest.STATE_INTERACTIVE),
      String.valueOf(XMLHttpRequest.STATE_COMPLETED),
      xml
    };
    assertEquals(Arrays.asList(alerts).toString(), collectedAlerts.toString());
    assertEquals(1, executedJobs);
  }
예제 #10
0
 public <P extends Page> P getPage(String url)
     throws FailingHttpStatusCodeException, MalformedURLException, IOException {
   String hostAddress =
       MessageFormat.format("http://localhost:{0,number,#####}", getServer().getPort());
   P page = webClient.<P>getPage(hostAddress + url);
   return page;
 }
예제 #11
0
 /**
  * @param ticker The ticker of the stock to look up.
  * @throws ConnectionException if it has problems connecting to Yahoo.
  * @return The value of the given stock, as %CompanyName: %Value.
  */
 public String getStockValue(String ticker) throws ConnectionException {
   try {
     final HtmlPage lookup =
         client.getPage(
             new StringBuilder("http://www.google.com/finance?q=").append(ticker).toString());
     final DomElement pricediv =
         lookup.getFirstByXPath("//div[@id = 'price-panel']/div/span/span");
     final DomElement compname =
         lookup.getFirstByXPath("//div[@class = 'appbar-snippet-primary']/span");
     return new StringBuilder(compname.getTextContent())
         .append(": ")
         .append(pricediv.getTextContent())
         .toString();
   } catch (IOException | FailingHttpStatusCodeException e) {
     System.out.println(
         new StringBuilder("Exception in retrieval.")
             .append("InternetConnection.getStockValue(")
             .append(ticker)
             .append(")")
             .toString());
     System.out.println(e.toString());
   }
   throw new ConnectionException(
       new StringBuilder("Unable to find data for ").append(ticker).toString());
 }
예제 #12
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();
  }
예제 #13
0
파일: Main.java 프로젝트: 9316/IdeaProjects
  public static void main(String[] args) {
    WebClient client = new WebClient(BrowserVersion.CHROME);
    HtmlPage page = null;

    try {
      page = client.getPage("http://www.facebook.com");

      System.out.println(page.getTitleText());

      HtmlForm form = (HtmlForm) page.getElementById("login_form");
      form.getInputByName("email").setValueAttribute("*****@*****.**");
      form.getInputByName("pass").setValueAttribute("saburtalo16");
      page = form.getInputByValue("Log In").click();
      System.out.println(page.getTitleText());

      HtmlTextArea statusText = (HtmlTextArea) page.getElementByName("xhpc_message_text");
      statusText.click();
      statusText.setText("I'm a robot");
      HtmlButton post =
          (HtmlButton)
              page.getFirstByXPath("//button[@id=\"u_jsonp_3_4\"]/div/div[4]/div/ul/li[2]/button");
      post.click();

    } catch (Exception e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
  }
 @Override
 protected SkypeGuestTempSession run(WebClient webClient) throws Exception {
   webClient.getPage(joinUrl);
   String csrf = webClient.getCookieManager().getCookie("csrf_token").getValue();
   String sessionId = webClient.getCookieManager().getCookie("launcher_session_id").getValue();
   return new SkypeGuestTempSession(csrf, sessionId);
 }
예제 #15
0
  /**
   * get the ajax url from the click button
   *
   * @param clickOfXpath:页面待点击按钮的xpath表达式
   * @param index
   * @return List<String>:链表的第一个信息是页面的title,以后的信息是所有的ajax的url
   */
  public static List<String> getAjaxUrl(String targetUrl, String clickOfXpath, int index)
      throws FailingHttpStatusCodeException, MalformedURLException, IOException {
    // TARGET_URL =
    // "http://app.flyme.cn/apps/public/detail?package_name=com.myzaker.zaker_phone_smartbar";
    List<String> urls = new LinkedList<String>();
    // 每次ajax请求时都会创建一个AjaxController对象,在该对象中可以查看ajax请求的地址
    MyNicelyResynchronizingAjaxController ajaxController =
        new MyNicelyResynchronizingAjaxController();

    List alertHandler = new LinkedList();
    // 模拟一个浏览器
    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
    // HtmlUnitDriver
    // 设置webClient的相关参数
    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setCssEnabled(false);
    webClient.setAjaxController(ajaxController);
    webClient.getOptions().setTimeout(35000);
    webClient.getOptions().setThrowExceptionOnScriptError(false);
    webClient.setAlertHandler(
        new CollectingAlertHandler(alertHandler)); // 将JavaScript中alert标签产生的数据保存在一个链表中

    // 模拟浏览器打开一个目标网址
    HtmlPage rootPage = webClient.getPage(targetUrl);
    urls.add(rootPage.getTitleText());
    urls.add(ajaxController.getVisitUrl());
    // System.out.println("url1:" + url);
    HtmlElement elementA = (HtmlElement) rootPage.getByXPath(clickOfXpath).get(index);
    Page page = elementA.click();
    urls.add(ajaxController.getVisitUrl());
    return urls;
  }
예제 #16
0
  /**
   * Test that a JS job using setTimeout is processed on GAE.
   *
   * @throws IOException if fails to get page.
   * @throws FailingHttpStatusCodeException if fails to get page.
   */
  @Test
  public void setTimeout() throws FailingHttpStatusCodeException, IOException {
    final String html =
        "<html>\n"
            + "  <body>\n"
            + "    <script>\n"
            + "      setTimeout(\"alert('hello')\", 0);"
            + "      setTimeout(\"alert('hello again')\", 200);"
            + "    </script>\n"
            + "  </body>\n"
            + "</html>";
    final WebClient client = new WebClient();
    final List<String> collectedAlerts = new ArrayList<String>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection conn = new MockWebConnection();
    conn.setDefaultResponse(html);
    client.setWebConnection(conn);
    client.getPage(FIRST_URL);

    int executedJobs = client.getJavaScriptEngine().pumpEventLoop(20);
    assertEquals(Arrays.asList("hello"), collectedAlerts);
    assertEquals(1, executedJobs);

    executedJobs = client.getJavaScriptEngine().pumpEventLoop(100);
    assertEquals(Arrays.asList("hello"), collectedAlerts);
    assertEquals(0, executedJobs);

    executedJobs = client.getJavaScriptEngine().pumpEventLoop(200);
    assertEquals(Arrays.asList("hello", "hello again"), collectedAlerts);
    assertEquals(1, executedJobs);
  }
예제 #17
0
 @Test
 public void testDecoratedFactories() throws Exception {
   HtmlPage page = webClient.getPage(webUrl);
   String pageXml = page.asXml();
   assertTrue(pageXml.contains("MyFacesContextFactory$MyFacesContext"));
   assertTrue(pageXml.contains("MyExceptionHandlerFactory$MyExceptionHandler"));
 }
예제 #18
0
  @Test
  public void testStable() throws Exception {

    String inputValue1 = "value=" + '"' + "1" + '"';
    String inputValue2 = "value=" + '"' + "2" + '"';
    String idText3 = "id=" + '"' + "text3" + '"';

    /*
     * Make sure the three dynamically added input components are in their proper place.
     */
    HtmlPage page = webClient.getPage(webUrl + "faces/stable.xhtml");
    assertTrue(page.asXml().indexOf("encodeBegin") < page.asXml().indexOf(inputValue1));
    assertTrue(page.asXml().indexOf(inputValue1) < page.asXml().indexOf(inputValue2));
    assertTrue(page.asXml().indexOf(inputValue2) < page.asXml().indexOf(idText3));
    assertTrue(page.asXml().indexOf("text3") < page.asXml().indexOf("encodeEnd"));

    /**
     * After clicking make sure the added component is still in its proper place. Also verify the
     * validation required error message appears as the third input component has required attribute
     * set to true.
     */
    HtmlSubmitInput button = (HtmlSubmitInput) page.getElementById("button");
    page = button.click();
    assertTrue(page.asXml().contains("text3: Validation Error: Value is required."));
    assertTrue(page.asXml().indexOf("encodeBegin") < page.asXml().indexOf(inputValue1));
    assertTrue(page.asXml().indexOf(inputValue1) < page.asXml().indexOf(inputValue2));
    assertTrue(page.asXml().indexOf(inputValue2) < page.asXml().indexOf(idText3));
    assertTrue(page.asXml().indexOf("text3") < page.asXml().indexOf("encodeEnd"));
  }
예제 #19
0
 public String getParsedPage() {
   List<String> alertHandler = new LinkedList<String>();
   ;
   WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24); // CHROME);
   webClient.setAjaxController(new MyNicelyResynchronizingAjaxController());
   webClient.getOptions().setJavaScriptEnabled(true);
   webClient.getOptions().setTimeout(3500);
   webClient.getOptions().setThrowExceptionOnScriptError(true);
   webClient.getOptions().setCssEnabled(true);
   webClient.getOptions().isRedirectEnabled();
   webClient.setAlertHandler(
       new CollectingAlertHandler(alertHandler)); // 将JavaScript中alert标签产生的数据保存在一个链表中
   // webClient.getOptions().setThrowExceptionOnScriptError(false);
   HtmlPage page = null;
   JavaScriptEngine engine = new JavaScriptEngine(webClient);
   webClient.setJavaScriptEngine(engine);
   try {
     page = webClient.getPage(data);
   } catch (FailingHttpStatusCodeException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (MalformedURLException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
   if (page != null) {
     return page.asXml();
   }
   return null;
 }
예제 #20
0
 /** This test verifies ajax behavior was programmatically attached to input text component. */
 @Test
 public void testProgrammaticAjaxBehavior() throws Exception {
   String expectedString =
       "<input id="
           + '"'
           + "form:input1"
           + '"'
           + " type="
           + '"'
           + "text"
           + '"'
           + " name="
           + '"'
           + "form:input1"
           + '"'
           + " value="
           + '"'
           + "hi"
           + '"'
           + " onfocus="
           + '"'
           + "mojarra.ab(this,event,'focus',0,0)"
           + '"';
   HtmlPage page = webClient.getPage(webUrl + "faces/issue2674.xhtml");
   assertTrue(page.asXml().contains(expectedString));
 }
예제 #21
0
파일: Crawl.java 프로젝트: OliveLv/MyUtils
 public static HtmlPage getPage(String url)
     throws FailingHttpStatusCodeException, MalformedURLException, IOException {
   WebClient wc = new WebClient(BrowserVersion.CHROME);
   wc.getOptions().setCssEnabled(false);
   HtmlPage page = wc.getPage(url);
   return page;
 }
예제 #22
0
 /** Validates a not enforced resources. */
 @Test(
     groups = {
       "ldapv3",
       "ldapv3_sec",
       "s1ds",
       "s1ds_sec",
       "ad",
       "ad_sec",
       "amsdk",
       "amsdk_sec",
       "jdbc",
       "jdbc_sec"
     })
 public void evaluateNotEnforced() throws Exception {
   entering("evaluateNotEnforced", null);
   webClient = new WebClient();
   try {
     URL urlLoc = new URL(resourceNotProtected);
     page = (HtmlPage) webClient.getPage(urlLoc);
     log(Level.FINEST, "evaluateNotEnforced", "Resource Page :\n" + page.asXml());
     iIdx = -1;
     iIdx = getHtmlPageStringIndex(page, "Notenforced Page");
     assert (iIdx != -1);
   } catch (Exception e) {
     log(Level.SEVERE, "evaluateNotEnforced", e.getMessage());
     e.printStackTrace();
     throw e;
   }
   exiting("evaluateNotEnforced");
 }
  public void discoverInputs(WebClient client, String url) {

    HashMap<String, List<HtmlElement>> urlToInput = new HashMap<String, List<HtmlElement>>();
    List<HtmlElement> inputs = new ArrayList<HtmlElement>();

    try {

      HtmlPage page = client.getPage(url);
      List<HtmlElement> elements = page.getTabbableElements();

      for (HtmlElement element : elements) {
        if (element instanceof HtmlInput || element instanceof HtmlTextArea) {
          inputs.add(element);
        }
      }

      urlToInput.put(url, inputs);

      System.out.println("----------------------------------------------------------------");
      System.out.println("Inputs Discovered:");
      for (Map.Entry<String, List<HtmlElement>> input : urlToInput.entrySet()) {
        System.out.println("URL: " + input.getKey());
        for (HtmlElement element : input.getValue()) {
          System.out.println(" -> Input: " + element.toString());
        }
      }

    } catch (Exception excep) {

      System.out.println("Exception found.");
      excep.printStackTrace();
    }
  }
예제 #24
0
  public boolean athenticated() {

    web = new WebClient();
    try {
      page1 = web.getPage(url);
    } catch (FailingHttpStatusCodeException | IOException e) {
      e.printStackTrace();
    }
    HtmlForm form = page1.getFormByName("form2");

    form.getInputByName("txtUserName").setAttribute("value", username);
    form.getInputByName("txtPassword").setAttribute("value", password);
    try {
      page2 = form.getInputByName("LoginButton").click();
    } catch (ElementNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    if (page2.getUrl().toString().equals("http://78.39.200.210/Default.aspx")) {
      return true;
    } else {
      web.closeAllWindows();
      return false;
    }
  }
예제 #25
0
  public static ArrayList<String> trans(String url)
      throws FailingHttpStatusCodeException, MalformedURLException, IOException {

    ArrayList<String> hrefList = new ArrayList<String>();
    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setJavaScriptEnabled(false);
    webClient.getOptions().setCssEnabled(false);
    try {
      HtmlPage page = null;
      try {
        page = (HtmlPage) webClient.getPage(url);
      } catch (ConnectException e) {
        System.out.println("Connect fails here:" + e.getMessage());
      }
      InputStream temp = new ByteArrayInputStream(page.asText().getBytes());
      InputStreamReader isr = new InputStreamReader(temp);
      BufferedReader br = new BufferedReader(isr);
      String str = null, rs = null;
      while ((str = br.readLine()) != null) {
        rs = str;
        // System.out.println(rs);
        if (rs != null) hrefList.add(rs);
      }
      System.out.println("从该网址查找的可能相关文本如下:");
      for (int i = 0; i < hrefList.size(); i++) {
        String string = hrefList.get(i);
        string = getTextFromHtml(string);
        if (string.length() >= 30) System.out.println("------" + i + ":" + string);
      }
    } catch (IOException e) {
    }
    return hrefList;
  }
예제 #26
0
 @Test
 public void testResponse() throws Exception {
   WebClient webClient = new WebClient();
   TextPage page = webClient.getPage(url);
   assertEquals("Pong", page.getContent());
   webClient.closeAllWindows();
 }
예제 #27
0
  @Test
  @Ignore
  public void test3() throws Exception {
    System.out.println("-------------------------------");

    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setCssEnabled(false);
    webClient.getOptions().setJavaScriptEnabled(false);
    HtmlPage page =
        webClient.getPage("http://www.zjnu.edu.cn/news/common/article_show.aspx?article_id=19285");

    System.out.println("---------------标题----------------");
    HtmlSpan span1 = (HtmlSpan) page.getElementById("mytitle");
    System.out.println(span1.asText());
    System.out.println("-------------------------------");

    System.out.println("---------------正文----------------");
    HtmlSpan span2 = (HtmlSpan) page.getElementById("mycontent");
    System.out.println(span2.asText());
    System.out.println("-------------------------------");

    System.out.println("---------------图片----------------");
    DomNodeList<HtmlElement> elements = span2.getElementsByTagName("img");
    for (HtmlElement element : elements) {
      System.out.println(element.getAttribute("src"));
    }
    // log.debug("{}", elements);
    System.out.println("-------------------------------");

    webClient.close();
    System.out.println("-------------------------------");
  }
예제 #28
0
  /**
   * Test that a JS job using setInterval is processed on GAE.
   *
   * @throws IOException if fails to get page.
   * @throws FailingHttpStatusCodeException if fails to get page.
   */
  @Test
  public void setInterval() throws FailingHttpStatusCodeException, IOException {
    final String html =
        "<html>\n"
            + "  <body>\n"
            + "    <script>\n"
            + "      setInterval(\"alert('hello')\", 100);"
            + "    </script>\n"
            + "  </body>\n"
            + "</html>";
    final WebClient client = new WebClient();
    final List<String> collectedAlerts = new ArrayList<String>();
    client.setAlertHandler(new CollectingAlertHandler(collectedAlerts));
    final MockWebConnection conn = new MockWebConnection();
    conn.setDefaultResponse(html);
    client.setWebConnection(conn);
    client.getPage(FIRST_URL);

    assertEquals(0, collectedAlerts.size());

    // pump but not long enough
    int executedJobs = client.getJavaScriptEngine().pumpEventLoop(50);
    assertEquals(0, collectedAlerts.size());

    // pump a bit more
    executedJobs = client.getJavaScriptEngine().pumpEventLoop(100);
    assertEquals(Arrays.asList("hello"), collectedAlerts);
    assertEquals(1, executedJobs);

    // pump even more
    executedJobs = client.getJavaScriptEngine().pumpEventLoop(250);
    assertEquals(Arrays.asList("hello", "hello", "hello"), collectedAlerts);
    assertEquals(2, executedJobs);
  }
예제 #29
0
 private HtmlPage getPage(String url) throws IOException {
   final WebClient webClient =
       new WebClient(
           getBrowserVersionFromName(
               searchEngine.getDefaultBrowser())); // BrowserVersion.FIREFOX_24);
   webClient.getOptions().setJavaScriptEnabled(false);
   return webClient.getPage(url);
 }
  @Test
  @SpecAssertions({@SpecAssertion(section = CONVERSATION_CONTEXT, id = "bc")})
  public void testLifecycleEventFired() throws Exception {

    WebClient webClient = new WebClient();

    // Begin new long-running conversation
    TextPage page = webClient.getPage(contextPath + "test?action=begin");
    assertTrue(page.getContent().contains("cid:" + ConversationScopedBean.CID));

    // Invalidate HTTP session - destroy non-attached long-running conversation
    page = webClient.getPage(contextPath + "test?action=invalidate");

    // Get the info
    page = webClient.getPage(contextPath + "test?action=info");
    assertTrue(page.getContent().contains("destroyed cid:" + ConversationScopedBean.CID));
  }