LoadRunner进行接口自动化测试

我们以测试豆瓣api获取图书信息的接口为例
  接口的信息如下:
  接口ip:https://api.douban.com/v2/book/:id
  接口返回值:status=200
  返回数据:
{
… (图书信息的其他部分)
"current_user_collection": {
"status":"read",
"rating": {
"max":5,
"value":"5",
"min":0
},
"updated":"2012-11-2012:08:04",
"user_id":"33388491",
"book_id":"6548683",
"id":605519800
}
}
  第一步:
  此请求是https请求的,对于loadrunner12以下的版本,需要加上下面这句才能测试。
  web_set_sockets_option("SSL_VERSION","TLS")
  第二步:
  写接口的请求,用的是web_custome_request。
  web_custom_request("web_custom_request",
  "URL=https://api.douban.com/v2/book/1003078",//书的id号我们用1003078
  "Method=GET",//从接口说明书得知它是get请求
  "TargetFrame=",
  "Resource=0",
  "Referer=",
  "Body=",
  LAST);
  第三步:
  需要检查返回值是否正确,可以通过检查response返回值判断请求是否正确。
  HttpRetCode = web_get_int_property(HTTP_INFO_RETURN_CODE);
  if (HttpRetCode == 200)
  {lr_log_message("success");
  }
  else{
  lr_log_message(" failed");
  }
  注意上面的HttpRetCode变量需要在脚本中声明。
  最后:给出大家完成的代码。
Action()
{
int HttpRetCode;
web_set_sockets_option("SSL_VERSION","TLS");//主要是为了解决测试https请求的问题
web_custom_request("web_custom_request",
"Method=GET",
"TargetFrame=",
"Resource=0",
"Referer=",
"Body=",
LAST);
HttpRetCode = web_get_int_property(HTTP_INFO_RETURN_CODE);
if (HttpRetCode == 200)
{lr_log_message("success");
}
else{
lr_log_message(" failed");
}
return 0;
}
posted on 2022-09-21 18:29  胖妞的瘦猴  阅读(203)  评论(0编辑  收藏  举报