mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-07 23:41:22 +08:00
har2case add profile args
This commit is contained in:
@@ -45,6 +45,11 @@ def init_har2case_parser(subparsers):
|
|||||||
help="Specify exclude keyword, url that includes exclude string will be ignored, "
|
help="Specify exclude keyword, url that includes exclude string will be ignored, "
|
||||||
"multiple keywords can be joined with '|'",
|
"multiple keywords can be joined with '|'",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--profile",
|
||||||
|
dest="profile",
|
||||||
|
help="Specify yaml file to overwrite headers and cookies in HAR.",
|
||||||
|
)
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
@@ -60,6 +65,6 @@ def main_har2case(args):
|
|||||||
output_file_type = "pytest"
|
output_file_type = "pytest"
|
||||||
|
|
||||||
capture_message(f"har2case {output_file_type}")
|
capture_message(f"har2case {output_file_type}")
|
||||||
HarParser(har_source_file, args.filter, args.exclude).gen_testcase(output_file_type)
|
HarParser(har_source_file, args.filter, args.exclude, args.profile).gen_testcase(output_file_type)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from sentry_sdk import capture_exception
|
|||||||
|
|
||||||
from httprunner.ext.har2case import utils
|
from httprunner.ext.har2case import utils
|
||||||
from httprunner.make import make_testcase, format_pytest_with_black
|
from httprunner.make import make_testcase, format_pytest_with_black
|
||||||
|
from httprunner.loader import load_test_file
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from json.decoder import JSONDecodeError
|
from json.decoder import JSONDecodeError
|
||||||
@@ -36,10 +37,11 @@ def ensure_file_path(path: Text) -> Text:
|
|||||||
|
|
||||||
|
|
||||||
class HarParser(object):
|
class HarParser(object):
|
||||||
def __init__(self, har_file_path, filter_str=None, exclude_str=None):
|
def __init__(self, har_file_path, filter_str=None, exclude_str=None, profile=None):
|
||||||
self.har_file_path = ensure_file_path(har_file_path)
|
self.har_file_path = ensure_file_path(har_file_path)
|
||||||
self.filter_str = filter_str
|
self.filter_str = filter_str
|
||||||
self.exclude_str = exclude_str or ""
|
self.exclude_str = exclude_str or ""
|
||||||
|
self.profile = profile and load_test_file(profile)
|
||||||
|
|
||||||
def __make_request_url(self, teststep_dict, entry_json):
|
def __make_request_url(self, teststep_dict, entry_json):
|
||||||
""" parse HAR entry request url and queryString, and make teststep url and params
|
""" parse HAR entry request url and queryString, and make teststep url and params
|
||||||
@@ -97,12 +99,15 @@ class HarParser(object):
|
|||||||
teststep_dict["request"]["method"] = method
|
teststep_dict["request"]["method"] = method
|
||||||
|
|
||||||
def __make_request_cookies(self, teststep_dict, entry_json):
|
def __make_request_cookies(self, teststep_dict, entry_json):
|
||||||
cookies = {}
|
if self.profile and self.profile.get("cookies"):
|
||||||
for cookie in entry_json["request"].get("cookies", []):
|
teststep_dict["request"]["cookies"] = self.profile.get("cookies")
|
||||||
cookies[cookie["name"]] = cookie["value"]
|
else:
|
||||||
|
cookies = {}
|
||||||
|
for cookie in entry_json["request"].get("cookies", []):
|
||||||
|
cookies[cookie["name"]] = cookie["value"]
|
||||||
|
|
||||||
if cookies:
|
if cookies:
|
||||||
teststep_dict["request"]["cookies"] = cookies
|
teststep_dict["request"]["cookies"] = cookies
|
||||||
|
|
||||||
def __make_request_headers(self, teststep_dict, entry_json):
|
def __make_request_headers(self, teststep_dict, entry_json):
|
||||||
""" parse HAR entry request headers, and make teststep headers.
|
""" parse HAR entry request headers, and make teststep headers.
|
||||||
@@ -128,15 +133,18 @@ class HarParser(object):
|
|||||||
}
|
}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
teststep_headers = {}
|
if self.profile and self.profile.get("headers"):
|
||||||
for header in entry_json["request"].get("headers", []):
|
teststep_dict["request"]["headers"] = self.profile.get("headers")
|
||||||
if header["name"] == "cookie" or header["name"].startswith(":"):
|
else:
|
||||||
continue
|
teststep_headers = {}
|
||||||
|
for header in entry_json["request"].get("headers", []):
|
||||||
|
if header["name"] == "cookie" or header["name"].startswith(":"):
|
||||||
|
continue
|
||||||
|
|
||||||
teststep_headers[header["name"]] = header["value"]
|
teststep_headers[header["name"]] = header["value"]
|
||||||
|
|
||||||
if teststep_headers:
|
if teststep_headers:
|
||||||
teststep_dict["request"]["headers"] = teststep_headers
|
teststep_dict["request"]["headers"] = teststep_headers
|
||||||
|
|
||||||
def _make_request_data(self, teststep_dict, entry_json):
|
def _make_request_data(self, teststep_dict, entry_json):
|
||||||
""" parse HAR entry request data, and make teststep request data
|
""" parse HAR entry request data, and make teststep request data
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class TestHar(TestHar2CaseUtils):
|
|||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.har_path = os.path.join(os.path.dirname(__file__), "data", "demo.har")
|
self.har_path = os.path.join(os.path.dirname(__file__), "data", "demo.har")
|
||||||
self.har_parser = HarParser(self.har_path)
|
self.har_parser = HarParser(self.har_path)
|
||||||
|
self.profile_path = os.path.join(os.path.dirname(__file__), "data", "profile.yml")
|
||||||
|
|
||||||
def test_prepare_teststep(self):
|
def test_prepare_teststep(self):
|
||||||
log_entries = load_har_log_entries(self.har_path)
|
log_entries = load_har_log_entries(self.har_path)
|
||||||
@@ -47,6 +48,18 @@ class TestHar(TestHar2CaseUtils):
|
|||||||
self.assertTrue(os.path.isfile(json_file))
|
self.assertTrue(os.path.isfile(json_file))
|
||||||
os.remove(json_file)
|
os.remove(json_file)
|
||||||
|
|
||||||
|
def test_profile(self):
|
||||||
|
har_parser = HarParser(self.har_path, profile=self.profile_path)
|
||||||
|
teststeps = har_parser._prepare_teststeps()
|
||||||
|
self.assertEqual(
|
||||||
|
teststeps[0]["request"]["headers"],
|
||||||
|
{"Content-Type": "application/x-www-form-urlencoded"},
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
teststeps[0]["request"]["cookies"],
|
||||||
|
{"CASTGC": "TGT"},
|
||||||
|
)
|
||||||
|
|
||||||
def test_filter(self):
|
def test_filter(self):
|
||||||
filter_str = "httprunner"
|
filter_str = "httprunner"
|
||||||
har_parser = HarParser(self.har_path, filter_str)
|
har_parser = HarParser(self.har_path, filter_str)
|
||||||
|
|||||||
4
tests/ext/har2case/data/profile.yml
Normal file
4
tests/ext/har2case/data/profile.yml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
headers:
|
||||||
|
Content-Type: "application/x-www-form-urlencoded"
|
||||||
|
cookies:
|
||||||
|
CASTGC: "TGT"
|
||||||
Reference in New Issue
Block a user