在Selenium IDE中执行HTTP POST
Doing an HTTP POST in Selenium IDE
我想对使用具有XML有效负载的Selenium IDE的服务器进行单个HTTPS POST。 我正在与之交互的服务不支持HTTPS GET,否则我会这样做。(执行HTTPS GET非常简单,因为整个URL都进入Selenium打开命令。 我知道还有其他方法可以做HTTP POST,比如使用curl,但我的Web测试目前是在Selenium IDE中完成的,我不想担心两三个不同的工具来进行测试。
我查看了Javascript的POST提交,并修改了我在那里找到的代码。我把它作为Selenium IDE中的storeEval命令放在里面。代码的最终版本如下:
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();
当我运行示例代码的稍微修改版本时,我在Selenium IDE日志中收到一条消息,指出"document.body is undefined"。 我尝试删除对document.body的所有引用,希望一个简单的表单对象本身就足够了。 其实不然。 我得到了"form.submit 不是一个函数"。 也许有一些Javascript的魔力可以工作。
我在Google或StackOverflow上没有找到任何有人试图在Selenium IDE中进行HTTP POST的参考资料。 如果可以进行 POST,我愿意为 Selenium 编写一个扩展。
这是一个古老的线程,但这可能有助于其他人(如上周的我!)寻找从Selenium IDE发送POST的方法。
我已经设法得到了一个可行的解决方案 - 我只发送参数和一个空的主体,因为我不需要发送数据有效载荷,但也许这会在这里帮助某人。
在 IDE 中,我将基本 url 保存为 Selenium 存储的 Vars 项"testPageBaseUrl":
<td>store</td>
<td>${TargetEnvironment}/testpage.html</td>
<td>testPageBaseUrl</td>
我的参数字符串也作为存储的 Vars 项目"参数":
<td>store</td>
<td>com=update&some-param=false&some-id=1</td>
<td>params</td>
然后我调用我的 Selenium IDE 扩展函数,将基本 url 作为参数传递:
<td>postByXMLHttpRequest</td>
<td>testPageBaseUrl</td>
<td></td>
我无法在 IDE 中函数调用(命令)的 Target 字段中使用多个参数来执行此操作,因此该函数显式访问"参数"存储的 Vars 变量。
这是我添加到user_extentions.js文件中的函数,然后在Selenium IDE设置中引用该函数:
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);
};
一些背景信息:https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequesthttp://docs.seleniumhq.org/docs/08_user_extensions.jsp
据我所知,没有办法通过Selenium IDE进行HTTP POST,我认为我试图强迫它做一些从未打算过的事情。 我将使用 JUnit 和 WebDriver 来代替我的 POST。
您需要使用 seleinum.browserbot.getCurrentWindow()
来获取窗口并使用其文档成员。
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();
};
- 如何绑定“;这个“;在$http.post中->在AngularJS中解析/拒绝?(现在this=Window对象
- 为什么我的$http.post返回一个400错误
- AngularJS$http.post调用返回502(坏网关)错误
- AngularJS$http.post没有'不适用于Chrome,仅适用于IE
- 如何将多个onError函数分配给promise(由angular's$http.post返回)
- 如何将AJAX POST转换为HTTP POST,以便在单击时发布
- $http.post undefined 不是函数
- 在Selenium IDE中执行HTTP POST
- 从 Angular 数据工厂中的 HTTP POST 请求接收未定义的结果
- $http.post() angularjs 响应函数未按正确的顺序执行
- 索引未在 $http.post 查询中获取正确的值
- 在javascript中获取 http.post,然后发送响应
- Angular无法读取http post响应中的cookie
- Angular 2:实现HTTP POST请求
- Angular 2:HTTP Post Request,带有URL参数和正文类型参数
- angular.js$http.post将数据发送到Rails4
- Angular$http.post未到达服务器
- $timeout包装$http.post返回undefined而不是promise
- HTTP Post请求json数据更改
- 在Http Post上添加授权标头