我试图用POST请求启动一个Selenium测试到我的应用程序。有什么办法可以使用Selenium开始POST请求吗?
而不是一个简单的open(/startpoint)
我想这样做open(/startpoint, stuff=foo,stuff2=bar)
有没有办法做到这一点?
我问这是因为原来的网页,其中职位,这个起点取决于经常离线(开发环境),外部供应商,因此往往会失败为时尚早(并且不是测试的主题)
我想发送数据为GET也可以。我只想使用POST方法。
我试图用POST请求启动一个Selenium测试到我的应用程序。有什么办法可以使用Selenium开始POST请求吗?
而不是一个简单的open(/startpoint)
我想这样做open(/startpoint, stuff=foo,stuff2=bar)
有没有办法做到这一点?
我问这是因为原来的网页,其中职位,这个起点取决于经常离线(开发环境),外部供应商,因此往往会失败为时尚早(并且不是测试的主题)
我想发送数据为GET也可以。我只想使用POST方法。
简短的回答:
不可以,但你也许能有点filthing的做到这一点。如果您打开一个测试页面(使用GET),然后评估该页面上的一些JavaScript,您应该能够复制POST请求。请参阅JavaScript post request like a form submit以了解如何使用JavaScript复制POST请求。
希望这会有所帮助。
硒IDE让你运行JavaScript使用storeEval命令。如果你有测试页面(HTML,而不是XML),并且你只需要执行POST请求,上面提到的解决方案工作正常。
如果您需要POST/PUT/DELETE或任何其他的要求,那么你就需要另一种方法:
的XMLHttpRequest!
下面列出的例子已经过测试 - 所有方法(POST/PUT/DELETE)都可以正常工作。
<!--variables-->
<tr>
<td>store</td>
<td>/your/target/script.php</td>
<td>targetUrl</td>
</tr>
<tr>
<td>store</td>
<td>user=user1&password</td>
<td>requestParams</td>
</tr>
<tr>
<td>store</td>
<td>POST</td>
<td>requestMethod</td>
</tr>
<!--scenario-->
<tr>
<td>storeEval</td>
<td>window.location.host</td>
<td>host</td>
</tr>
<tr>
<td>store</td>
<td>http://${host}</td>
<td>baseUrl</td>
</tr>
<tr>
<td>store</td>
<td>${baseUrl}${targetUrl}</td>
<td>absoluteUrl</td>
</tr>
<tr>
<td>store</td>
<td>${absoluteUrl}?${requestParams}</td>
<td>requestUrl</td>
</tr>
<tr>
<td>storeEval</td>
<td>var method=storedVars['requestMethod']; var url = storedVars['requestUrl']; loadXMLDoc(url, method); function loadXMLDoc(url, method) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { if(xmlhttp.status==200) { alert("Results = " + xmlhttp.responseText);} else { alert("Error!"+ xmlhttp.responseText); }}}; xmlhttp.open(method,url,true); xmlhttp.send(); }</td>
<td></td>
</tr>
澄清:
$ {requestParams} - 你想发布的参数(如参数1 =值1 &参数2 =值3 &参数1 =值3) ,因为你需要,你可以指定许多参数
$ {targetUrl这个} - 路径脚本(如果您有位于http://domain.com/application/update.php一页,然后targetUrl这个应该等于/application/update.php)
$ {requestMethod} - 方法类型(在这种特定情况下它应该是“POST”,但可以“PUT”或“删除”或任何其他)
硒目前不提供的API这,但有几种方法可以在您的测试中启动HTTP请求。这取决于你在写什么语言。
在Java为例,它可能是这样的:
// setup the request
String request = "startpoint?stuff1=foo&stuff2=bar";
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// get a response - maybe "success" or "true", XML or JSON etc.
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
StringBuffer response = new StringBuffer();
while ((line = bufferedReader.readLine()) != null) {
response.append(line);
response.append('\r');
}
bufferedReader.close();
// continue with test
if (response.toString().equals("expected response"){
// do selenium
}
一个非常实用的方式做,这是创建你的测试的虚拟起始页面,仅仅是用POST形式,具有单“开始测试”按钮和一堆<input type="hidden"
...元素与适当的发布数据。
例如,你可以创建一个SeleniumTestStart.html
页面,内容如下:
<body>
<form action="/index.php" method="post">
<input id="starttestbutton" type="submit" value="starttest"/>
<input type="hidden" name="stageid" value="stage-you-need-your-test-to-start-at"/>
</form>
</body>
在这个例子中,index.php的是你正常的Web应用程序的位置。然后
在您的测试开始时的硒代码将包括:
open /SeleniumTestStart.html
clickAndWait starttestbutton
这非常类似于自动测试使用的其他模拟和存根技术。你只是嘲笑Web应用程序的入口点。
很显然,在这种方法的一些局限性:
请考虑阅读我的文章左右在Qualities of an Ideal Test
如果您正在使用Python selenium
绑定,如今,有一个扩展selenium
- selenium-requests
:
扩展硒类的webdriver从Reques包括请求功能 ts库,同时做所有需要的cookie和 请求头处理。
例子:
from seleniumrequests import Firefox
webdriver = Firefox()
response = webdriver.request('POST', 'url here', data={"param1": "value1"})
print(response)
这看起来很有希望,但有一些问题,首先是如果我使用'webdriver.request',它会打开一个新选项卡而不是现有的选项卡,并在请求结束时关闭,其次它不会呈现预期的页面。例如:假设我发送GET请求'webdriver.request('GET','http://example.com/some/path',data = {'a':1,'b'= 2}',浏览器打开并只显示'http:// example.com /'而不是'http://example.com/some/path?a = 1&b = 2' – bawejakunal 2016-04-22 05:23:48
@bawejakunal但是如何发布请求? – Volatil3 2016-08-17 10:48:22
好吧,我同意@Mishkin Berteig - 敏捷教练的回答。使用表单是使用POST功能的快速方法。
无论如何,我看到一些关于JavaScript的提及,但没有代码。我已经为我自己的需要,其中包括jQuery易于POST和其他人。
基本上,使用driver.execute_script()
您可以发送任何JavaScript,包括Ajax查询。
#/usr/local/env/python
# -*- coding: utf8 -*-
# proxy is used to inspect data involved on the request without so much code.
# using a basic http written in python. u can found it there: http://voorloopnul.com/blog/a-python-proxy-in-less-than-100-lines-of-code/
import selenium
from selenium import webdriver
import requests
from selenium.webdriver.common.proxy import Proxy, ProxyType
jquery = open("jquery.min.js", "r").read()
#print jquery
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = "127.0.0.1:3128"
proxy.socks_proxy = "127.0.0.1:3128"
proxy.ssl_proxy = "127.0.0.1:3128"
capabilities = webdriver.DesiredCapabilities.PHANTOMJS
proxy.add_to_capabilities(capabilities)
driver = webdriver.PhantomJS(desired_capabilities=capabilities)
driver.get("http://httpbin.org")
driver.execute_script(jquery) # ensure we have jquery
ajax_query = '''
$.post("post", {
"a" : "%s",
"b" : "%s"
});
''' % (1,2)
ajax_query = ajax_query.replace(" ", "").replace("\n", "")
print ajax_query
result = driver.execute_script("return " + ajax_query)
#print result
#print driver.page_source
driver.close()
# this retuns that from the proxy, and is OK
'''
POST http://httpbin.org/post HTTP/1.1
Accept: */*
Referer: http://httpbin.org/
Origin: http://httpbin.org
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.0.0 Safari/538.1
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 7
Cookie: _ga=GAxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; _gat=1
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Accept-Language: es-ES,en,*
Host: httpbin.org
None
a=1&b=2 <<---- that is OK, is the data contained into the POST
None
'''
更多的测试比其他答案。 – Smar 2015-03-16 12:00:11