Prepare enviroment
- Download IntelliJ IDEA
新建一个gradle工程
- path: http://start.spring.io/
-
pic
add dependency in project
compile "org.projectlombok:lombok:1.16.18"
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-test')
compile group: 'org.springframework', name: 'spring-web', version:'5.0.8.RELEASE'
compile group: 'com.alibaba', name: 'fastjson', version:'1.2.47'
//compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version:'2.8.10'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version:'2.8.10'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version:'2.8.0'
compile group: 'io.cucumber', name: 'cucumber-java', version:'2.3.1'
compile group: 'io.cucumber', name: 'cucumber-junit', version:'2.3.1'
compile group: 'junit', name: 'junit', version:'4.12'
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version:'3.13.0'
compile 'com.google.guava:guava:18.0'
compile 'org.apache.httpcomponents:httpclient:4.4.1'
Develop API Framework
- Get Token example for "x-www-form-urlencoded"
String tokenURL =" Your token url";
HttpHeaders tokenRequestHeaders = new HttpHeaders();
tokenRequestHeaders.add("Content-Type", " application/x-www-form-urlencoded");
tokenRequestHeaders.add("Accept", "application/json");
String tokenRequestBody = "Your project body"(can check in postman)
//tokenRequestHeaders.add("Authorization", "Basic Y2xpZW50OnBhc3N3b3Jk");
RestTemplate tokenTemplate = new RestTemplate();
//HttpEntity> request = new HttpEntity>(popHeaders(), tokenRequestHeaders);
HttpEntity tokenRequestEntity = new HttpEntity(tokenRequestBody, tokenRequestHeaders);
ResponseEntity tokenResponse = tokenTemplate.exchange(tokenURL, HttpMethod.POST, tokenRequestEntity, String.class);
JSONObject tokenResponseBody = JSONObject.parseObject(tokenResponse.getBody());
String accessToken = tokenResponseBody.get("access_token").toString();
return accessToken;
- Get Token example for "form-data"
String tokenURL =" Your token url";
HttpHeaders tokenRequestHeaders = new HttpHeaders();
tokenRequestHeaders.add("Content-Type", " form-data");
tokenRequestHeaders.add("Accept", "application/json");
String tokenRequestBody = "Your project body"(can check in postman)
//tokenRequestHeaders.add("Authorization", "Basic Y2xpZW50OnBhc3N3b3Jk");
RestTemplate tokenTemplate = new RestTemplate();
HttpEntity> request = new HttpEntity>(popHeaders(), tokenRequestHeaders);
//HttpEntity tokenRequestEntity = new HttpEntity(tokenRequestBody, tokenRequestHeaders);
ResponseEntity tokenResponse = tokenTemplate.exchange(tokenURL, HttpMethod.POST, request , String.class);
JSONObject tokenResponseBody = JSONObject.parseObject(tokenResponse.getBody());
String accessToken = tokenResponseBody.get("access_token").toString();
return accessToken;
protected static MultiValueMap popHeaders() {
MultiValueMap map = new LinkedMultiValueMap();
//get from postman
map.add("grant_type","grant_type");
map.add("username","user_name");
map.add("password","user_password");
return map;
}
- Send request to API
public static ResponseEntity getObject() {
String accessToken = Token.getToken();
String url = "api_url";
HttpHeaders APIRequestHeaders = new HttpHeaders();
APIRequestHeaders.add("Authorization", "bearer " + accessToken);
APIRequestHeaders.add("Content-Type", "application/json");
RestTemplate APITemplate = new RestTemplate();
HttpEntity APIRequestEntity = new HttpEntity(null, APIRequestHeaders);
ResponseEntity APIResponse = APITemplate.exchange(url, HttpMethod.GET, APIRequestEntity, String.class);
return APIResponse;
}
- Analyze response code
public void Get_Response(String id) throws Exception {
ResponseEntity APIResponse = Service.getSomeById(aliasValue(id), Object().toBuilder().name(aliasValue(id)).build(),1);
actualStatusCode = APIResponse.getStatusCodeValue();
String body="["+APIResponse.getBody()+"]";
JSONArray APIResponseBody = JSONArray.parseArray(body);
JSONObject firstImage = APIResponseBody.getJSONObject(0);
}
Develop test cases for API
- Develop .feature file
Under Resources, add .feature file for your project
For Cucumber some key word:
- Feature
- Scenario
- Before
- Given
- When
- Then
- And
- After
should add some tags in the .feature file
For example:
@Tag1
@Tag1_ForFeature1
Feature: This feature is for test
@Tag1_ForFeature1_Test1
Scenario: I want to do something
Given a precondition
When I do something
Then should get result1(assertion)
And should get result2(assertion)
-Develop step definition
In Src folder add, java classes for step definitions
Add function for each step
In step definition, should implement all steps in .feature file
- Trigger Run
Add a class: Run CucumberTest under test
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/main/resources",tags = "@tag")
public class RunCucumberTest {
}
- That's all.