基于C#的接口自动化测试
Posted 这个达
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于C#的接口自动化测试相关的知识,希望对你有一定的参考价值。
其实就是找个地方然后给关键的代码做个笔记什么的……
字符串访问API接口,访问方法为POST:
string url = URL; string RequestParam = Param; string headername = HeaderName; string header = Header; string html = ""; try { WebRequest wbreq = WebRequest.Create(url); } catch (WebException WebEx) { Console.WriteLine("无法访问的URI:" + "\r\n" + WebEx.ToString()); } byte[] byteArray = Encoding.UTF8.GetBytes(RequestParam); { //POST访问接口 HttpWebRequest RequestInterfaceRequsetByString = (HttpWebRequest)HttpWebRequest.Create(new Uri(URL)); RequestInterfaceRequsetByString.KeepAlive = false; RequestInterfaceRequsetByString.ProtocolVersion = HttpVersion.Version11; RequestInterfaceRequsetByString.Method = "post"; RequestInterfaceRequsetByString.ContentType = "application/x-www-form-urlencoded"; RequestInterfaceRequsetByString.Timeout = -1;//超时时间设置为无限大 RequestInterfaceRequsetByString.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0"; Encoding encoding = Encoding.GetEncoding("utf-8"); RequestInterfaceRequsetByString.Headers.Add(headername, Header); try { Stream requeststream = RequestInterfaceRequsetByString.GetRequestStream(); requeststream.Write(byteArray, 0, byteArray.Length); requeststream.Close(); try { HttpWebResponse response = (HttpWebResponse)RequestInterfaceRequsetByString.GetResponse(); Stream responsestream = response.GetResponseStream(); StreamReader sr = new StreamReader(responsestream); html = sr.ReadToEnd(); //从头读到尾,放到字符串html responsestream.Close(); response.Close(); } catch (Exception ex) { html = ex.Message; } } catch (Exception ex) { html = ex.Message; } } return html; }
json转字典类型:
public static Dictionary<string, object> ConvertDictionary(string str) { javascriptSerializer jss = new JavaScriptSerializer(); //string jsstr = ConvertJsonString(str); try { return jss.Deserialize<Dictionary<string, object>>(str); } catch (Exception ex) { throw new Exception(ex.Message); } }
字典类型访问API接口:与字符串类型访问接口基本类似,但是需要加入字典相关的东西,访问方法为POST
Dictionary<string, object> requestdic = ConvertToDictionary.ConvertDictionary(RequestParam); StringBuilder buffer = new StringBuilder(); int i = 0; foreach (string key in requestdic.Keys) { if (i > 0) { buffer.AppendFormat("&{0}={1}", key, requestdic[key]); } else { buffer.AppendFormat("{0}={1}", key, requestdic[key]); } i++; } byte[] byteArray = Encoding.UTF8.GetBytes(buffer.ToString());
格式化json字符串:
public static string ConvertJsonString(string str) { //格式化json字符串 JsonSerializer serializer = new JsonSerializer(); TextReader tr = new StringReader(str); JsonTextReader jtr = new JsonTextReader(tr); object obj = serializer.Deserialize(jtr); if (obj != null) { StringWriter textWriter = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(textWriter) { Formatting = Formatting.Indented, Indentation = 4, IndentChar = ‘ ‘ }; serializer.Serialize(jsonWriter, obj); return textWriter.ToString(); } else { return str; } } }
以上是关于基于C#的接口自动化测试的主要内容,如果未能解决你的问题,请参考以下文章