-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1173 lines (953 loc) · 43.3 KB
/
main.py
File metadata and controls
1173 lines (953 loc) · 43.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Test Evidence Helper - Desktop Application
A tool for manual software testers to capture screenshots and notes,
organize them by test steps, and export to Word documents.
Requirements:
pip install pyqt5 keyboard python-docx pandas pillow openpyxl pyperclip
"""
import sys
import os
import threading
import tempfile
import logging
from pathlib import Path
from datetime import datetime
from typing import List, Optional, Dict, Any
from dataclasses import dataclass, field
import re
import platform
# Third-party imports
try:
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QPushButton, QLabel, QTextEdit, QFileDialog, QLineEdit,
QMessageBox, QStatusBar, QGroupBox, QTableWidget, QTableWidgetItem,
QDialog, QDialogButtonBox, QHeaderView, QComboBox, QCheckBox
)
from PyQt5.QtCore import Qt, QTimer, pyqtSignal, QObject
from PyQt5.QtGui import QIcon, QFont
except ImportError as e:
print(f"PyQt5 not found: {e}. Install via: pip install pyqt5")
sys.exit(1)
try:
import keyboard
except ImportError as e:
print(f"keyboard module not found: {e}. Install via: pip install keyboard")
sys.exit(1)
try:
from docx import Document
from docx.shared import Inches, Pt, RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
except ImportError as e:
print(f"python-docx not found: {e}. Install via: pip install python-docx")
sys.exit(1)
try:
from PIL import ImageGrab, Image
except ImportError as e:
print(f"PIL/Pillow not found: {e}. Install via: pip install pillow")
sys.exit(1)
try:
import pandas as pd
except ImportError as e:
print(f"pandas not found: {e}. Install via: pip install pandas openpyxl")
sys.exit(1)
try:
import pyperclip
except ImportError as e:
print(f"pyperclip not found: {e}. Install via: pip install pyperclip")
sys.exit(1)
# ==================== CONFIGURATION ====================
CAPTURE_HOTKEY = "ctrl+alt+s"
NEXT_STEP_HOTKEY = "ctrl+alt+right"
PREV_STEP_HOTKEY = "ctrl+alt+left"
# Logging configuration
LOG_DIR = Path.home() / ".test_evidence_helper"
LOG_DIR.mkdir(exist_ok=True)
LOG_FILE = LOG_DIR / "app.log"
# ==================== LOGGING SETUP ====================
def setup_logging() -> logging.Logger:
"""Configure application logging with rotation and proper formatting."""
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Avoid duplicate handlers
if logger.handlers:
return logger
# File handler with rotation
from logging.handlers import RotatingFileHandler
file_handler = RotatingFileHandler(
LOG_FILE,
maxBytes=5*1024*1024, # 5MB
backupCount=3
)
file_handler.setLevel(logging.DEBUG)
# Console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# Formatter
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
logger = setup_logging()
# ==================== DATA MODELS ====================
@dataclass
class Step:
"""Represents a single test step with evidence."""
index: int
title: str = ""
expected: str = ""
actual: str = ""
notes: List[str] = field(default_factory=list)
screenshots: List[str] = field(default_factory=list) # file paths
def add_note(self, text: str) -> None:
"""Add a note to this step if not empty."""
if text and text.strip():
self.notes.append(text.strip())
logger.debug(f"Added note to step {self.index}: {text[:50]}...")
def add_screenshot(self, path: str) -> bool:
"""Add a screenshot if the file exists."""
if path and os.path.exists(path):
self.screenshots.append(path)
logger.info(f"Added screenshot to step {self.index}: {path}")
return True
logger.warning(f"Screenshot path invalid or doesn't exist: {path}")
return False
def is_empty(self) -> bool:
"""Check if step has any content."""
return not any([
self.title.strip(),
self.expected.strip(),
self.actual.strip(),
self.notes,
self.screenshots
])
# ==================== CLIPBOARD PARSER ====================
class ClipboardParser:
"""Parse clipboard content for Excel data (TSV/CSV format like Xray)."""
@staticmethod
def parse_excel_from_clipboard() -> Optional[pd.DataFrame]:
"""
Parse Excel data from clipboard (tab-separated or comma-separated).
Supports formats from Excel, Google Sheets, Jira Xray, etc.
Returns:
DataFrame if successful, None otherwise
"""
try:
# Try to get text from clipboard
clipboard_text = pyperclip.paste()
if not clipboard_text or not clipboard_text.strip():
logger.warning("Clipboard is empty")
return None
# Try tab-separated first (most common from Excel/Xray)
if '\t' in clipboard_text:
logger.info("Detected tab-separated data in clipboard")
from io import StringIO
df = pd.read_csv(StringIO(clipboard_text), sep='\t', encoding='utf-8')
return df
# Try comma-separated
elif ',' in clipboard_text:
logger.info("Detected comma-separated data in clipboard")
from io import StringIO
df = pd.read_csv(StringIO(clipboard_text), sep=',', encoding='utf-8')
return df
# Try line-by-line (simple list)
else:
lines = [line.strip() for line in clipboard_text.split('\n') if line.strip()]
if len(lines) > 0:
logger.info("Detected line-separated data in clipboard")
# Create single-column dataframe
df = pd.DataFrame({'Step': lines})
return df
logger.warning("Could not parse clipboard as tabular data")
return None
except Exception as e:
logger.error(f"Failed to parse clipboard: {e}", exc_info=True)
return None
@staticmethod
def detect_columns(df: pd.DataFrame) -> Dict[str, Optional[str]]:
"""
Auto-detect column mappings for test steps.
Args:
df: Input dataframe
Returns:
Dictionary with detected column names
"""
columns_lower = [str(c).lower().strip() for c in df.columns]
mapping = {
'title': None,
'expected': None,
'actual': None,
'description': None
}
# Detect title/step column
title_keywords = ['step', 'title', 'test', 'case', 'action', 'description', 'name', 'summary']
for idx, col_name in enumerate(columns_lower):
if any(keyword in col_name for keyword in title_keywords):
mapping['title'] = df.columns[idx]
break
# Default to first column if not found
if mapping['title'] is None and len(df.columns) > 0:
mapping['title'] = df.columns[0]
# Detect expected result column
expected_keywords = ['expected', 'result', 'outcome', 'should']
for idx, col_name in enumerate(columns_lower):
if any(keyword in col_name for keyword in expected_keywords):
mapping['expected'] = df.columns[idx]
break
# Default to second column if not found and exists
if mapping['expected'] is None and len(df.columns) > 1:
mapping['expected'] = df.columns[1]
# Detect actual result column
actual_keywords = ['actual', 'status', 'pass', 'fail']
for idx, col_name in enumerate(columns_lower):
if any(keyword in col_name for keyword in actual_keywords):
mapping['actual'] = df.columns[idx]
break
logger.info(f"Detected column mapping: {mapping}")
return mapping
# ==================== COLUMN MAPPING DIALOG ====================
class ColumnMappingDialog(QDialog):
"""Dialog for mapping Excel columns to step fields."""
def __init__(self, df: pd.DataFrame, auto_mapping: Dict[str, Optional[str]], parent=None):
super().__init__(parent)
self.df = df
self.mapping = auto_mapping.copy()
self.setWindowTitle("Map Excel Columns")
self.setMinimumWidth(500)
layout = QVBoxLayout()
# Info label
info = QLabel(
"Map your Excel columns to test step fields.\n"
"Auto-detected mappings are pre-selected."
)
info.setWordWrap(True)
layout.addWidget(info)
# Preview table
preview_group = QGroupBox("Data Preview (first 5 rows)")
preview_layout = QVBoxLayout()
self.preview_table = QTableWidget()
self.preview_table.setRowCount(min(5, len(df)))
self.preview_table.setColumnCount(len(df.columns))
self.preview_table.setHorizontalHeaderLabels([str(c) for c in df.columns])
for row_idx in range(min(5, len(df))):
for col_idx, col_name in enumerate(df.columns):
value = str(df.iloc[row_idx, col_idx])
if pd.isna(df.iloc[row_idx, col_idx]):
value = ""
item = QTableWidgetItem(value)
item.setFlags(item.flags() & ~Qt.ItemIsEditable)
self.preview_table.setItem(row_idx, col_idx, item)
self.preview_table.resizeColumnsToContents()
preview_layout.addWidget(self.preview_table)
preview_group.setLayout(preview_layout)
layout.addWidget(preview_group)
# Mapping controls
mapping_group = QGroupBox("Column Mappings")
mapping_layout = QVBoxLayout()
self.combos = {}
column_options = ["<None>"] + [str(c) for c in df.columns]
# Title mapping
title_layout = QHBoxLayout()
title_layout.addWidget(QLabel("Step Title/Description:"))
self.combos['title'] = QComboBox()
self.combos['title'].addItems(column_options)
if auto_mapping['title']:
self.combos['title'].setCurrentText(str(auto_mapping['title']))
title_layout.addWidget(self.combos['title'])
mapping_layout.addLayout(title_layout)
# Expected mapping
expected_layout = QHBoxLayout()
expected_layout.addWidget(QLabel("Expected Result:"))
self.combos['expected'] = QComboBox()
self.combos['expected'].addItems(column_options)
if auto_mapping['expected']:
self.combos['expected'].setCurrentText(str(auto_mapping['expected']))
expected_layout.addWidget(self.combos['expected'])
mapping_layout.addLayout(expected_layout)
# Actual mapping (optional)
actual_layout = QHBoxLayout()
actual_layout.addWidget(QLabel("Actual Result (optional):"))
self.combos['actual'] = QComboBox()
self.combos['actual'].addItems(column_options)
if auto_mapping.get('actual'):
self.combos['actual'].setCurrentText(str(auto_mapping['actual']))
actual_layout.addWidget(self.combos['actual'])
mapping_layout.addLayout(actual_layout)
mapping_group.setLayout(mapping_layout)
layout.addWidget(mapping_group)
# Options
options_group = QGroupBox("Import Options")
options_layout = QVBoxLayout()
self.skip_empty_checkbox = QCheckBox("Skip rows with empty title")
self.skip_empty_checkbox.setChecked(True)
options_layout.addWidget(self.skip_empty_checkbox)
options_group.setLayout(options_layout)
layout.addWidget(options_group)
# Buttons
buttons = QDialogButtonBox(
QDialogButtonBox.Ok | QDialogButtonBox.Cancel
)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
self.setLayout(layout)
def get_mapping(self) -> Dict[str, Any]:
"""Get the final column mapping and options."""
return {
'title': self.combos['title'].currentText() if self.combos['title'].currentText() != "<None>" else None,
'expected': self.combos['expected'].currentText() if self.combos['expected'].currentText() != "<None>" else None,
'actual': self.combos['actual'].currentText() if self.combos['actual'].currentText() != "<None>" else None,
'skip_empty': self.skip_empty_checkbox.isChecked()
}
# ==================== HOTKEY MANAGER ====================
class HotkeyManager(QObject):
"""Manages global keyboard shortcuts in a separate thread."""
capture_triggered = pyqtSignal()
next_triggered = pyqtSignal()
prev_triggered = pyqtSignal()
error_occurred = pyqtSignal(str)
def __init__(self,
capture_key: str = CAPTURE_HOTKEY,
next_key: str = NEXT_STEP_HOTKEY,
prev_key: str = PREV_STEP_HOTKEY):
super().__init__()
self.capture_key = capture_key
self.next_key = next_key
self.prev_key = prev_key
self._registered = False
self._thread: Optional[threading.Thread] = None
logger.info(f"HotkeyManager initialized with: capture={capture_key}, "
f"next={next_key}, prev={prev_key}")
def register(self) -> bool:
"""Register all global hotkeys in a background thread."""
if self._registered:
logger.warning("Hotkeys already registered")
return False
try:
keyboard.add_hotkey(self.capture_key, self._on_capture)
keyboard.add_hotkey(self.next_key, self._on_next)
keyboard.add_hotkey(self.prev_key, self._on_prev)
self._registered = True
# Start listener thread
self._thread = threading.Thread(target=self._listen, daemon=True)
self._thread.start()
logger.info("Global hotkeys registered successfully")
return True
except Exception as e:
error_msg = f"Failed to register hotkeys: {e}"
logger.error(error_msg, exc_info=True)
# self.error_occurred.emit(error_msg)
return False
def unregister(self) -> None:
"""Unregister all hotkeys."""
if not self._registered:
return
try:
keyboard.unhook_all_hotkeys()
self._registered = False
logger.info("Hotkeys unregistered")
except Exception as e:
logger.error(f"Error unregistering hotkeys: {e}", exc_info=True)
def _listen(self) -> None:
"""Background thread listener (blocks forever)."""
try:
keyboard.wait()
except Exception as e:
logger.error(f"Hotkey listener thread error: {e}", exc_info=True)
def _on_capture(self) -> None:
"""Capture hotkey callback."""
try:
self.capture_triggered.emit()
except Exception as e:
logger.error(f"Error in capture callback: {e}", exc_info=True)
def _on_next(self) -> None:
"""Next step hotkey callback."""
try:
self.next_triggered.emit()
except Exception as e:
logger.error(f"Error in next callback: {e}", exc_info=True)
def _on_prev(self) -> None:
"""Previous step hotkey callback."""
try:
self.prev_triggered.emit()
except Exception as e:
logger.error(f"Error in prev callback: {e}", exc_info=True)
# ==================== DOCX EXPORTER ====================
class DocxExporter:
"""Handles Word document generation from test steps."""
@staticmethod
def export(steps: List[Step], filepath: str, title: str = "Manual Test Execution") -> bool:
"""
Export steps to a Word document.
Args:
steps: List of Step objects
filepath: Output file path
title: Document title
Returns:
True if successful, False otherwise
"""
try:
doc = Document()
# Title
heading = doc.add_heading(title, level=0)
heading.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
# Metadata
doc.add_paragraph(f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
doc.add_paragraph(f"Total Steps: {len(steps)}")
doc.add_page_break()
# Steps
for step in steps:
if step.is_empty():
logger.debug(f"Skipping empty step {step.index}")
continue
# Step heading
doc.add_heading(f"Step {step.index}: {step.title or 'Untitled'}", level=1)
# Expected vs Actual table
if step.expected or step.actual:
table = doc.add_table(rows=2, cols=2)
table.style = 'Light Grid Accent 1'
# Headers
hdr_cells = table.rows[0].cells
hdr_cells[0].text = "Expected Result"
hdr_cells[1].text = "Actual Result"
# Content
content_cells = table.rows[1].cells
content_cells[0].text = step.expected or "N/A"
content_cells[1].text = step.actual or "N/A"
doc.add_paragraph() # spacing
# Notes
if step.notes:
doc.add_heading("Notes:", level=2)
for note in step.notes:
doc.add_paragraph(note, style='List Bullet')
# Screenshots
if step.screenshots:
doc.add_heading("Screenshots:", level=2)
for idx, screenshot_path in enumerate(step.screenshots, 1):
if not os.path.exists(screenshot_path):
logger.warning(f"Screenshot not found: {screenshot_path}")
doc.add_paragraph(f"[Screenshot {idx} missing: {screenshot_path}]")
continue
try:
# Add caption
doc.add_paragraph(f"Screenshot {idx}:", style='Caption')
# Add image with max width
doc.add_picture(screenshot_path, width=Inches(5.5))
# Add spacing
doc.add_paragraph()
except Exception as img_error:
logger.error(f"Failed to add image {screenshot_path}: {img_error}")
doc.add_paragraph(f"[Error loading screenshot {idx}]")
doc.add_page_break()
# Save
doc.save(filepath)
logger.info(f"Document exported successfully to {filepath}")
return True
except Exception as e:
logger.error(f"Failed to export document: {e}", exc_info=True)
return False
# ==================== MAIN WINDOW ====================
class MainWindow(QMainWindow):
"""Main application window."""
def __init__(self):
super().__init__()
self.steps: List[Step] = []
self.current_step_idx: int = 0
self.session_active: bool = False
# Managers
self.hotkey_manager = HotkeyManager()
self.hotkey_manager.capture_triggered.connect(self.on_capture_hotkey)
self.hotkey_manager.next_triggered.connect(self.on_next_hotkey)
self.hotkey_manager.prev_triggered.connect(self.on_prev_hotkey)
self.hotkey_manager.error_occurred.connect(self.show_error)
self._init_ui()
self._init_default_steps()
self._update_step_view()
# Register hotkeys
if not self.hotkey_manager.register():
QMessageBox.warning(
self,
"Hotkey Registration Failed",
"Failed to register global hotkeys. The app may require administrator privileges."
)
logger.info("MainWindow initialized")
def _init_ui(self) -> None:
"""Initialize the user interface."""
self.setWindowTitle("Test Evidence Helper")
self.setMinimumSize(700, 550)
# Central widget
central = QWidget()
main_layout = QVBoxLayout()
main_layout.setSpacing(10)
# Document name section
doc_group = QGroupBox("Document Settings")
doc_layout = QHBoxLayout()
doc_layout.addWidget(QLabel("Document Name:"))
self.doc_name_input = QLineEdit(self._default_doc_name())
self.doc_name_input.setPlaceholderText("Enter document name...")
doc_layout.addWidget(self.doc_name_input)
doc_group.setLayout(doc_layout)
main_layout.addWidget(doc_group)
# Step navigation section
step_group = QGroupBox("Current Step")
step_layout = QVBoxLayout()
self.step_label = QLabel("Step 1/1")
font = QFont()
font.setPointSize(12)
font.setBold(True)
self.step_label.setFont(font)
step_layout.addWidget(self.step_label)
self.step_title_label = QLabel("Title:")
self.step_expected_label = QLabel("Expected:")
step_layout.addWidget(self.step_title_label)
step_layout.addWidget(self.step_expected_label)
step_group.setLayout(step_layout)
main_layout.addWidget(step_group)
# Actual result / notes editor
editor_group = QGroupBox("Actual Result / Notes")
editor_layout = QVBoxLayout()
self.actual_edit = QTextEdit()
self.actual_edit.setPlaceholderText(
"Enter actual test result or notes here...\n"
"This will be saved for the current step.\n\n"
"You can also paste text/tables from clipboard using Ctrl+Alt+S"
)
editor_layout.addWidget(self.actual_edit)
editor_group.setLayout(editor_layout)
main_layout.addWidget(editor_group)
# Buttons
btn_layout_1 = QHBoxLayout()
self.start_btn = QPushButton("▶ Start Session")
self.start_btn.clicked.connect(self.on_start_session)
self.start_btn.setStyleSheet("background-color: #4CAF50; color: white; font-weight: bold;")
btn_layout_1.addWidget(self.start_btn)
self.load_excel_btn = QPushButton("📁 Load from Excel File")
self.load_excel_btn.clicked.connect(self.on_load_excel_file)
btn_layout_1.addWidget(self.load_excel_btn)
self.paste_excel_btn = QPushButton("📋 Paste from Clipboard")
self.paste_excel_btn.clicked.connect(self.on_paste_excel_from_clipboard)
self.paste_excel_btn.setStyleSheet("background-color: #FF9800; color: white; font-weight: bold;")
btn_layout_1.addWidget(self.paste_excel_btn)
main_layout.addLayout(btn_layout_1)
btn_layout_2 = QHBoxLayout()
self.prev_btn = QPushButton("◀ Previous")
self.prev_btn.clicked.connect(self.on_prev_step)
btn_layout_2.addWidget(self.prev_btn)
self.next_btn = QPushButton("Next ▶")
self.next_btn.clicked.connect(self.on_next_step)
btn_layout_2.addWidget(self.next_btn)
self.export_btn = QPushButton("💾 Export to DOCX")
self.export_btn.clicked.connect(self.on_export_docx)
self.export_btn.setStyleSheet("background-color: #2196F3; color: white; font-weight: bold;")
btn_layout_2.addWidget(self.export_btn)
main_layout.addLayout(btn_layout_2)
# Hotkey info
hotkey_info = QLabel(
f"<b>Global Hotkeys:</b> Capture (Screenshot/Text/Table): <i>{CAPTURE_HOTKEY}</i> | "
f"Prev: <i>{PREV_STEP_HOTKEY}</i> | Next: <i>{NEXT_STEP_HOTKEY}</i>"
)
hotkey_info.setWordWrap(True)
hotkey_info.setStyleSheet("background-color: #FFF9C4; padding: 5px; border-radius: 3px;")
main_layout.addWidget(hotkey_info)
central.setLayout(main_layout)
self.setCentralWidget(central)
# Status bar
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
self.update_status("Ready - Paste Excel data with '📋 Paste from Clipboard' or load file")
# Auto-save on focus change
self.actual_edit.focusOutEvent = self._on_editor_focus_out
def _default_doc_name(self) -> str:
"""Generate default document name with timestamp."""
return f"TestRun_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
def _init_default_steps(self) -> None:
"""Initialize with one empty step."""
self.steps = [Step(index=1)]
self.current_step_idx = 0
logger.debug("Initialized with default empty step")
def update_status(self, message: str, timeout: int = 0) -> None:
"""Update status bar message."""
self.status_bar.showMessage(message, timeout)
logger.debug(f"Status: {message}")
def show_error(self, message: str) -> None:
"""Show error message dialog."""
QMessageBox.critical(self, "Error", message)
logger.error(f"Error shown to user: {message}")
def show_info(self, title: str, message: str) -> None:
"""Show info message dialog."""
QMessageBox.information(self, title, message)
# ==================== SESSION MANAGEMENT ====================
def on_start_session(self) -> None:
"""Start a test session."""
try:
self.session_active = True
self.update_status("Session running - Hotkeys active")
self.start_btn.setEnabled(False)
self.start_btn.setText("✓ Session Active")
logger.info("Test session started")
except Exception as e:
logger.error(f"Error starting session: {e}", exc_info=True)
self.show_error(f"Failed to start session: {e}")
# ==================== STEP NAVIGATION ====================
def _save_current_step_actual(self) -> None:
"""Save the current actual/notes text to current step."""
try:
if 0 <= self.current_step_idx < len(self.steps):
self.steps[self.current_step_idx].actual = self.actual_edit.toPlainText()
logger.debug(f"Saved actual text for step {self.current_step_idx + 1}")
except Exception as e:
logger.error(f"Error saving step actual: {e}", exc_info=True)
def on_next_step(self) -> None:
"""Navigate to next step (create if at end)."""
try:
self._save_current_step_actual()
if self.current_step_idx < len(self.steps) - 1:
self.current_step_idx += 1
else:
# Create new step
new_step = Step(index=len(self.steps) + 1)
self.steps.append(new_step)
self.current_step_idx = len(self.steps) - 1
logger.info(f"Created new step {new_step.index}")
self._update_step_view()
except Exception as e:
logger.error(f"Error navigating to next step: {e}", exc_info=True)
self.show_error(f"Failed to navigate: {e}")
def on_prev_step(self) -> None:
"""Navigate to previous step."""
try:
self._save_current_step_actual()
if self.current_step_idx > 0:
self.current_step_idx -= 1
self._update_step_view()
else:
self.update_status("Already at first step", 2000)
except Exception as e:
logger.error(f"Error navigating to previous step: {e}", exc_info=True)
self.show_error(f"Failed to navigate: {e}")
def _update_step_view(self) -> None:
"""Update UI to reflect current step."""
try:
if not self.steps:
return
step = self.steps[self.current_step_idx]
self.step_label.setText(f"Step {step.index}/{len(self.steps)}")
self.step_title_label.setText(f"<b>Title:</b> {step.title or '<i>No title</i>'}")
self.step_expected_label.setText(f"<b>Expected:</b> {step.expected or '<i>Not specified</i>'}")
self.actual_edit.setPlainText(step.actual)
status = (f"Step {step.index} | "
f"Screenshots: {len(step.screenshots)} | "
f"Notes: {len(step.notes)}")
self.update_status(status)
logger.debug(f"Updated view for step {step.index}")
except Exception as e:
logger.error(f"Error updating step view: {e}", exc_info=True)
def _on_editor_focus_out(self, event) -> None:
"""Handle focus leaving the editor."""
try:
self._save_current_step_actual()
except Exception as e:
logger.error(f"Error in focus out handler: {e}", exc_info=True)
finally:
QTextEdit.focusOutEvent(self.actual_edit, event)
# ==================== HOTKEY HANDLERS ====================
def on_capture_hotkey(self) -> None:
"""Handle capture hotkey press."""
if not self.session_active:
return
try:
self.capture_clipboard_into_step()
except Exception as e:
logger.error(f"Error in capture hotkey handler: {e}", exc_info=True)
self.show_error(f"Capture failed: {e}")
def on_next_hotkey(self) -> None:
"""Handle next step hotkey."""
if not self.session_active:
return
try:
self.on_next_step()
except Exception as e:
logger.error(f"Error in next hotkey handler: {e}", exc_info=True)
def on_prev_hotkey(self) -> None:
"""Handle previous step hotkey."""
if not self.session_active:
return
try:
self.on_prev_step()
except Exception as e:
logger.error(f"Error in prev hotkey handler: {e}", exc_info=True)
# ==================== CLIPBOARD CAPTURE ====================
def capture_clipboard_into_step(self) -> None:
"""Capture clipboard content (image, text, or table) into current step."""
if not self.steps:
self.update_status("No steps available", 2000)
return
step = self.steps[self.current_step_idx]
# Try image first
try:
img = ImageGrab.grabclipboard()
except Exception as e:
logger.warning(f"Failed to grab clipboard image: {e}")
img = None
if img is not None:
# Save screenshot
try:
tmp_dir = tempfile.gettempdir()
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S%f')
filename = f"testhelper_step{step.index}_{timestamp}.png"
filepath = os.path.join(tmp_dir, filename)
# Validate image
if not isinstance(img, Image.Image):
logger.error(f"Invalid image type: {type(img)}")
self.update_status("Invalid image in clipboard", 3000)
return
img.save(filepath, "PNG")
if step.add_screenshot(filepath):
self.update_status(f"✓ Screenshot captured for step {step.index}", 3000)
self._update_step_view()
else:
self.update_status("Failed to add screenshot", 3000)
return
except Exception as e:
logger.error(f"Failed to save screenshot: {e}", exc_info=True)
self.show_error(f"Screenshot save failed: {e}")
return
# Try Excel/table data (TSV/CSV)
try:
df = ClipboardParser.parse_excel_from_clipboard()
if df is not None and not df.empty:
# Format as table text
table_text = df.to_string(index=False)
step.add_note(f"[Table Data]\n{table_text}")
# Also append to actual field
existing = step.actual
if existing:
step.actual = f"{existing}\n\n[Pasted Table]\n{table_text}"
else:
step.actual = f"[Pasted Table]\n{table_text}"
self._update_step_view()
self.update_status(f"✓ Table data captured ({len(df)} rows) for step {step.index}", 3000)
return
except Exception as e:
logger.debug(f"Not Excel/table data: {e}")
# Try plain text
try:
clipboard = QApplication.clipboard()
text = clipboard.text()
if text and text.strip():
step.add_note(text)
# Also append to actual field
existing = step.actual
if existing:
step.actual = f"{existing}\n{text}"
else:
step.actual = text
self._update_step_view()
self.update_status(f"✓ Text captured for step {step.index}", 3000)
else:
self.update_status("Clipboard is empty", 2000)
except Exception as e:
logger.error(f"Failed to read clipboard text: {e}", exc_info=True)
self.show_error(f"Clipboard read failed: {e}")
# ==================== EXCEL IMPORT FROM FILE ====================
def on_load_excel_file(self) -> None:
"""Load test steps from an Excel file."""
try:
filepath, _ = QFileDialog.getOpenFileName(
self,
"Select Excel File",
"",
"Excel Files (*.xlsx *.xls);;All Files (*)"
)
if not filepath:
return
logger.info(f"Loading steps from Excel: {filepath}")
# Read Excel
try:
df = pd.read_excel(filepath)
except Exception as e:
logger.error(f"Failed to read Excel: {e}", exc_info=True)
self.show_error(f"Failed to read Excel file:\n{e}")
return
if df.empty:
self.show_error("Excel file is empty")
return
# Process with column mapping
self._process_excel_dataframe(df)
except Exception as e:
logger.error(f"Error loading Excel: {e}", exc_info=True)
self.show_error(f"Failed to load Excel:\n{e}")
# ==================== EXCEL PASTE FROM CLIPBOARD ====================
def on_paste_excel_from_clipboard(self) -> None:
"""Parse Excel data from clipboard (Jira Xray style)."""
try:
logger.info("Attempting to parse Excel from clipboard")
df = ClipboardParser.parse_excel_from_clipboard()
if df is None:
self.show_error(
"Could not parse clipboard as Excel data.\n\n"
"Please copy data from Excel, Google Sheets, or Jira Xray:\n"
"1. Select cells in Excel/Sheets/Xray\n"
"2. Copy (Ctrl+C)\n"
"3. Click 'Paste from Clipboard' button"
)
return
if df.empty:
self.show_error("Clipboard contains empty data")
return
logger.info(f"Parsed {len(df)} rows with {len(df.columns)} columns from clipboard")
# Process with column mapping
self._process_excel_dataframe(df)
except Exception as e:
logger.error(f"Error pasting Excel from clipboard: {e}", exc_info=True)
self.show_error(f"Failed to parse clipboard:\n{e}")
def _process_excel_dataframe(self, df: pd.DataFrame) -> None:
"""Process Excel dataframe with column mapping dialog."""
try:
# Auto-detect columns
auto_mapping = ClipboardParser.detect_columns(df)
# Show mapping dialog