-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinteraction_logger.py
More file actions
95 lines (69 loc) · 2.86 KB
/
interaction_logger.py
File metadata and controls
95 lines (69 loc) · 2.86 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
import queue
import time
import settings
import os
interaction_queue = queue.Queue()
def get_selector_keyword(selector):
if selector and (selector.startswith('/') or selector.startswith('(')):
return 'xpath'
return 'xpath'
def get_test_file():
if not settings.test_file:
return None
test_dir = os.path.dirname(settings.test_file)
os.makedirs(test_dir, exist_ok=True)
return settings.test_file
def log_interaction(interaction):
text = define_interaction(interaction)
test_file = get_test_file()
if test_file and text:
try:
with open(test_file, "a", encoding="utf-8") as f:
f.write(f"{text}\n")
except Exception as e:
print(f"ERROR: Could not write interaction to {test_file}: {e}")
def define_interaction(interaction):
text = None
interaction_type = interaction.get("type")
selector = interaction.get("xpath")
ignored_types = ["initialState", "zoomChange", "go"]
if interaction_type in ignored_types:
return None
if interaction_type == "click" and selector:
keyword = get_selector_keyword(selector)
text = f'\tAnd I click on element with {keyword} "{selector}"'
elif interaction_type == "input" and selector:
value = interaction.get("value", "")
keyword = get_selector_keyword(selector)
text = f'\tAnd I type "{value}" into field with {keyword} "{selector}"'
elif interaction_type == "keypress" and interaction.get("key") == "Enter" and selector:
keyword = get_selector_keyword(selector)
text = f'\tAnd I press the "Enter" key on element with {keyword} "{selector}"'
elif interaction_type == "back":
text = f'\tAnd I go back'
elif interaction_type == "forward":
text = f'\tAnd I go forward'
elif interaction_type == "viewportChange":
viewport_data = interaction.get("viewport")
if viewport_data:
width = viewport_data.get("width")
height = viewport_data.get("height")
if width is not None and height is not None:
text = f'\tAnd I set the viewport to {width}x{height}'
else:
print("WARNING: viewportChange action missing width or height.")
else:
print("WARNING: viewportChange action missing viewport data.")
elif interaction_type:
print(f"WARNING: Unhandled interaction type: {interaction_type}")
return text
def main():
while True:
try:
interaction = interaction_queue.get(timeout=1)
print(f"INFO: Received interaction {interaction.get('type')}")
log_interaction(interaction)
except queue.Empty:
continue
except Exception as e:
print(f"ERROR: Error in logger loop: {e}")