package HttpUnitDemo; import com.meterware.httpunit.*; import junit.framework.*; import java.net.*; import java.io.*; import java.util.*; public class HttpUnitDemoTest extends TestCase { String url = "http://www.dice.com"; String openSearchText = "java"; String daysBack = "7"; String jobsPerPage="50"; public HttpUnitDemoTest(String s) { super(s); } public static void main(String args[]) { junit.textui.TestRunner.run( suite() ); } public static Test suite() { TestSuite suite = new TestSuite(); TestCase test = new HttpUnitDemoTest("testDice"); suite.addTest(test); return suite; } public void testDice() { try { // JavaScript is always an Enigma, lets not worry about it //HttpUnitOptions.setExceptionsThrownOnScriptError( false ); URL serverUrl = new URL(url); WebConversation conversation = new WebConversation(); WebRequest request = new GetMethodWebRequest(serverUrl,""); WebResponse response = conversation.getResponse(request); assertNotNull("response of url: " + url, response); // notice the navigation path from the image to its associated link WebImage image = response.getImageWithSource("images/topnav_findajob_off.gif"); assertNotNull("find a job impage",image); WebLink findJobLink = image.getLink(); assertNotNull("find a job link",findJobLink); System.out.println("found job seeker link"); response = findJobLink.click(); assertNotNull("job seeker page",response); System.out.println("got job seeker page"); // Using IE View Source we see that Dice uses frames response = response.getSubframeContents("ENGINE_WIN"); assertNotNull("got the engine frame",response); System.out.println("got engine frame"); WebForm findJobsForm = response.getForms()[0]; assertNotNull("find jobs form",findJobsForm); System.out.println("got the find jobs search form" ); /** * lets put in some free text into the search * We know that the form parameter name is FREE_TEXT by looking on the IE html source */ findJobsForm.setParameter("FREE_TEXT",openSearchText); findJobsForm.setParameter("DAYSBACK",daysBack); findJobsForm.setParameter("NUM_PER_PAGE",jobsPerPage); response = findJobsForm.submit(); assertNotNull("got search result frame",response); System.out.println("got search result " ); // the results are not listed within a from since each is a hyperlink using GET method WebLink resultLinks[] = response.getLinks(); assertNotNull("search links", resultLinks); System.out.println("got search links back"); // count only the job opening links int openingCounter = 0; for(int i=0; i < resultLinks.length; i++) { String url = resultLinks[i].getURLString(); if(url.indexOf("/jobsearch/servlet/JobSearch?op") != -1) { openingCounter++; System.out.println("found job opening: " + resultLinks[i].asText()); } } // click on some job opening link: if(openingCounter >0) { response = resultLinks[0].click(); assertNotNull("job opening page",response); //System.out.println(response.getText()); } } catch (Exception ex) { ex.printStackTrace(); super.fail(ex.getMessage()); } } }