/** * 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; }
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) { } }
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)); }
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; }
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; }
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"); }