Doing an HTTP POST in Selenium IDE Doing an HTTP POST in Selenium IDE selenium selenium

Doing an HTTP POST in Selenium IDE


This is an old thread but this might help someone else (like me last week!) looking for a way to send a POST from Selenium IDE.

I've managed to get a working solution - I'm only sending parameters and an empty body, since I had no requirement to send a data payload, but maybe this will help someone out here.

In the IDE, I save my base url as a Selenium storedVars item "testPageBaseUrl":

<td>store</td><td>${TargetEnvironment}/testpage.html</td><td>testPageBaseUrl</td>

and my parameter string also as storedVars item "params":

<td>store</td><td>com=update&some-param=false&some-id=1</td><td>params</td>

Then I call my Selenium IDE extension function, passing the base url as a parameter:

<td>postByXMLHttpRequest</td><td>testPageBaseUrl</td><td></td>

I wasn't able to get this working with more than one param in the Target field for the function call (command) in the IDE, so the function explicitly accesses the "params" storedVars variable.

Here's the function I added to a user_extentions.js file which I then reference in the Selenium IDE settings:

Selenium.prototype.doPostByXMLHttpRequest = function (baseUrl) {var httpReq = new XMLHttpRequest();var params =  storedVars ["params"]; httpReq.open("POST",  storedVars[ baseUrl ]);httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");httpReq.setRequestHeader("Content-length", params.length);httpReq.setRequestHeader("Connection", "close");httpReq.send(params);};

Some background info:https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequesthttp://docs.seleniumhq.org/docs/08_user_extensions.jsp


So as far as I can tell, there isn't a way to do HTTP POST through Selenium IDE and I think I was attempting to force it to do something for which it was never intended. I'll be using JUnit and WebDriver to do my POSTs instead.


You need to use seleinum.browserbot.getCurrentWindow() to get the window and use its document member.

Selenium.prototype.doCustomPost = function (baseUrl) {    var win = selenium.browserbot.getCurrentWindow();    var form = win.document.createElement("form");    form.setAttribute("method", "post"  );    form.setAttribute("action", baseUrl );    //StoredVars is a selenium variable that can be added to by:    //<td> store </td> <td> Value </td> <td> Key </td>     for(var key in storedVars ) {        if(storedVars.hasOwnProperty( key )) {            var hiddenField = win.document.createElement("input");            hiddenField.setAttribute("type", "hidden");            hiddenField.setAttribute("name", key);            hiddenField.setAttribute("value", storedVars[ key ] );            form.appendChild(hiddenField);         }    }    win.document.body.appendChild(form);    form.submit();};