selenium中发送http请求来测试
来源:互联网 发布:mac有没有升降调软件 编辑:程序博客网 时间:2024/05/04 10:04
所使用的自动化测试框架selenium+maven+testng。最近在做自动化测试时,需要初始化数据,由于开发那边没有具体的说明文档,不知道应该动到数据库里面的哪些表。所以想起是否可以调用接口来帮我初始化数据。
产品通过get和post方式来发送请求。需要先登录一次获取Cookie,后面就直接写入Cookie调用了。代码(Java)如下:
public class Testhttp {public static CookieStore cookiestore = null;//get方式public static HttpResponse doGet(String url, String param) {HttpResponse response = null;HttpGet httpget = null;String reurl = null;try {if(param != null) {reurl = String.format("%s?%s", url, param);httpget = new HttpGet(reurl);}//得到Cookieif(null == cookiestore) {DefaultHttpClient httpclient = new DefaultHttpClient();response = httpclient.execute(httpget);cookiestore = httpclient.getCookieStore();}//获取Cookieif(null == cookiestore) {DefaultHttpClient httpclient = new DefaultHttpClient();response = httpclient.execute(httpget);cookiestore = httpclient.getCookieStore();}if(response.getStatusLine().getStatusCode() == 200) {String result = EntityUtils.toString(response.getEntity());System.out.println(result);}System.out.println("Http request response is: " + response.getStatusLine());} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return response;}//post方式public static HttpResponse doPost(String url, String param) {HttpResponse response = null;HttpPost httppost = null;String reurl = null;try {if(param != null) {reurl = String.format("%s?%s", url, param);httppost = new HttpPost(reurl);}//得到Cookieif(cookiestore == null) {DefaultHttpClient httpclient = new DefaultHttpClient();response = httpclient.execute(httppost);cookiestore = httpclient.getCookieStore();}//获取Cookieif(cookiestore != null) {DefaultHttpClient httpclient = new DefaultHttpClient();httpclient.setCookieStore(cookiestore);response = httpclient.execute(httppost);}if(response.getStatusLine().getStatusCode() == 200) {String result = EntityUtils.toString(response.getEntity());//System.out.println(result);}System.out.println("Http request response is: " + response.getStatusLine());} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return response;}}
这样就可以不用管开发在添加、删除操作中到底用了到哪些库哪些表,直接调用接口帮我完成了。
0 0
- selenium中发送http请求来测试
- 利用curl来发送http请求
- java中发送http请求
- 用telnet发送HTTP请求进行测试
- android中Post方式发送HTTP请求
- Sql server2012中触发器发送http请求
- 如何在WinForm中发送HTTP请求
- Java中发送Http请求Get、Post
- iOS中发送HTTP请求的方案
- Spring框架中发送http请求--RestTemplate
- Android中对服务器发送http请求
- 使用CURL来发送HTTP请求的方法
- Node.js中http 接收get请求、接收post请求、发送get请求、发送post请求
- SOAPUI 使用groovy发送http请求 自动化测试
- java发送http的get、post请求,测试端口
- URL与 HTTP,iOS中发送HTTP请求
- Gauge中执行Http请求测试
- C#发送HTTP请求
- Android:创建可穿戴应用 - 自定义布局
- 微软算法经典2 最长公共子序列问题
- 《Clean Code》学习笔记 2
- 对数据模型model中时间轴排序和compare的使用
- 陕西:3男子将2女子拐至偏僻山村强奸后活埋
- selenium中发送http请求来测试
- Hadoop Hive sql语法详解3--DML 操作:元数据存储
- POJ 2485 Highways (最小生成树)
- c遍历所有文件夹中的文件
- Linux cp命令直接覆盖不提示方法
- 微软算法经典3 求最长递减子序列
- Jlink_V8+mini2440+axd在线调试的配置
- 关系型和非关系型数据库——学习笔记
- Hadoop Hive sql语法详解4--DQL 操作:数据查询SQL