Skip to content

Commit

Permalink
add more collection file
Browse files Browse the repository at this point in the history
updated
  • Loading branch information
lqthang2204 committed Dec 11, 2024
1 parent e0eadd1 commit b07bb1f
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 8 deletions.
14 changes: 12 additions & 2 deletions features/scenarios/api/Newman.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@ Feature: Demo features run newman
@newman @api
Scenario: run newman without data, method get
Given I navigate to url GURU99
And I run postman collection with file apichallenges.postman_collection.json
# Enter steps here
And I run postman collection file apichallenges.postman_collection.json
# Enter steps here

@newman @api @apichallenges3
Scenario: run newman with multi collection
Given I navigate to url GURU99
And I run postman collection file apichallenges3.postman_collection.json

@newman @api @apichallenges4
Scenario: run newman with collection and data file
Given I navigate to url GURU99
And I run postman collection file apichallenges4.postman_collection.json with data file apichallenges_2.data.json
18 changes: 15 additions & 3 deletions features/steps/api_stepdefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ def step_impl(context, payload_file):
# After reading We can read the Datatable and replace the values with some Runtime values also using the function get_test_data_for()
payload_json = json.loads(payload_json)
context.req.set_payload(payload_json, context)


@step(u'I set payload {payload_file}')
def step_impl(context, payload_file):
if context.table:
logger.error('you are setting below attributes with datatable but it is not required, please choise script "set payload {payload_file} with below attributes"')
logger.error(
'you are setting below attributes with datatable but it is not required, please choise script "set payload {payload_file} with below attributes"')
else:
payload_file = os.path.join(context.root_path, 'resources', 'api', 'request-json', payload_file + '.json')
logger.info(f'payload file : {payload_file}')
Expand Down Expand Up @@ -79,7 +82,9 @@ def step_impl(context, api_method):
if row[0].lower() == 'param':
list_data.append(row[1])
elif row[0].lower() == 'path':
context.req.api_base_url = context.req.api_base_url.replace('{' + row[1] + '}', get_test_data_for(row[2], context.dict_save_value))
context.req.api_base_url = context.req.api_base_url.replace('{' + row[1] + '}',
get_test_data_for(row[2],
context.dict_save_value))
if list_data:
context.req.params = json.dumps(list_data)
# code to be moved into a separate function as it will include a lot of detailing based on Issue #30
Expand Down Expand Up @@ -127,8 +132,15 @@ def step_impl(context, api_method, times_number):
break
if not success:
raise Exception('Polling attempts exceeded')
@step(u'I run postman collection with file {collection_json_file}')

@step(u'I run postman collection file {} with data file {}')
def step_impl(context, collection_json_file, data_file):
collection_path = os.path.join(context.root_path, "resources/postman-test/collection", f"{collection_json_file}")
data_file_path = os.path.join(context.root_path, "resources/postman-test/data-file", f"{data_file}")
api_newman.run_command(collection_path, data_file_path)
@step(u'I run postman collection file {}')
def step_impl(context, collection_json_file):
file_path = os.path.join(context.root_path, "resources/postman-test/collection", f"{collection_json_file}")
api_newman.run_command(file_path)


4 changes: 1 addition & 3 deletions libraries/api/api_newman.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ def run_command(file, data_file = 0):
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

output, error = p.communicate()

