javascript - Doing an HTTP POST in Selenium IDE - Stack Overflow

I would like to do a single HTTPS POST to a server using Selenium IDE with an XML payload.The service

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 mand.) 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 mand in Selenium IDE. The final version of the code is below:

var method = method || "post"; 
var path = "/";
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.

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 mand.) 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 mand 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.

Share Improve this question edited May 23, 2017 at 12:07 CommunityBot 11 silver badge asked Aug 16, 2012 at 21:02 GreenGreen 5867 silver badges26 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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>=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 (mand) 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/en/docs/Web/API/XMLHttpRequest http://docs.seleniumhq/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();
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744662113a4586522.html

相关推荐

  • javascript - Doing an HTTP POST in Selenium IDE - Stack Overflow

    I would like to do a single HTTPS POST to a server using Selenium IDE with an XML payload.The service

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信