public void testImageInsideCollection() throws Exception { execute("CRUD.new"); execute("Collection.new", "viewObject=xava_view_section0_ingredients"); execute("ImageEditor.changeImage", "newImageProperty=image"); assertNoErrors(); assertAction("LoadImage.loadImage"); String imageUrl = System.getProperty("user.dir") + "/test-images/cake.gif"; setFileValue("newImage", imageUrl); execute("LoadImage.loadImage"); assertNoErrors(); HtmlPage page = (HtmlPage) getWebClient().getCurrentWindow().getEnclosedPage(); URL url = page.getWebResponse().getRequestSettings().getUrl(); String urlPrefix = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort(); HtmlImage image = (HtmlImage) page.getElementsByName(decorateId("image")).get(0); String imageURL = null; if (image.getSrcAttribute().startsWith("/")) { imageURL = urlPrefix + image.getSrcAttribute(); } else { String urlBase = Strings.noLastToken(url.getPath(), "/"); imageURL = urlPrefix + urlBase + image.getSrcAttribute(); } WebResponse response = getWebClient().getPage(imageURL).getWebResponse(); assertTrue("Image not obtained", response.getContentAsString().length() > 0); assertEquals("Result is not an image", "image", response.getContentType()); }
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; }
public static void main(String[] args) { try { System.out.println("Starting application..."); java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); wc = new WebClient(BrowserVersion.FIREFOX_10); // wc = new WebClient(BrowserVersion.FIREFOX_10, "50.117.67.126", 3131); final DefaultCredentialsProvider scp = new DefaultCredentialsProvider(); scp.addCredentials("754d539bc6ade1ad", "454585f105747a76"); // wc.setCredentialsProvider(scp); wc.setJavaScriptEnabled(false); wc.setThrowExceptionOnScriptError(false); wc.setThrowExceptionOnFailingStatusCode(false); // Get the first page HtmlPage page = wc.getPage("http://torn.com/index.php"); if (page.asText().contains("You are no longer logged in.")) page = login(page, "tempaccount", "temp123"); else System.out.println("Already logged in."); System.out.println("Index.php loaded."); int loop = 0; int errors = 0; while (true) { System.out.println("Beginning loop " + loop + "..."); loop++; try { if (inHospital(page) || inJail(page)) { System.out.println("Sleeping 5 minutes..."); Thread.sleep(1000 * 5 * 60); } else { System.out.println("Loading page..."); wc.setJavaScriptEnabled(true); page = wc.getPage("http://torn.com/gym.php"); // page = wc.getPage("http://torn.com/crimes.php"); while (onCaptcha(page)) { wc.setJavaScriptEnabled(true); System.out.println("Captcha encountered."); page = solveCaptcha(page); wc.setJavaScriptEnabled(false); } // System.out.println(page.getWebResponse().getContentAsString()); trainStrength(page, 20); // doCrime(page, 5); System.out.println("Sleeping 30 minutes..."); Thread.sleep(1000 * 15); } page = loadIndex(); } catch (Exception e) { System.out.println(e.toString()); errors++; page = loadIndex(); if (errors > 3) System.exit(1); } } } catch (Exception e) { wc.closeAllWindows(); e.printStackTrace(); } }