Skip to content

Commit

Permalink
Merge pull request #315 from NiceProjectPoland/Test-#196-for-Issue-#38
Browse files Browse the repository at this point in the history
Test case for Issue #38 - Verify docx raport content
  • Loading branch information
damies13 authored Oct 18, 2024
2 parents 46f6091 + 527afd1 commit 4149ea4
Show file tree
Hide file tree
Showing 222 changed files with 154,786 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Tests/Regression/Reporter/GUI_Common.robot
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ Library OperatingSystem
Library Process
Library String
Library Collections
Library DocTest.VisualTest

Library ImageHorizonLibrary reference_folder=${IMAGE_DIR}
Library OCRLibrary

Library read_docx.py

*** Variables ***
${default_image_timeout} ${120}
Expand Down
503 changes: 503 additions & 0 deletions Tests/Regression/Reporter/GUI_Features.robot

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Tests/Regression/Reporter/pip_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
numpy>2; python_version > '3.12' and platform_system == 'Windows'
PyMuPDF>=1.23.1; python_version > '3.12' and platform_system == 'Windows'
robotframework
robotframework-doctestlibrary
# pyscreeze 0.1.29 doesn't seem to work with pyautogui 0.9.53
pyscreeze==0.1.28
# pyautogui 0.9.54 has issues on MacOS https://github.com/asweigart/pyautogui/issues/783
# pyautogui is a prerequisite of imagehorizonlibrary
pyautogui!=0.9.54
robotframework-imagehorizonlibrary
opencv-python
Pillow
robotframework-ocrlibrary
clipboard
python-docx
128 changes: 128 additions & 0 deletions Tests/Regression/Reporter/read_docx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import os
import re
import subprocess

from docx import Document
from docx.oxml.shared import OxmlElement, qn
from PIL import Image


def read_paragraphs_docx_file(docx_path: str) -> dict:
"""Read paragraphs docx file and return dictionary."""
doc = Document(docx_path)

doc_sections: dict = {}
current_heading = None

for paragraph in doc.paragraphs:
if paragraph.style.name.startswith('Heading'):
current_heading = paragraph.text.strip()
doc_sections[current_heading] = []

elif paragraph.text.strip():
if current_heading:
doc_sections[current_heading].append(paragraph.text.strip())
else:
doc_sections.setdefault('No Heading', []).append(paragraph.text.strip())

return doc_sections


def update_docx_with_libreoffice(docx_path: str):
"""Not working currently."""
command = [
'libreoffice', '--headless', '--convert-to', 'docx',
'--outdir', os.path.dirname(docx_path), docx_path
]
process = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.returncode != 0:
raise Exception(f"LibreOffice failed: {process.stderr.decode('utf-8')}")
else:
print(f"Document fields updated successfully: {docx_path}")


def update_table_of_contents(docx_path: str):
"""Set updateFields in DOCX file to true."""
doc = Document(docx_path)

settings_element = doc.settings.element
update_fields_element = OxmlElement('w:updateFields')
update_fields_element.set(qn('w:val'), 'true')

settings_element.append(update_fields_element)

doc.save(docx_path)


def convert_image_to_black_and_white(image_path: str):
"""Convert given image to black and white.
All colours except white will be converted to black."""
image = Image.open(image_path)
gray_image = image.convert("L")
bw_image = gray_image.point(lambda x: 0 if x < 255 else 255, '1')
bw_image.save(image_path)


def extract_images_from_docx(docx_path: str, output_folder: str, black_and_wite=False) -> list:
"""
Extract all images from DOCX one by one.
Returns list of the saved images.
"""
doc = Document(docx_path)
saved_images = []

if not os.path.exists(output_folder):
os.makedirs(output_folder)

for rel in doc.part.rels.values():
if "image" in rel.target_ref:
img_name = os.path.basename(rel.target_ref)
img_data = rel.target_part.blob

with open(os.path.join(output_folder, img_name), "wb") as img_file:
img_file.write(img_data)
if black_and_wite:
convert_image_to_black_and_white(os.path.join(output_folder, img_name))
print(f"DOCX RFSwarm raport image saved as: {img_name}")
saved_images.append(img_name)

return saved_images


def extract_tables_from_docx(docx_path: str) -> list:
"""
Extract all tables from DOCX one by one.
Returns nested lists of tables.
Note: For RFSwarm DOCX raport the first value of each row will be empty.
"""
doc = Document(docx_path)
tables = []

def iter_unique_row_cells(row):
"""Generate cells in given row skipping empty grid cells."""
last_cell_tc = None
for cell in row.cells:
this_cell_tc = cell._tc
if this_cell_tc is last_cell_tc:
continue
last_cell_tc = this_cell_tc
yield cell

for table in doc.tables:
table_data = []
for row in table.rows:
row_data = []
for cell in iter_unique_row_cells(row):
row_data.append(cell.text)
table_data.append(row_data)
tables.append(table_data)

return tables


def sort_docx_images(docx_images: list) -> list:
"""Sort given list using natural sorting"""
def natural_sort_key(filename):
return [int(text) if text.isdigit() else text for text in re.split(r'(\d+)', filename)]

return sorted(docx_images, key=natural_sort_key)
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
==============================================================================
Odoo
==============================================================================
Odoo Sales | FAIL |
Text 'New' did not appear in 2 minutes.
------------------------------------------------------------------------------
Odoo | FAIL |
1 test, 0 passed, 1 failed
==============================================================================
Output: /tmp/rfswarmagent/logs/20230728_154253_Odoo-demo/Odoo_1_1_1690522997_1_1690522999/Odoo_Sales_output.xml
Log: /tmp/rfswarmagent/logs/20230728_154253_Odoo-demo/Odoo_1_1_1690522997_1_1690522999/log.html
Report: /tmp/rfswarmagent/logs/20230728_154253_Odoo-demo/Odoo_1_1_1690522997_1_1690522999/report.html
Loading

0 comments on commit 4149ea4

Please sign in to comment.