Re: [Httpunit-develop] Session Management in HTTPUnit
Brought to you by:
russgold
From: Russell G. <rus...@ht...> - 2009-09-17 13:05:19
|
HttpUnit handles cookies on its own. There is no need for you to play with the cookie headers. Cookies will be returned to the server that sent them. On Sep 16, 2009, at 5:46 AM, Sandeep SN wrote: > Hi Russel, > > this is the code in better format. The section about Cookies in this > code is not changing anything. With or without this code I get invalid > session. > > 1. I would like to know how to handle this session issue. Starting > from the logon method I would like to execute all the other tests in > the same session. > > > import com.meterware.httpunit.*; > import com.meterware.servletunit.*; > import java.util.*; > import java.io.*; > > import junit.framework.*; > import org.w3c.dom.Document; > import org.w3c.dom.Node; > import org.w3c.dom.NodeList; > import org.w3c.dom.Element; > //import tutorial.persistence.*; > > public class BenchMark extends TestCase { > WebConversation wc = new WebConversation(); > WebRequest req=null; > WebResponse resp=null; > String sResponse=null; > public String CookieVal; > public static void main( String[] args) { > junit.textui.TestRunner.run( suite() ); > } > > public static TestSuite suite() { > return new TestSuite( BenchMark.class ); > } > > public BenchMark( String s ) { > super( s ); > } > > public void testEntry() throws Exception { > > > req = new GetMethodWebRequest( "http://server:8080/arl/start.html > " ); > resp = wc.getResponse( req ); > sResponse= resp.getText(); > writeToFile(resp.getText()); > } > > public void testLogon() throws Exception { > > > try{ > req = new GetMethodWebRequest( > "http://server:8080/arl/logon/?httpMethod=GET&password=&cmsName=cmsnameval&authType=normAuth&userName=Administrator > " > ); > resp = wc.getResponse( req ); > > String sHeadernames[]=resp.getHeaderFieldNames(); > > > String[] CookieVal=resp.getHeaderFields("SET-COOKIE"); > String sessionid=CookieVal[0].substring(0, CookieVal[0].indexOf > (";")); > System.out.println("this is sessionid"+sessionid); > wc.setHeaderField("Cookie",sessionid); > writeToFile(resp.getText()); > } > catch (java.net.MalformedURLException e) { > writeToFile(resp.getText()); > } > } > public void testuDtaProfile() throws Exception { > > > > try{ > req = new GetMethodWebRequest( "http://localhost:8080/arl/udta/profile > "); > resp = wc.getResponse( req ); > writeToFile(resp.getText()); > } > catch (java.net.MalformedURLException e) { > writeToFile(resp.getText()); > } > } > > public void testData() throws Exception { > > > > try{ > req = new GetMethodWebRequest( "http://localhost:8080/arl/bdta/data > "); > resp = wc.getResponse( req ); > writeToFile(resp.getText()); > } > catch (java.net.MalformedURLException e) { > writeToFile(resp.getText()); > } > } > > > > > > > > > /*Dom start > Document dom = null; > try { > dom = resp.getDOM(); > } catch (org.xml.sax.SAXException e) { > System.out.println("XML Err: " + e); > } > String[] domChildren; > domChildren[]=dom.getChildNodes(1); > System.out.println(domChildren[0]); > Dom end*/ > > // Code for writing to file > public void writeToFile(String sResponse) throws Exception { > > > try { > Writer output = null; > File file = new > File("c:\\JavaTests\\"+this.getClass().getName()+this.getName() > +"OutPut.txt"); > output = new BufferedWriter(new FileWriter(file)); > output.write(sResponse); > output.close(); > System.out.println("Your file has been written as output"); > > } > catch (IOException ex){ > ex.printStackTrace(); > } > > } > > } > > > > > Regards > Sandeep > > On Wed, Sep 16, 2009 at 2:25 PM, Sandeep SN <sns...@gm...> > wrote: >> Hi Russel, >> >> When I try to run tests using different urls, I get a invalid session >> error. I observe that there is something to do with ?JSESSIONID >> parameter. >> >> >> Below is the httpunit program. I logon in the first test ->testlogon >> and then continue working on other tests in the same session. >> >> Please help me in achieving this. >> >> //package tutorial; >> >> import com.meterware.httpunit.*; >> import com.meterware.servletunit.*; >> import java.util.*; >> import java.io.*; >> >> import junit.framework.*; >> import org.w3c.dom.Document; >> import org.w3c.dom.Node; >> import org.w3c.dom.NodeList; >> import org.w3c.dom.Element; >> >> >> public class BenchMark extends TestCase { >> public String CookieVal; >> public static void main( String[] args) { >> junit.textui.TestRunner.run( suite() ); >> } >> >> public static TestSuite suite() { >> return new TestSuite( BenchMark.class ); >> } >> >> public BenchMark( String s ) { >> super( s ); >> } >> >> public void testEntry() throws Exception { >> >> >> WebConversation wc = new WebConversation(); >> WebRequest req=null; >> req = new GetMethodWebRequest( "http://Server:8080/ui_enter/First.html >> " ); >> WebResponse resp = wc.getResponse( req ); >> String sResponse= resp.getText(); >> writeToFile(resp.getText()); >> } >> >> public void testLogon() throws Exception { >> >> >> WebConversation wc = new WebConversation(); >> WebRequest req=null; >> WebResponse resp = null; >> try{ >> req = new GetMethodWebRequest( >> "http://Server:8080/ui_enter/logon/? >> httpMethod >> = >> GET >> &password >> =&cmsName=<Server>&authType=normalAuth&userName=Administrator" >> ); >> resp = wc.getResponse( req ); >> >> String sHeadernames[] >> =resp.getHeaderFieldNames(); >> >> >> String[] CookieVal=resp.getHeaderFields("SET- >> COOKIE"); >> String sessionid=CookieVal[0].substring(0, >> CookieVal[0].indexOf(";")); >> System.out.println("this is >> sessionid"+sessionid); >> wc.setHeaderField("Cookie",sessionid); >> writeToFile(resp.getText()); >> } >> catch (java.net.MalformedURLException e) { >> //System.out.println("Bad url: " + e); >> writeToFile(resp.getText()); >> System.out.println("Came to exception"); >> } >> } >> public void testuDtaprfle() throws Exception { >> >> >> WebConversation wc = new WebConversation(); >> WebRequest req=null; >> WebResponse resp = null; >> try{ >> req = new GetMethodWebRequest( >> "http://localhost:8080/ui_enter/u_dta/prfle"); >> resp = wc.getResponse( req ); >> writeToFile(resp.getText()); >> } >> catch (java.net.MalformedURLException e) { >> //System.out.println("Bad url: " + e); >> writeToFile(resp.getText()); >> } >> } >> >> >> >> >> //Code for writing to file >> /*try { >> Writer output = null; >> File file = new >> File("K:\\JavaTests\\"+this.getClass().getName()+this.getName() >> +"OutPut.txt"); >> output = new BufferedWriter(new FileWriter(file)); >> output.write(sResponse); >> output.close(); >> System.out.println("Your file has been written as output"); >> >> >> } >> catch (IOException ex){ >> ex.printStackTrace(); >> }*/ >> >> /*Dom start >> Document dom = null; >> try { >> dom = resp.getDOM(); >> } catch (org.xml.sax.SAXException e) { >> System.out.println("XML Err: " + e); >> } >> String[] domChildren; >> domChildren[]=dom.getChildNodes(1); >> System.out.println(domChildren[0]); >> Dom end*/ >> >> public void writeToFile(String sResponse) throws Exception { >> >> >> try { >> Writer output = null; >> File file = new >> File("K:\\JavaTests\\"+this.getClass().getName()+this.getName() >> +"OutPut.txt"); >> output = new BufferedWriter(new FileWriter(file)); >> output.write(sResponse); >> output.close(); >> System.out.println("Your file has been written as output"); >> >> } >> catch (IOException ex){ >> ex.printStackTrace(); >> } >> >> } >> >> } >> >> >> >> >> >> >> On Sun, Sep 13, 2009 at 10:04 PM, Russell Gold >> <rus...@ht...> wrote: >>> Have you looked at the unit tests? They should demonstrate all of >>> the >>> functionality. Briefly, as a client, HttpUnit saves the session >>> cookie and >>> returns it as needed. Nothing else is necessary. Logging off is >>> done just as >>> in the browser. >>> >>> >>> >>> On Sep 13, 2009, at 11:58 AM, Sandeep SN wrote: >>> >>>> Hi russgold, >>>> >>>> Greetings. >>>> >>>> I would appreciate if I could get a code snippet which shows how >>>> httpunit manages the Sessions or any relevant information about the >>>> same. >>>> >>>> The scenario is >>>> >>>> 1. I login to an application and perfomr some actions. All the >>>> actions >>>> translate the http requests. >>>> >>>> 2. I plan to write a httpunit class with various test methods. I >>>> would >>>> use the setup method the login and Destroy method to logout of the >>>> app. >>>> >>>> >>>> I have not been much into code level scripting so please do bear >>>> with me. >>>> >>>> Regards >>>> Sandeep >>> >>> >> |