@Test public void login() throws Exception { final HtmlPage loginPage = webClient.getPage(BASEURI); final HtmlForm form = loginPage.getForms().get(0); final HtmlTextInput userTextField = form.getInputByName("jsecLogin"); final HtmlPasswordInput passwordTextField = form.getInputByName("jsecPassword"); final HtmlSubmitInput button = form.getInputByName("login"); // Change the value of the text field userTextField.setValueAttribute("admin"); passwordTextField.setValueAttribute("admin"); HtmlPage homePage = null; try { homePage = button.click(); } catch (Exception e) { logger.error("error submitting", e); } Assert.assertEquals(homePage.getTitleText(), "Tynamo!"); webClient.closeAllWindows(); }
public static HtmlPage login(HtmlPage page, String username, String password) { int attempts = 0; boolean loggedIn = false; while (!loggedIn) { try { if (attempts > 3) System.exit(1); attempts++; System.out.println("Logging in... (Attempt " + attempts + ")"); page = wc.getPage("http://torn.com/login"); final HtmlForm form = page.getFormByName("login"); HtmlSubmitInput button = form.getInputByName("btnLogin"); HtmlTextInput textField = form.getInputByName("player"); // Change the value of the text field textField.setValueAttribute(username); final HtmlPasswordInput passwordField = form.getInputByName("password"); // Change the value of the text field passwordField.setValueAttribute(password); // Now submit the form by clicking the button and get back the second page. page = button.click(); page = wc.getPage("http://torn.com/index.php"); loggedIn = true; } catch (Exception e) { System.out.println(e.toString()); loggedIn = false; } } return page; }