2018-10-17java配置引用变量以及正则表达式

2018-10-17  本文已影响39人  心若无情

第一步

打开 file-setting-Editor-Live Templates


image.png

第二步,新建一个

点 右上角 + Tempiate Group


image.png

第三步,自定义名称

image.png

第四步,内部添加列表

点右上角+在 Live Template


image.png

第五步,设置第一个变量名

image.png

需要用到的语句

*
 *  
 * 
 * @author chenmc
 * @date $date$ $time$
 * @param $params$
 * @return $returns$
 */

第六步设置get 请求方法

image.png
@Test
@Parameters({"host"})
public void $functionName$(String host){
    System.out.println(host);
    // API为接口地址,data为请求数据
    String url = host + "$API$" + "?" +"$data$";
    System.out.println(url);
    //result 为响应结果
    String result = HttpClientUtil.doGet(url);
    System.out.println(result);
    //assert 为预期结果
    boolean actual = result.contains("$assert$");
    Assert.assertEquals(actual, true);
}

第七步设置 post请求方法

image.png

需要黏贴的语句

@Test
@Parameters({"host"})
public void $functionName$(String host) {
        
    System.out.println(host);
    // API为接口地址
    String url = host + "$API$";
    System.out.println(url);
    //bodyData 请求参数
    String bodyData = "$bodyData$";
    //键值对 application/x-www-form-urlencoded
    //json  application/json
    //xml  application/xml
    String contentType = "$contentType$";
    String result = HttpClientUtil.doPost(url,contentType,bodyData);
    System.out.println(result);
    //assert 为预期结果
    boolean actual = result.contains("$assert$");
    Assert.assertEquals(actual, true);
}

第八步 正则提取式设置

image.png

需要黏贴的语句

//正则表达式
String pattern = "$re1$";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(result);
String c = "";
if(m.find()){
    String s = m.group(0);
    System.out.println(s);
    //正则表达式
    String pattern1 = "$re2$";
    Pattern r1 = Pattern.compile(pattern1);
    Matcher m1 = r1.matcher(s);
    if(m1.find()){
        c = m1.group(0);
        System.out.println(c);
    }else{
        System.out.println( s + " no matcher");
    }

}else{
    System.out.println(result + " no matcher");
}

例子
如果出现发红的地方 直接 alt+回车键即可补全

public class TestNEW {
  public String csId;
@Test
@Parameters({"host"})
public void testUserSignUp(String host) {
    System.out.println(host);
    // API为接口地址
    String url = host + "/user/signup";
    System.out.println(url);
    //bodyData 请求参数
    String bodyData = "{\n"
        + "  \"phone\": \"18939934759\",\n"
        + "  \"pwd\": \"jia123456\",\n"
        + "  \"rePwd\": \"jia123456\",\n"
        + "  \"userName\": \"jia968\"\n"
        + "}";
    //键值对 application/x-www-form-urlencoded
    //json  application/json
    //xml  application/xml
    String contentType = "application/json";
    String result = HttpClientUtil.doPost(url,contentType,bodyData);
    System.out.println(result);
    //正则表达式
    String pattern = "\"cstId\":(.*?),";
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(result);
    String c = "";
    if(m.find()){
      String s = m.group(0);
      System.out.println(s);
      //正则表达式
      String pattern1 = "\\d+";
      Pattern r1 = Pattern.compile(pattern1);
      Matcher m1 = r1.matcher(s);
      if(m1.find()){
        c = m1.group(0);
        System.out.println(c);
      }else{
        System.out.println( s + " no matcher");
      }
    
    }else{
      System.out.println(result + " no matcher");
    }
  csId =c;
    //assert 为预期结果
    boolean actual = result.contains("0000");
    Assert.assertEquals(actual, true);
}
  @Test
  @Parameters({"host"})
  public void testCstreaLname(String host) {
          
      System.out.println(host);
      // API为接口地址
      String url = host + "/cst/realname";
      System.out.println(url);
      //bodyData 请求参数
      String bodyData = "{\n"
          + "  \"birthday\": \"2018-10-17T03:53:27.008Z\",\n"
          + "  \"certno\": \"342423198105063389\",\n"
          + "  \"city\": \"上海\",\n"
          + "  \"cstId\":"+ csId+" ,\n"
          + "  \"cstName\": \"贾成宝\",\n"
          + "  \"email\": \"953719054@qq.com\",\n"
          + "  \"province\": \"安徽\",\n"
          + "  \"sex\": 1\n"
          + "}";
      //键值对 application/x-www-form-urlencoded
      //json  application/json
      //xml  application/xml
      String contentType = "application/json";
      String result = HttpClientUtil.doPost(url,contentType,bodyData);
      System.out.println(result);
      //assert 为预期结果
      boolean actual = result.contains("0000");
      Assert.assertEquals(actual, true);
  }
}

上一篇下一篇

猜你喜欢

热点阅读