Saturday, August 10, 2013

Functional HtmlUnit testing in Python using Requests for Humans,

Using testing for Html by HtmlUnit/http-requests using server side requests is a good way to improve the speed of testing. In Ajax era, testing the web throws more challenges in automation testing, better way is to test the XHR requests separately rather than trying to test all combinations in browser based UI automation.

First let us see the GET/POST request by HtmlUnit,

//Create a WebRequest for POST, use HttpMethod.GET for GET requests.
WebRequest request = new WebRequest(new URL("http://yoururl.com"), HttpMethod.POST);
request.setRequestParameters(new ArrayList());

//Add the params using ArrayList
request.getRequestParameters().add(new NameValuePair("param1","value1"));

//Send the request to the server using getPage.
HtmlPage page = webClient.getPage(request);

//Get the response as a String.
String html= page.getWebResponse().getContentAsString();
//Cookies can accessed using the following code,
CookieManager CM = webClient.getCookieManager();
Set ck = CM.getCookies();
for(Cookie value : ck)   
{
     System.out.println(value+" "+value.getValue());
}
So, how to do this in Python using http-requests (Requests for humans)?

#First to create a session using requests.Session
s=requests.Session()
#if you need to print the url along with params using this
s=requests.Session(config={'verbose':sys.stderr})

#Create a dict for params to be sent
url = 'https://yoururlhere.com'
params={'param1':'value1' 'param2':'value2'}

#Call the url & params
reply=s.post(url, params=params)

#if you want to send the params in the request body not in query string use the data option
reply=s.post(url,data=params)

#GET request using s.get option
reply=s.get(url,data=params)

#get the redirected url by accessing .url
print reply.url

#text or content will give the response
print reply.text
print reply.content

#Accessing cookie is by s.cookies
print s.cookies

#convert cookie into dict by dict_from_cookiejar
cookdict=requests.utils.dict_from_cookiejar(s.cookies)

For HtmlUnit you may need to call  the following depending upon your need, options are self explanatory.


WebClient webClient = new WebClient();
webClient.setHTMLParserListener(HTMLParserListener.LOG_REPORTER);
webClient.setRedirectEnabled(true);
webClient.setJavaScriptEnabled(true);                                            
webClient.setThrowExceptionOnScriptError(false);            
webClient.setThrowExceptionOnFailingStatusCode(false); 
webClient.setUseInsecureSSL(true);
webClient.setCssEnabled(true);
webClient.setAppletEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());


Thanks for http-requests and HtmlUnit for these amazing frameworks which makes testing easier and faster. 

Happy Testing.

ShareThis

raja's shared items

There was an error in this gadget

My "Testing" Bundle