logger.debug(output.decode('utf-8').strip())
# Handle the process return code
if p.returncode != 0:
error_msg = error.decode('utf-8').strip() if error else 'Unknown error'
response = f"Error while running the command: {error_msg}"
assert False, response
else:
# GREEN = "\033[32m"
# RESET = "\033[0m"
response = f"Command succeeded:\n{output.decode('utf-8').strip()}"
# logging.(response)
logger.info(response)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"info": {
"_postman_id": "cd65f242-28bb-445d-8f99-bb8838873389",
"name": "New Collection",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "12276119",
"_collection_link": "https://techsdd.postman.co/workspace/ATA-ticket~bd7140bf-47c0-42b2-849f-8629af1a85b0/collection/12276119-cd65f242-28bb-445d-8f99-bb8838873389?action=share&source=collection_link&creator=12276119"
},
"item": [
{
"name": "get all",
"event": [
{
"listen": "test",
"script": {
"exec": [
"json_response = JSON.parse(responseBody);\r",
"entities_id = json_response.entities[0]['id'];\r",
"pm.globals.set(\"entities_id\", entities_id);\r",
"console.log(\"texst\"+ entities_id)\r",
"\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);})"
],
"type": "text/javascript",
"packages": {}
}
}
],
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "https://apichallenges.eviltester.com/sim/entities",
"protocol": "https",
"host": [
"apichallenges",
"eviltester",
"com"
],
"path": [
"sim",
"entities"
]
}
},
"response": []
},
{
"name": "get single",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
"console.log(entities_id)"
],
"type": "text/javascript",
"packages": {}
}
},
{
"listen": "test",
"script": {
"exec": [
"json_response = JSON.parse(responseBody);\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);})\r",
"\r",
" \r",
"pm.test(\"Body is correct\", function () {\r",
" json_response['name'] === 'entity number 5';\r",
"});"
],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://apichallenges.eviltester.com/sim/entities/{{entities_id}}",
"protocol": "https",
"host": [
"apichallenges",
"eviltester",
"com"
],
"path": [
"sim",
"entities",
"{{entities_id}}"
]
}
},
"response": []
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"info": {
"_postman_id": "c11b0163-1394-43de-8a02-02c1e1079871",
"name": "New Collection",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "12276119",
"_collection_link": "https://techsdd.postman.co/workspace/ATA-ticket~bd7140bf-47c0-42b2-849f-8629af1a85b0/collection/12276119-c11b0163-1394-43de-8a02-02c1e1079871?action=share&source=collection_link&creator=12276119"
},
"item": [
{
"name": "get all",
"event": [
{
"listen": "test",
"script": {
"exec": [
"\r",
"\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);})"
],
"type": "text/javascript",
"packages": {}
}
}
],
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url-qa-test}}/sim/entities",
"host": [
"{{url-qa-test}}"
],
"path": [
"sim",
"entities"
]
}
},
"response": []
},
{
"name": "add entities",
"event": [
{
"listen": "test",
"script": {
"exec": [
"json_response = JSON.parse(responseBody);\r",
"pm.test(\"Status code is 201\", function () {\r",
" pm.response.to.have.status(201);})\r",
"\r",
" \r",
"pm.test(\"Body is correct\", function () {\r",
" json_response['name'] === 'bob'\r",
"});\r",
"entities_id = json_response['id']\r",
"pm.globals.set(\"entities_id\", entities_id)"
],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"name\": {{name}}\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url-qa-test}}/sim/entities",
"host": [
"{{url-qa-test}}"
],
"path": [
"sim",
"entities"
]
}
},
"response": []
},
{
"name": "get single",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
"console.log(entities_id)"
],
"type": "text/javascript",
"packages": {}
}
},
{
"listen": "test",
"script": {
"exec": [
"json_response = JSON.parse(responseBody);\r",
"pm.test(\"Status code is 200\", function () {\r",
" pm.response.to.have.status(200);})\r",
"\r",
" \r",
"pm.test(\"Body is correct\", function () {\r",
" json_response['name'] === 'bob';\r",
"});"
],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{url-qa-test}}/sim/entities/{{entities_id}}",
"host": [
"{{url-qa-test}}"
],
"path": [
"sim",
"entities",
"{{entities_id}}"
]
}
},
"response": []
}
]
}
5 changes: 5 additions & 0 deletions resources/postman-test/data-file/apichallenges_2.data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[ {
"url-qa-test": "https://apichallenges.eviltester.com",
"name": "bob"

} ]

0 comments on commit b07bb1f

Please sign in to comment.