From a4a691ce2387cb065abe55d7b65f803698e96e1b Mon Sep 17 00:00:00 2001 From: Jonas <32525495+JoSglch@users.noreply.github.com> Date: Thu, 12 Jan 2023 16:05:22 +0100 Subject: [PATCH] Feature/limit step report output size (#224) * feat: limit step report output size --- .../test_stepreport/stepreport_example.py | 58 +++++++++++++++++++ .../test_result/templates/report_template.css | 5 ++ .../templates/report_template.html.j2 | 48 ++++++++++----- .../templates/report_template_script.js | 40 +++++++++++++ 4 files changed, 136 insertions(+), 15 deletions(-) create mode 100644 src/pykiso/test_result/templates/report_template_script.js diff --git a/examples/test_stepreport/stepreport_example.py b/examples/test_stepreport/stepreport_example.py index 3eb127ca..6eed2062 100644 --- a/examples/test_stepreport/stepreport_example.py +++ b/examples/test_stepreport/stepreport_example.py @@ -109,3 +109,61 @@ def test_run(self): self.assertAlmostEqual(voltage, 4, delta=1, msg="Check voltage device") logging.info(f"I HAVE RUN 0.1.1 for tag {self.tag}!") + + +@pykiso.define_test_parameters( + suite_id=1, + case_id=3, +) +class WrapLongTextTest(pykiso.BasicTest): + """This test shows wrapping of long results into foldable html elements""" + + def setUp(self): + """Set header information and check setup conditions""" + super().setUp() + # additional data to include in the step-report + self.step_report.header["Version_device"] = "2022-1234" + + def test_run(self): + """Write long results to the step report to show how foldable html + elements get used to make the report overview more readable + """ + logging.info( + f"--------------- RUN: {self.test_suite_id}, {self.test_case_id} ---------------" + ) + + # data to test + device_on = True + actual_dummy_result = {"result": True} + expected_dummy_result = { + "result": "pykiso is an integration test framework. With it, it is possible to write: Whitebox integration tests directly on my target device, Graybox integration tests to make sure the communication-link with my target device is working as expected, Blackbox integration tests to make sure my external device interfaces are working as expected", + } + + # check that it works with multiple tables + self.step_report.current_table = "First table" + + self.assertEqual( + expected_dummy_result, + expected_dummy_result, + msg="The very long message should be wrapped", + ) + + self.step_report.current_table = "Second table" + + with self.subTest("Non critical checks"): + # This check will fail but the test continues + self.assertFalse(device_on, msg="Some check") + + # assert with custom message + # assert msg overwritten when step_report_message not null + self.step_report.message = "Big data message" + + self.assertEqual( + expected_dummy_result, expected_dummy_result, msg="Check big data" + ) + + self.assertEqual( + expected_dummy_result, actual_dummy_result, msg="Check big data" + ) + + logging.info(f"I HAVE RUN 0.1.1 for tag {self.tag}!") diff --git a/src/pykiso/test_result/templates/report_template.css b/src/pykiso/test_result/templates/report_template.css index 427cf993..fdc81b09 100644 --- a/src/pykiso/test_result/templates/report_template.css +++ b/src/pykiso/test_result/templates/report_template.css @@ -61,6 +61,11 @@ body { text-transform: uppercase; } + summary { + text-decoration: underline; + cursor: pointer; + } + /* Media Queries*/ @media screen and (max-width: 600px) { diff --git a/src/pykiso/test_result/templates/report_template.html.j2 b/src/pykiso/test_result/templates/report_template.html.j2 index 85c45189..96680473 100644 --- a/src/pykiso/test_result/templates/report_template.html.j2 +++ b/src/pykiso/test_result/templates/report_template.html.j2 @@ -4,32 +4,31 @@ - {# For each TestClass #} + {# For each TestClass -#} {% for class_name, class_content in ALL_STEP_REPORT.items() -%}
- {# Add Title and description #} + {# Add Title and description -#}

ITF Test Report for: {{class_name}} - {% if class_content["succeed"] %} + {% if class_content["succeed"] -%} -> [Success] - {% else %} + {% else -%} -> [Fail] - {% endif %} + {%- endif %}

Test Description:

-

{{class_content["description"] | replace("\n", "
\n") }}

+

{{class_content["description"] | replace("\n", "
\n") }}

Date, Time, Software versions:

- {# Add additional information from header key #} + {# Add additional information from header key -#} {% for data in [class_content["time_result"], class_content["header"]] -%} {% for key, value in data.items() -%} {{key}}: {{value}}
{%- endfor %} {%- endfor %}

- - {# For each test (setUp, run, tearDown) #} - {% for test_name, test_content in class_content["test_list"].items() -%} + {#- For each test (setUp, run, tearDown) -#} + {% for test_name, test_content in class_content["test_list"].items() %} {% set test_success = is_test_success(test_content) -%}

{{test_name}} {% if test_success -%}Success @@ -38,7 +37,7 @@ - {# Set Columns name from the first row, excluding succeed flag #} + {# Set Columns name from the first row, excluding succeed flag -#} {% for column_name in test_content[0].keys() if column_name != "succeed" -%} @@ -46,7 +45,7 @@ - {# Loop over each assert method called (step-report) -#} + {#- Loop over each assert method called (step-report) -#} {% for row in test_content -%} {# Set cell color variable according to the assert result and emptiness -#} {% if row.pop("succeed") -%} @@ -56,13 +55,28 @@ {% set color_cell = "background-color: rgb(236, 160, 160);" -%} {% set color_empty_cell = "background-color: rgb(236, 160, 160);" -%} {%- endif %} + {# Needed for setting unique class per row in html in inner loop -#} + {% set row_index = loop.index -%} - {# Loop over each cell of the row -#} + {#- Loop over each cell of the row -#} {% for col_value in row.values() -%} - {# Set the value and apply colors to the cell -#} + {#- Set the value and apply colors to the cell #} {%- endfor %} @@ -79,3 +93,7 @@ + + diff --git a/src/pykiso/test_result/templates/report_template_script.js b/src/pykiso/test_result/templates/report_template_script.js new file mode 100644 index 00000000..40c20dff --- /dev/null +++ b/src/pykiso/test_result/templates/report_template_script.js @@ -0,0 +1,40 @@ +/** + * synchronize the toggling of all details elements by class name + * class name is unique for each test + row combination and gets rendered in by jinja + * @param {HTMLDetailsElement} element the details element being toggled + * @param {string} className class attribute of all details elements in the row + */ +function toggleDetailsInRow(element, className) { + element.scrollIntoView(); + const isOpen = element.hasAttribute("open"); + const detailsElements = document.getElementsByClassName(className); + for (details of detailsElements) { + details.open = isOpen; + isOpen ? setSummary("Click to minimize", details) : setTruncatedSummary(details); + } +} + +/* helper functions */ + +/** + * set the summary text of the details element + * @param {string} content new content of the summary element + * @param {HTMLDetailsElement} detailsElement parent of the summary being modified + */ +function setSummary(content, detailsElement) { + const summaryElement = detailsElement.getElementsByTagName("summary")[0]; + summaryElement.innerHTML = content; +} + +/** + * same result as jinja truncate when rendering to set summary after closing + * @param {HTMLDetailsElement} detailsElement parent of the summary being modified + * @param {number} length length to cut the content to, default is 50 + * @returns the truncated string + */ +function setTruncatedSummary(detailsElement, length = 50) { + const divElement = detailsElement.getElementsByTagName("div")[0]; + let shortened = divElement.textContent.trim().slice(0, length); + shortened = shortened + "..."; + setSummary(shortened, detailsElement); +}
Step{{column_name}}
{{loop.index}} - {{col_value}} + {# Use a
if content is too long for better results overview and synchronize toggling them per row -#} + {% if col_value | string | length > 100 -%} +
+ {{col_value | string | truncate(53, true, leeway = 0)}} +
+
+ {{col_value}} +
+
+ {%- else -%} +
+ {{col_value}} +
+ {%- endif %}