@Test public void testConfigurationAuthentication() throws Exception { prepareSecurity(); FreeStyleProject p = j.createFreeStyleProject(); WebClient wc = j.createWebClient(); wc.login("test1"); // Reauthentication is not required if No need for re-authentication is checked p.addProperty( new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("admin", true))); j.submit(wc.getPage(p, "configure").getFormByName("config")); // Reauthentication is required if No need for re-authentication is checked p.removeProperty(AuthorizeProjectProperty.class); p.addProperty( new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("admin", false))); try { j.submit(wc.getPage(p, "configure").getFormByName("config")); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(400, e.getStatusCode()); } // No authentication is required if oneself. { HtmlPage page = wc.getPage(p, "configure"); HtmlTextInput userid = page.<HtmlTextInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'userid') and @type='text']"); userid.setValueAttribute("test1"); j.submit(page.getFormByName("config")); assertEquals( "test1", ((SpecificUsersAuthorizationStrategy) p.getProperty(AuthorizeProjectProperty.class).getStrategy()) .getUserid()); } // Reauthentication is required to change userid even if No need for re-authentication is // checked p.addProperty( new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("admin", true))); { HtmlPage page = wc.getPage(p, "configure"); HtmlTextInput userid = page.<HtmlTextInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'userid') and @type='text']"); userid.setValueAttribute("test2"); try { j.submit(page.getFormByName("config")); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(400, e.getStatusCode()); } } }
public static HtmlPage trainStrength(HtmlPage page, int amount) throws IOException { System.out.println("Training " + amount + " strength..."); for (DomNode n : page.getElementById("divStrength").getDescendants()) { // System.out.println(n.toString()); HtmlTextInput textInput; HtmlSubmitInput submitInput; if (n.toString() .contains("HtmlTextInput[<input type=\"text\" value=\"1\" id=\"t\" name=\"t\">]")) { textInput = (HtmlTextInput) n; textInput.setValueAttribute(Integer.toString(amount)); System.out.println(textInput.toString()); } else if (n.toString() .contains("HtmlSubmitInput[<input type=\"submit\" value=\"Train\">]")) { submitInput = (HtmlSubmitInput) n; page = submitInput.click(); System.out.println(submitInput.toString()); } } /* System.out.println(this.user.getUsername()+"-"+"Training "+amount+" strength..."); HtmlTextInput amountInput = (HtmlTextInput) page.getByXPath("/html/body/div[4]/table/tbody/tr/td[2]/center/div[2]/div/div[2]/div[3]/div[1]/div[1]/div[2]/form/table/tbody/tr/td[1]/input[1]").get(0); amountInput.setValueAttribute(Integer.toString(amount)); HtmlElement e = (HtmlElement) page.getByXPath("/html/body/div[4]/table/tbody/tr/td[2]/center/div[2]/div/div[2]/div[3]/div[1]/div[1]/div[2]/form/table/tbody/tr/td[3]/input").get(0); e.click(); */ System.out.println(page.asText()); return page; }
public void testMethodExpressionDefaults() throws Exception { HtmlPage page = getPage("/faces/composite/defaultAttributeMethodExpression.xhtml"); HtmlSubmitInput submit = (HtmlSubmitInput) getInputContainingGivenId(page, "def:form1:command"); page = submit.click(); assertTrue(page.asText().contains("Action invoked")); page = getPage("/faces/composite/defaultAttributeMethodExpression.xhtml"); submit = (HtmlSubmitInput) getInputContainingGivenId(page, "def:form2:command2"); page = submit.click(); assertTrue(page.asText().contains("ActionListener invoked")); page = getPage("/faces/composite/defaultAttributeMethodExpression.xhtml"); submit = (HtmlSubmitInput) getInputContainingGivenId(page, "def:form3:command3"); page = submit.click(); assertTrue(page.asText().contains("Custom action invoked")); page = getPage("/faces/composite/defaultAttributeMethodExpression.xhtml"); submit = (HtmlSubmitInput) getInputContainingGivenId(page, "def:form4:command"); HtmlTextInput text = (HtmlTextInput) getInputContainingGivenId(page, "def:form4:input"); text.setValueAttribute("foo"); page = submit.click(); assertTrue(page.asText().contains("validator invoked")); page = getPage("/faces/composite/defaultAttributeMethodExpression.xhtml"); submit = (HtmlSubmitInput) getInputContainingGivenId(page, "def:form5:command"); page = submit.click(); assertTrue(page.asText().contains("ValueChange invoked")); }
@Override protected String getCallbackUrl(final HtmlPage authorizationPage) throws Exception { HtmlForm form = authorizationPage.getFormByName("loginform"); final HtmlTextInput login = form.getInputByName("log"); login.setValueAttribute("testscribeup"); final HtmlPasswordInput passwd = form.getInputByName("pwd"); passwd.setValueAttribute("testpwdscribeup"); HtmlElement button = (HtmlElement) authorizationPage.createElement("button"); button.setAttribute("type", "submit"); form.appendChild(button); // HtmlButton button = form.getButtonByName("wp-submit"); final HtmlPage confirmPage = button.click(); form = confirmPage.getFormByName("loginform"); button = (HtmlElement) confirmPage.createElement("button"); button.setAttribute("type", "submit"); form.appendChild(button); // button = form.getButtonByName("wp-submit"); final HtmlPage callbackPage = button.click(); final String callbackUrl = callbackPage.getUrl().toString(); logger.debug("callbackUrl : {}", callbackUrl); return callbackUrl; }
@Test public void testConfigurationEffective() throws Exception { HtmlPage page = webClient.getPage(webUrl + "faces/spec566.xhtml"); HtmlTextInput input1 = (HtmlTextInput) page.getHtmlElementById("input1"); input1.setTextContent("3"); HtmlTextInput input2 = (HtmlTextInput) page.getHtmlElementById("input2"); input2.setTextContent("4"); HtmlSubmitInput button = (HtmlSubmitInput) page.getHtmlElementById("reload"); page = button.click(); input1 = (HtmlTextInput) page.getHtmlElementById("input1"); input1.setValueAttribute(""); input2 = (HtmlTextInput) page.getHtmlElementById("input2"); input2.setValueAttribute("abcd"); button = (HtmlSubmitInput) page.getHtmlElementById("reload"); page = button.click(); HtmlElement message1 = page.getHtmlElementById("input1Message"); assertTrue(!message1.getTextContent().contains("input1:")); HtmlElement message2 = page.getHtmlElementById("input2Message"); assertTrue(message2.getTextContent().contains("input2:")); input1 = (HtmlTextInput) page.getElementById("input1"); assertEquals(0, input1.getTextContent().length()); input2 = (HtmlTextInput) page.getElementById("input2"); assertEquals("abcd", input2.getValueAttribute()); }
/** * Test method. * * @throws IOException exception */ public void testLoginValid() throws IOException { final WebClient webClient = new WebClient(); // XXX: issues with jquery webClient.setJavaScriptEnabled(false); final HtmlPage page = webClient.getPage(HTTP_PROCC_LOGIN_USER); final HtmlForm form = page.getFormByName(LOGIN_USER_FORM_NAME); final HtmlTextInput textEmail = form.getInputByName(EMAIL_INPUT_NAME); final HtmlPasswordInput textPassword = form.getInputByName(PASSWD_INPUT_NAME); final HtmlSubmitInput btnLogin = form.getInputByName(LOGIN_SUBMIT_NAME); textEmail.setValueAttribute("*****@*****.**"); textPassword.setValueAttribute("malczyk123"); final HtmlPage nextPage = btnLogin.click(); assertTrue(nextPage.getTitleText().equals("Taskflow-Main")); webClient.closeAllWindows(); }
/** * Test method. * * @throws IOException exception */ public void testLoginInvalid() throws IOException { final WebClient webClient = new WebClient(); // XXX: issues with jquery webClient.setJavaScriptEnabled(false); final HtmlPage page = webClient.getPage(HTTP_PROCC_LOGIN_USER); final HtmlForm form = page.getFormByName(LOGIN_USER_FORM_NAME); final HtmlTextInput textEmail = form.getInputByName(EMAIL_INPUT_NAME); final HtmlPasswordInput textPassword = form.getInputByName(PASSWD_INPUT_NAME); final HtmlSubmitInput btnLogin = form.getInputByName(LOGIN_SUBMIT_NAME); textEmail.setValueAttribute("invalidLogin"); textPassword.setValueAttribute("invalidPasswd"); final HtmlPage nextPage = btnLogin.click(); assertTrue(nextPage.asText().contains("Błędny login bądź hasło")); webClient.closeAllWindows(); }
@Override protected String getCallbackUrl(final WebClient webClient, final HtmlPage authorizationPage) throws Exception { webClient.waitForBackgroundJavaScript(5000); HtmlForm form = authorizationPage.getForms().get(0); final HtmlTextInput login = form.getInputByName("login_email"); login.setValueAttribute("*****@*****.**"); final HtmlPasswordInput passwd = form.getInputByName("login_password"); passwd.setValueAttribute("testpwdscribeup"); HtmlButton submit = form.getButtonByName(""); HtmlPage confirmPage = submit.click(); confirmPage = (HtmlPage) confirmPage.refresh(); webClient.waitForBackgroundJavaScript(5000); form = confirmPage.getForms().get(0); HtmlButton submit2 = form.getButtonByName("allow_access"); final HtmlPage callbackPage = submit2.click(); final String callbackUrl = callbackPage.getUrl().toString(); logger.debug("callbackUrl : {}", callbackUrl); return callbackUrl; }
public static HtmlPage solveCaptcha(HtmlPage page) throws Exception { System.out.println("Solving captcha..."); String url = page.getUrl().toString(); page = wc.getPage(url + "?bypass=1"); HtmlImage captchaImage = (HtmlImage) page.getElementById("recaptcha_image").getFirstChild(); captchaImage.saveAs(new File("captcha-image.jpg")); CaptchaSolver captchaSolver = new CaptchaSolver(new SocketClient("trialaccount", "Cappie1!"), "captcha-image.jpg"); captchaSolver.run(); Captcha captcha = captchaSolver.getCaptcha(); HtmlTextInput textField = (HtmlTextInput) page.getElementById("recaptcha_response_field"); textField.setValueAttribute(captcha.text); HtmlElement element = (HtmlElement) page.getElementByName("submit"); page = element.click(); System.out.println("Captcha entered."); return wc.getPage(url); }
@JsfTest(JsfVersion.JSF_2_2_5) @Test public void testDropFlashCookie() throws Exception { HtmlPage page = webClient.getPage(webUrl + "faces/flashDropCookie.xhtml"); webClient.getOptions().setRedirectEnabled(true); HtmlTextInput textInput = (HtmlTextInput) page.getHtmlElementById("input"); textInput.setValueAttribute("test"); HtmlSubmitInput button = (HtmlSubmitInput) page.getHtmlElementById("submit"); int currentSize = webClient.getCookieManager().getCookies().size(); page = button.click(); HtmlElement element = page.getHtmlElementById("link"); page = element.click(); int newSize = webClient.getCookieManager().getCookies().size(); assertTrue(newSize < currentSize); }
// Submit the form with updates to every field public void testFormInput02_submit02() throws Exception { checkFormInput00(); HtmlPage page = getFormInput02(); checkFormInput02(page, formInput02_pristine); HtmlForm form = getFormById(page, formInput02_name); assertNotNull("form exists", form); for (int i = 0; i < formInput02_names.length; i++) { HtmlTextInput input = (HtmlTextInput) form.getInputByName( formInput02_name + NamingContainer.SEPARATOR_CHAR + formInput02_names[i]); assertNotNull("field '" + formInput02_names[i] + "' exists", input); input.setValueAttribute(formInput02_updated[i]); } HtmlSubmitInput submit = (HtmlSubmitInput) form.getInputByName(formInput02_name + NamingContainer.SEPARATOR_CHAR + "submit"); page = (HtmlPage) submit.click(); checkFormInput02(page, formInput02_updated); checkFormInput01(); }
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; }
@Test public void testConfigureApitoken() throws Exception { prepareSecurity(); FreeStyleProject p = j.createFreeStyleProject(); p.addProperty( new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("test2", false))); WebClient wc = j.createWebClient(); wc.login("test1"); String apitokenForTest2 = User.get("test2").getProperty(ApiTokenProperty.class).getApiToken(); assertNotNull(apitokenForTest2); assertNotEquals("", apitokenForTest2); // authentication fails without apitoken { HtmlPage page = wc.getPage(p, "configure"); HtmlCheckBoxInput useApitoken = page.<HtmlCheckBoxInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']"); useApitoken.setChecked(true); try { j.submit(page.getFormByName("config")); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(400, e.getStatusCode()); } } // authentication succeeds with the good apitoken { HtmlPage page = wc.getPage(p, "configure"); HtmlCheckBoxInput useApitoken = page.<HtmlCheckBoxInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']"); useApitoken.setChecked(true); HtmlTextInput apitoken = page.<HtmlTextInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'apitoken') and @type='text']"); apitoken.setValueAttribute(apitokenForTest2); j.submit(page.getFormByName("config")); assertEquals( "test2", ((SpecificUsersAuthorizationStrategy) p.getProperty(AuthorizeProjectProperty.class).getStrategy()) .getUserid()); } // authentication fails with a bad apitoken { HtmlPage page = wc.getPage(p, "configure"); HtmlCheckBoxInput useApitoken = page.<HtmlCheckBoxInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']"); useApitoken.setChecked(true); HtmlTextInput apitoken = page.<HtmlTextInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'apitoken') and @type='text']"); apitoken.setValueAttribute(apitokenForTest2 + "xxx"); try { j.submit(page.getFormByName("config")); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(400, e.getStatusCode()); } } // authentication fails if the apitoken is used for password { HtmlPage page = wc.getPage(p, "configure"); HtmlCheckBoxInput useApitoken = page.<HtmlCheckBoxInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']"); useApitoken.setChecked(false); HtmlPasswordInput password = page.<HtmlPasswordInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'password') and @type='password']"); password.setValueAttribute(apitokenForTest2); HtmlTextInput apitoken = page.<HtmlTextInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'apitoken') and @type='text']"); apitoken.setValueAttribute(apitokenForTest2); try { j.submit(page.getFormByName("config")); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(400, e.getStatusCode()); } } }
@JsfTest( value = JSF_2_2_0_M02, excludes = {WEBLOGIC_12_1_4}) @Test public void testFlash() throws Exception { // Get the first page HtmlPage page = webClient.getPage(webUrl + "faces/index.xhtml"); String pageText = page.asXml(); // (?s) is an "embedded flag expression" for the "DOTALL" operator. // It says, "let . match any character including line terminators." // Because page.asXml() returns a big string with lots of \r\n chars // in it, we need (?s). // the page contains a table tag with a frame attribute whose value is hsides. // the page contains the following span, with the following id, with no contents assertTrue(pageText.matches("(?s)(?m).*<span.*id=\"fooValueId\">\\s*</span>.*")); // Click the reload button HtmlSubmitInput button = (HtmlSubmitInput) page.getHtmlElementById("reload"); page = (HtmlPage) button.click(); pageText = page.asXml(); // verify that fooValue is there, indicating that it's been stored in the flash assertTrue(pageText.matches("(?s)(?m).*<span.*id=\"fooValueId\">\\s*fooValue\\s*</span>.*")); // Get the first page, again page = webClient.getPage(webUrl + "faces/index.xhtml"); // the page contains the following span, with the following id, with no contents // meaning the flash has no value for foo // assertTrue(pageText.matches("(?s)(?m).*<span.*id=\"fooValueId\">\\s*</span>.*")); // fill in "addMessage" in the textBox HtmlTextInput text = (HtmlTextInput) page.getHtmlElementById("inputText"); text.setValueAttribute("addMessage"); // go to the next page button = (HtmlSubmitInput) page.getHtmlElementById("next"); page = (HtmlPage) button.click(); pageText = page.asXml(); // See that it has fooValue assertTrue( pageText.matches("(?s)(?m).*<span.*id=\"flash2FooValueId\">\\s*fooValue\\s*</span>.*")); // See that it has barValue assertTrue( pageText.matches("(?s)(?m).*<span.*id=\"flash2BarValueId\">\\s*barValue\\s*</span>.*")); // See that it has the message assertTrue(-1 != pageText.indexOf("test that this persists across the redirect")); // click the reload button button = (HtmlSubmitInput) page.getHtmlElementById("reload"); page = (HtmlPage) button.click(); pageText = page.asXml(); // See that it doesn't have the message assertTrue(-1 == pageText.indexOf("test that this persists across the redirect")); // Click the back button button = (HtmlSubmitInput) page.getHtmlElementById("back"); page = (HtmlPage) button.click(); // Click the next button button = (HtmlSubmitInput) page.getHtmlElementById("next"); page = (HtmlPage) button.click(); pageText = page.asXml(); // See that the page does not have the message assertTrue(-1 == pageText.indexOf("test that this persists across the redirect")); // Click the next button button = (HtmlSubmitInput) page.getHtmlElementById("next"); page = (HtmlPage) button.click(); pageText = page.asXml(); // See that it has banzai assertTrue( pageText.matches("(?s)(?m).*<span.*id=\"flash3NowValueId\">\\s*banzai\\s*</span>.*")); // Click the next button button = (HtmlSubmitInput) page.getHtmlElementById("next"); page = (HtmlPage) button.click(); pageText = page.asXml(); // See that it still has banzai assertTrue( pageText.matches("(?s)(?m).*<span.*id=\"flash4BuckarooValueId\">\\s*banzai\\s*</span>.*")); // click the link HtmlAnchor link = (HtmlAnchor) page.getElementById("link"); page = link.click(); // on flash5 link = (HtmlAnchor) page.getElementById("link"); page = link.click(); assertTrue(page.asText().contains("Value is 1.")); // click the link on the next page link = (HtmlAnchor) page.getElementById("link"); page = link.click(); assertTrue(page.asText().contains("Value is 1.")); // click the link on the same page link = (HtmlAnchor) page.getElementById("link"); page = link.click(); assertTrue(page.asText().contains("Value is 1.")); // click the link on the same page link = (HtmlAnchor) page.getElementById("link"); page = link.click(); assertTrue(page.asText().contains("Value is 1.")); link = (HtmlAnchor) page.getElementById("link2"); page = link.click(); assertTrue(page.asText().contains("Value is 1.")); link = (HtmlAnchor) page.getElementById("link"); page = link.click(); // it went away because there was no keep assertTrue(page.asText().contains("Value is .")); page = webClient.getPage(webUrl + "faces/flash9.xhtml"); text = (HtmlTextInput) page.getHtmlElementById("valueA"); text.setValueAttribute("a value"); text = (HtmlTextInput) page.getHtmlElementById("valueB"); text.setValueAttribute("b value"); text = (HtmlTextInput) page.getHtmlElementById("valueC"); text.setValueAttribute("c value"); button = (HtmlSubmitInput) page.getHtmlElementById("keep"); page = (HtmlPage) button.click(); pageText = page.asText(); assertTrue(pageText.contains("valueA: a value")); assertTrue(pageText.contains("valueB: b value")); assertTrue(pageText.contains("valueC: c value")); link = (HtmlAnchor) page.getElementById("flash9"); page = link.click(); text = (HtmlTextInput) page.getHtmlElementById("valueA"); assertEquals(text.getValueAttribute(), "a value"); text = (HtmlTextInput) page.getHtmlElementById("valueB"); assertEquals(text.getValueAttribute(), "b value"); text = (HtmlTextInput) page.getHtmlElementById("valueC"); assertEquals(text.getValueAttribute(), "c value"); page = webClient.getPage(webUrl + "faces/flash9.xhtml"); text = (HtmlTextInput) page.getHtmlElementById("valueA"); assertEquals(text.getValueAttribute(), ""); text = (HtmlTextInput) page.getHtmlElementById("valueB"); assertEquals(text.getValueAttribute(), ""); text = (HtmlTextInput) page.getHtmlElementById("valueC"); assertEquals(text.getValueAttribute(), ""); text = (HtmlTextInput) page.getHtmlElementById("valueA"); text.setValueAttribute("A value"); text = (HtmlTextInput) page.getHtmlElementById("valueB"); text.setValueAttribute("B value"); text = (HtmlTextInput) page.getHtmlElementById("valueC"); text.setValueAttribute("C value"); button = (HtmlSubmitInput) page.getHtmlElementById("nokeep"); page = (HtmlPage) button.click(); pageText = page.asText(); assertTrue(pageText.contains("valueA: A value")); assertTrue(pageText.contains("valueB: B value")); assertTrue(pageText.contains("valueC: C value")); button = (HtmlSubmitInput) page.getHtmlElementById("reload"); page = (HtmlPage) button.click(); pageText = page.asText(); assertTrue(!pageText.contains("valueA: A value")); assertTrue(!pageText.contains("valueB: B value")); assertTrue(!pageText.contains("valueC: C value")); // content from Sebastian Hennebrueder page = webClient.getPage(webUrl + "faces/flash12.xhtml"); button = (HtmlSubmitInput) page.getHtmlElementById("start"); page = (HtmlPage) button.click(); pageText = page.asText(); assertTrue(pageText.contains("4711")); page = (HtmlPage) page.refresh(); pageText = page.asText(); assertTrue(!pageText.contains("4711")); // Test for JAVASERVERFACES-2087 page = webClient.getPage(webUrl + "faces/flash13.xhtml"); button = (HtmlSubmitInput) page.getHtmlElementById("flashbtn"); page = (HtmlPage) button.click(); pageText = page.asText(); assertTrue(pageText.contains("read strobist")); page = webClient.getPage(webUrl + "faces/flash13.xhtml"); pageText = page.asText(); assertTrue(!pageText.contains("read strobist")); page = webClient.getPage(webUrl + "faces/flash13.xhtml"); pageText = page.asText(); assertTrue(!pageText.contains("read strobist")); page = webClient.getPage(webUrl + "faces/flash14.xhtml"); pageText = page.asText(); assertTrue( pageText.matches( "(?s).*\\[received javax.faces.event.PreClearFlashEvent source:\\{\\}\\].*")); }