tags:

views:

544

answers:

10

Howdy,

My cell phone provider offers a limited number of free text messages on their website. I frequently use the service although I hate constantly having a tab open in my browser.

Does anyone know/point me in the right direction of how I could create a jar file/command line utility so I can fill out the appropriate forms on the site. I've always wanted to code up a project like this in Java, just in case anyone asks why I'm not using something else.

Kind Regards, Lar

A: 

Try with Webdriver from Google or Selenium.

Luke
+2  A: 

Sounds like you need a framework designed for doing functional testing. These act as browsers and can navigate web sites for testing and automation. You don't need the testing functionality, but it would still serve your needs.

Try HtmlUnit, or LiFT, which is a higher-level abstraction built on HtmlUnit.

skaffman
A: 

It depends on how they are sending the form information.

If they are using a simple GET request, all you need to do is fill in the appropriate url parameters.

Otherwise you will need to post the form information to the target page.

jjnguy
It's almost certainly not just a single form that needs to be filled out - there will be at least one login form in from that gives you an auth cookie you need to proceed; otherwise everyone could use it and a cell phone provider would sooner give away its entire staff's firstborn that offer unlimited free text messages to everyone.
Michael Borgwardt
Exactly, you got all you need in Java itself. Compose the URL as a string, create a java.net.URL from the String, and open a connection. That simple. Check out: http://java.sun.com/j2se/1.4.2/docs/api/java/net/URL.html
zvikico
True, but my answer is regarding how to 'Fill out an online form using Java' which is what his question was in general. I understand that it is probably more difficult than just a single form.
jjnguy
A: 

You could use Watij, which provides a Java/COM interface onto Internet Explorer. Then write a small amount of Java code to navigate the form, insert values and submit.

Alternatively, if it's simple, then check out HttpClient, which is a simple Java HTTP client API.

Whatever you do, watch out that you don't contravene your terms of service (easy during testing - perhaps you should work against a mock interface initially?)

Brian Agnew
A: 

WebTest is yet another webapp testing framework that may be easier to use than the alternatives cited by others.

Michael Borgwardt
A: 

Check out the Apache Commons Net Package. There you can send a POSt request to a page. This is quite low level but may do what you want (if not you might check out the functional testing suites but it is probably not as easy to dig into).

Daff
A: 

As jjnguy says, you'll need to dissect the form to find out all the parameters. With them you can form your own request using Apache's HTTP Client and fire it off.

Chris J
A: 

Use Watij with the Eclipse IDE. When your done, compile as an .exe or run with a batch file.

Here is some sample code I wrote for filling in fields for a Google search, which can be adjusted for the web form you want to control :

package goog;
import junit.framework.TestCase;
import watij.runtime.ie.IE;
import static watij.finders.SymbolFactory.*;
public class GTestCases extends TestCase {

 private static watij.runtime.ie.IE activeIE_m;
 public static IE attachToIE(String url) throws Exception {   
  if (activeIE_m==null)
     {
       activeIE_m = new IE();
    activeIE_m.start(url);
     } else {
       activeIE_m.goTo(url);
     }
  activeIE_m.bringToFront();
     return (activeIE_m);    
 }

 public static String getActiveUrl () throws Exception {  
  String currUrl = activeIE_m.url().toString();  
  return currUrl;
 }

 public void testGoogleLogin() throws Exception {
  IE ie = attachToIE("http://google.com");
     if ( ie.containsText("/Sign in/") ) {
      ie.div(id,"guser").link(0).click();
      if ( ie.containsText("Sign in with your") ||
        ie.containsText("Sign in to iGoogle with your")) {
       ie.textField(name,"Email").set("[email protected]");
       ie.textField(name,"Passwd").set("test");
       if ( ie.checkbox(name,"PersistentCookie").checked() ){
        ie.checkbox(name,"PersistentCookie").click();
       }
       ie.button(name,"signIn").click();       
      }
     }
     System.out.println("Login finished.");
    }

 public void testGoogleSearch() throws Exception {
  //IE ie = attachToIE( getActiveUrl() );
  IE ie = attachToIE( "http://www.google.com/advanced_search?hl=en" );
     ie.div(id,"opt-handle").click();
     ie.textField(name,"as_q").set("Watij");
     ie.selectList(name,"lr").select("English");
     ie.button(value,"Advanced Search").click();
     System.out.println("Search finished.");
 }

 public void testGoogleResult() throws Exception {
  IE ie = attachToIE( getActiveUrl() );
  ie.link(href,"http://groups.google.com/group/watij").click();
  System.out.println("Followed link.");
 }

}
djangofan
A: 

djangofan

your solution rocks, I started working with Watij this week and in two days got Eclipse up and working and a full end to end test suite completed. It would have taken me

at least that long anyway with QTP. now all I have to do is make it data driven and thread it for load testing.

Thanks Tron621

Tron621
A: 

Can anyone please write a bit of Java code to fill the form on the below link and submit it? I tried to edit the above script and many other java code available on internet in Eclipse but failed. Any help is highly appreciated. About captcha, is it possible to flash captcha on the downloadable software (UI)? And then user will manually enter the captcha code.

http://www.shane-english.com/submit.php

lala.west AT hotmail DOT com

Lala