4

I would like to do a single HTTPS POST to a server using Selenium IDE with an XML payload. The service I'm interacting with doesn't support HTTPS GETs or I would do it that way. (Doing an HTTPS GET is really easy as the whole URL goes into a Selenium open command.) I know there are other ways to do HTTP POSTs such as with curl, but my web testing is currently done in Selenium IDE and I don't want to have to worry about two or three different tools to do my testing.

I looked into POST submission by Javascript and modified the code I found there. I put it in as a storeEval command in Selenium IDE. The final version of the code is below:

var method = method || "post"; 
var path = "https://service.url.srv/";
var post = "<xml>payload</xml>";    
var form = document.createElement("form");    
form.setAttribute("method", method);    
form.setAttribute("action", path); 
var hiddenField = document.createElement("input"); 
hiddenField.setAttribute("type", "hidden"); 
hiddenField.setAttribute("name", ""); 
hiddenField.setAttribute("value", post); 
form.appendChild(hiddenField); form.submit();

When I ran a slightly modified version of the example code, I got a message in the Selenium IDE log saying that "document.body is undefined". I tried removing all references to document.body, hoping that a simple form object by itself would be enough. It wasn't. I got "form.submit is not a function". Perhaps there is some Javascript mojo that will work.

I haven't found any references on Google or StackOverflow where someone is trying to do a HTTP POST in Selenium IDE. I'm willing to write an extension to Selenium if doing a POST is even possible.

Community
  • 1
  • 1
Green
  • 556
  • 7
  • 23

3 Answers3

4

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&amp;some-param=false&amp;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/XMLHttpRequest http://docs.seleniumhq.org/docs/08_user_extensions.jsp

Scott Spence
  • 89
  • 12
0

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.

Green
  • 556
  • 7
  • 23
0

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();
};
0fnt
  • 6,986
  • 9
  • 42
  • 60