TEST Engineering/API Test Tips
Postman Pre-requests 변수 사용
얍얍폴폴
2023. 9. 5. 13:42
포스트맨에서 Pre-requests 탭에서 미리 호출한 api 의 응답 값을 Body 탭의 json 값에 변수로 사용 하고 싶은 경우가 있다.
매번 다시 하려면 잊어 버려서 시간이 오래 걸려 기억 차원에서 간단히 정리 한다.
시나리오) Post 메소드를 호출 해 사이트에 로그인 하기 위한 스레드를 만들 예정
Body 탭에 필요한 파라미터 = 로그인할 ID, PW, security Key
Pre-requests 탭 = security Key 를 가지고 오기 위한 java script
// URL에 사용할 userid와 url을 const 를 사용해 선언
// const userid = 'qatest';
const userid = pm.variables.get("user");
const url = 'https://test.security.key/code?user_id='+userid;
// get method의 Request를 실행 하고 받은 response 데이터 중 securityKey, userID를 변수로 지정
pm.sendRequest({
url: url,
method: 'GET',
headers : {
'Content-Type': 'application/json'
}
}, function(err, response) {
if (err) {
console.error(err);
} else {
// response body를 선언, pm.variables.set을 변수로 사용
const responseBody = response.json()
pm.variables.set("securityKey", responseBody.securityKey);
pm.variables.set("user_id", responseBody.userID);
}
});
Body 탭에서 json을 다음과 같이 {{}} 묶어서 사용
{
"securityKey": "{{securityKey}}",
"password": "test1234",
"userId": "{{user_id}}"
}
Tests 탭은 아래 블로그 참고~
https://passorfail.tistory.com/158
Postman Tests 탭에서 변수 선언
앞에서 Pre-requests 탭에서 호출하여 받은 변수를 가지고 Post method의 변수로 사용했고, https://passorfail.tistory.com/157 Postman Pre-requests 변수 사용 포스트맨에서 Pre-requests 탭에서 미리 호출한 api 의 응답
passorfail.tistory.com