@Test
  public void testSetValueListLayout() throws Exception {
    HtmlPage page = environment.getPage("/autocompleteListLayoutTest.jsf");
    HtmlElement autocompleteList = page.getElementById("form:myAutocompleteList");
    Assert.assertNotNull(autocompleteList);

    List<?> countryList = autocompleteList.getByXPath("div/div/div/ul/li");
    Assert.assertEquals(30, countryList.size());

    HtmlInput input = (HtmlInput) page.getElementById("form:myAutocompleteInput");
    Assert.assertNotNull(input);
    input.type("be");

    for (int i = 0; i < 20; i++) {
      synchronized (page) {
        autocompleteList = page.getElementById("form:myAutocompleteList");
        Assert.assertNotNull(autocompleteList);

        countryList = autocompleteList.getByXPath("div/div/div/ul/li");

        if (countryList.size() == 5) {
          break;
        } else {
          page.wait(500);
        }
      }
    }

    Assert.assertEquals(5, countryList.size());
  }
Ejemplo n.º 2
0
  private void testInvalidSessionIdHelper(
      final String button,
      final String name,
      final String failMsg,
      final boolean fakeAppendedSessionId)
      throws IOException, JaxenException {
    HtmlPage editPage = clickEditLink(getWikiPage(name));
    final HtmlForm form = editPage.getFormByName(ID_EDIT_FORM);
    final HtmlInput sessionId = form.getInputByName("sessionId");
    sessionId.setValueAttribute(FAKE_SESSION_ID);
    if (fakeAppendedSessionId) {
      form.setActionAttribute(name + ";jsessionid=" + FAKE_SESSION_ID);
    }
    final HtmlTextArea content = form.getTextAreaByName("content");
    final String expectedContent = "http://www.example.com";
    content.setText(expectedContent);

    editPage = (HtmlPage) form.getInputByValue(button).click();

    try {
      getAnchorByHrefContains(editPage, expectedContent);
      fail(failMsg);
    } catch (NoSuchElementException e) {
    }
  }
Ejemplo n.º 3
0
 /**
  * @param form
  * @return
  */
 public static HtmlSubmitInput getSubmitInput(final HtmlForm form) {
   final List<HtmlElement> inputs = form.getHtmlElementsByTagName(HtmlInput.TAG_NAME);
   for (final HtmlElement element : inputs) {
     final HtmlInput input = (HtmlInput) element;
     if ("submit".equalsIgnoreCase(input.getTypeAttribute())) {
       return (HtmlSubmitInput) input;
     }
   }
   return null;
 }
Ejemplo n.º 4
0
  public void testHideNoSelectionOptionIsAValueExpression() throws Exception {
    HtmlSelect select = (HtmlSelect) this.page.getElementById("f:selectItemNoSelectedValue");
    // validate initial page
    assertEquals(4, select.getOptionSize());

    HtmlInput button = (HtmlInput) this.page.getElementById("f:command");
    select.getOptionByValue("Pippin").click();
    this.page = button.click();

    select = (HtmlSelect) this.page.getElementById("f:selectItemNoSelectedValue");
    assertEquals(5, select.getOptionSize());
  }
Ejemplo n.º 5
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;
  }
Ejemplo n.º 6
0
  public void testPreview() throws Exception {
    String name = uniqueWikiPageName("PreviewPageTest");
    HtmlPage editPage = clickEditLink(getWikiPage(name));
    HtmlForm form = editPage.getFormByName(ID_EDIT_FORM);
    HtmlInput minorEdit = form.getInputByName("minorEdit");
    assertFalse(minorEdit.isChecked());
    minorEdit.setChecked(true);
    HtmlInput description = form.getInputByName("description");
    String expectedDescription = "My test change";
    description.setValueAttribute(expectedDescription);
    HtmlTextArea content = form.getTextAreaByName("content");
    String expectedContent = "http://www.example.com";
    content.setText(expectedContent);

    // Now if we preview we should get the previewed text rendered, and in
    // the edit area.  The other options should be preserved too.
    editPage = (HtmlPage) form.getInputByValue("Preview").click();
    form = editPage.getFormByName(ID_EDIT_FORM);
    minorEdit = form.getInputByName("minorEdit");
    description = form.getInputByName("description");
    content = form.getTextAreaByName("content");
    assertEquals(expectedDescription, description.getValueAttribute());
    assertTrue(minorEdit.isChecked());
    assertEquals(expectedContent + NEWLINE_TEXTAREA, content.getText());
    // This checks for the rendering...
    assertNotNull(editPage.getAnchorByHref(expectedContent));
  }
