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(""), 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
#if you need to print the url along with params using this

#Create a dict for params to be sent
url = ''
params={'param1':'value1' 'param2':'value2'}

#Call the url & params, params=params)

#if you want to send the params in the request body not in query string use the data option,data=params)

#GET request using s.get option

#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

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

WebClient webClient = new WebClient();
webClient.setAjaxController(new NicelyResynchronizingAjaxController());

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

Happy Testing.


raja's shared items

My "Testing" Bundle