首页 > 其他分享 >Cucumber post API

Cucumber post API

时间:2023-03-21 10:58:53浏览次数:39  
标签:String cucumber API Cucumber io restassured import post public

1. 接口

Request URL: http://10.194.109.125/UserHabit/ICSUI/QuickLogin
Request Method: POST
Status Code: 204 No Content

 

2. feature 文件

Feature: API test


  Scenario: Login with role
    Given  LoginWithRole
      |Environment       |DT5                   |
      |UserId            |allencckan            |
      |BusinessFunctionId|2                     |
      |BusinessFunction  |BOCC                  |
      |RoleId            |16                   |
      |RoleName          |Assistant BOCC Manager|
      |FromEntry         |/QuickAccess/ICSUI    |
    Then LoginWithRoleSuccess

 

3. TestRunner文件

package APITestCase.TestRunner;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;


@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/APITestCase/Resources/",
        glue = {"Login/"},
        plugin = {"pretty","json:src/TestResult/cucumber.json","junit:src/TestResult/cucumber.xml"},
        tags = "@Smoke"
//        dryRun = true
)

//
public class TestRunner  extends AbstractTestNGCucumberTests{
//    public static void main(String[] args){
//        //System.out.println("args");
//        //System.out.println(args);
////        JUnitCore.main("TestRunner.TestRunner");
//    }
}

 

4. 公共方法封装文件

package APITestCase.APICommonMethods;

import static io.restassured.RestAssured.*;

import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class commonMethods {
    public Response postRequest(String baseURI, String basePath, String methodPath, RequestSpecification requestSpecification){
        Response postResponse = with().spec(requestSpecification).baseUri(baseURI).basePath(basePath).log().all().when().post(methodPath);
        return postResponse;
    }
}

 

5. Given Then 方法

package APITestCase.Login;

import APITestCase.APICommonMethods.commonMethods;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

import java.util.HashMap;
import java.util.Map;

import static io.restassured.RestAssured.given;

public class LoginWithRole {
    public Response loginWithRoleResponse;
    @Given("LoginWithRole")
    public void loginRole(Map<String, String> dataTable){
        commonMethods cm = new commonMethods();
        String baseURI = "http://10.194.109.125";
        String basePath = "UserHabit";
        String methodPath = "ICSUI/QuickLogin";
        Map<String,String> headerMap =new HashMap();
        headerMap.put("Accept", "application/json,text/javascript");
        headerMap.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        RequestSpecification loginRole = new RequestSpecBuilder().addHeaders(headerMap).addParams(dataTable).build();
        Response loginRoleResponse = cm.postRequest(baseURI,basePath,methodPath,loginRole);
        this.loginWithRoleResponse = loginRoleResponse;
    }

    @Then("LoginWithRoleSuccess")
    public void logninWithRole(){
        this.loginWithRoleResponse.then().statusCode(204);
    }
}

 

标签:String,cucumber,API,Cucumber,io,restassured,import,post,public
From: https://www.cnblogs.com/baxianhua/p/17239134.html

相关文章