Ejemplo n.º 7
0
  public Page submitAuthentication(String username, String password) throws IOException {
    if (usernameField != null && passwordField != null && submitField != null) {
      usernameField.setValueAttribute(username);
      passwordField.setValueAttribute(password);
      return submitField.click();
    }

    return null;
  }
  public void testAjaxTagEventAttribute() throws Exception {
    getPage("/faces/ajax/ajaxTagEventAttribute.xhtml");
    System.out.println("Start ajax tag event attribute test");

    // Check initial values
    checkTrue("out1", "0");
    checkTrue("out2", "1");
    checkTrue("out3", "");
    checkTrue("checkedvalue1", "false");
    checkTrue("checkedvalue2", "false");

    // Press Count
    HtmlSubmitInput button = (HtmlSubmitInput) lastpage.getHtmlElementById("button");
    lastpage = (HtmlPage) button.click();

    checkTrue("out1", "0");
    checkTrue("out2", "0");

    HtmlInput input = (HtmlInput) lastpage.getHtmlElementById("in1");
    lastpage = (HtmlPage) input.setValueAttribute("test");

    checkTrue("out3", "test");

    // Check ajax checkbox
    HtmlCheckBoxInput checked = ((HtmlCheckBoxInput) lastpage.getHtmlElementById("checkbox1"));
    lastpage = (HtmlPage) checked.click();

    checkTrue("checkedvalue1", "true");

    // Check ajax checkbox
    checked = ((HtmlCheckBoxInput) lastpage.getHtmlElementById("checkbox2"));
    lastpage = (HtmlPage) checked.click();

    checkTrue("checkedvalue2", "true");

    // Check ajax checkbox
    checked = ((HtmlCheckBoxInput) lastpage.getHtmlElementById("checkbox3"));
    lastpage = (HtmlPage) checked.click();

    checkTrue("checkedvalue3", "true");

    // Check that all ajax requests didn't result in a reload
    checkTrue("out4", "2");
  }
Ejemplo n.º 9
0
  private HtmlPage clickSearchButtonMainPage(final WebClient webClient, SearchEngine se) {

    String url = se.getBaseUrl();
    String queryTextboxName = se.getQueryTextBoxName();
    String searchButtonId = se.getSubmitButtonId();
    String searchButtonName = se.getSubmitButtonName();
    Object tempSubmitFromName, tempSubmitFromId, tempSubmitFromTagName;

    try {
      HtmlPage page1 = webClient.getPage(url);
      HtmlInput input1 = page1.getElementByName(queryTextboxName);
      input1.setValueAttribute(query);
      tempSubmitFromName = page1.getElementByName(searchButtonName);
      return clickButtonReturnPage(tempSubmitFromName);
      //                baseUrl = page1.getUrl().toString();
    } catch (Exception ex) {
      String aaa = "deneme";
    }
    try {
      HtmlPage page1 = webClient.getPage(url);
      HtmlInput input1 = page1.getElementByName(queryTextboxName);
      input1.setValueAttribute(query);
      tempSubmitFromId = page1.getElementById(searchButtonId);
      return clickButtonReturnPage(tempSubmitFromId);
      //               baseUrl = page1.getUrl().toString();
    } catch (Exception ex2) {
      String aaa = "deneme";
    }
    try {
      HtmlPage page1 = webClient.getPage(url);
      HtmlInput input1 = page1.getElementByName(queryTextboxName);
      input1.setValueAttribute(query);
      tempSubmitFromTagName = page1.getElementsByTagName("button").get(0);
      return clickButtonReturnPage(tempSubmitFromTagName);
      //                baseUrl = page1.getUrl().toString();
    } catch (Exception ex2) {
      String aaa = "deneme";
    }
    return null;
  }
Ejemplo n.º 10
0
 @Test
 public void testAbcInRepeatedInputTexts() throws Exception {
   HtmlPage page = webClient.getPage(webUrl + "faces/repeatAbc.xhtml");
   HtmlInput input = page.getHtmlElementById("form:j_idt5:5:inputIn");
   assertEquals("abc", input.getValueAttribute());
 }
Ejemplo n.º 11
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;
  }