Debian Codegraph @

test/common/dialoglib.py

raw
1 # Copyright (C) 2025 Red Hat, Inc.
2 #
3 # SPDX-License-Identifier: LGPL-2.1-or-later
4
5 # DIALOG HELPERS
6 #
7 # This is a class of dialog helpers. The idea is to instantiate one
8 # for each dialog that wants it, maybe like so:
9 #
10 #
11 # def testBasic(self):
12 # b = self.browser
13 #
14 # b.click("#open-my-dialog")
15 # b.wait_visible("#my-dialog")
16 #
17 # d = DialogHelpers(b, "#my-dialog")
18 # d.set_TextInput("name.first"), "Jim")
19 # d.set_TextInput("name.last"), "Kirk")
20 #
21 # You need to pass the CSS selector prefix of the <Modal> component to
22 # DialogHelpers. This makes it possible to work with multiple stacked
23 # dialogs.
24 #
25 # There are helpers for constructing CSS selectors for dialog elements
26 # such as input fields, helper texts, and the buttons. There are
27 # helpers for actually interacting with those elements as well.
28 #
29 # The fundamental function is this:
30 #
31 # - d.id(path, tag)
32 #
33 # This constructs the CSS selector for the DOM element that has been
34 # instantiated with an OUIA component ID computed by
35 # "value.ouia_id(tag)", see the documentation for the JavaScript
36 # dialog framework. The "path" parameter describes the way the value
37 # handle was derived in JavaScript: For top-level ones created by
38 # "dlg.field(name)" it is just "name". For sub-values created by
39 # "handle.sub(name_or_index)" it is the path of "handle" followed by
40 # ".", followed by "name_or_index". Nothing fancy.
41 #
42 # For example,
43 #
44 # <input
45 # data-ouia-component-id={dlg.field("name").sub("first").ouia_id("input")}
46 # ...
47 # />
48 #
49 # can be selected in a test like this:
50 #
51 # b.set_input_text(d.id("name.first", "input"), "Jim")
52 #
53 # There are some functions that encode the guidelines for choosing
54 # tags. These should be used most of the time:
55 #
56 # - d.field(path)
57 #
58 # The main input element of a field. Same as d.id(path, "field").
59 #
60 # - d.helper_text(path)
61 #
62 # The helper text for a field. Same as d.id(path, "helper-text").
63 #
64 # The helpers for interacting with porcelain elements are called
65 # set_TextInput, wait_TextInput, set_RadioSelect, etc. They are
66 # hopefully easy to figure out.
67 #
68 # For example,
69 #
70 # <DialogTextInput label="First" field={dlg.field("name").sub("first")} />
71 #
72 # can be controlled like this by a test:
73 #
74 # d.set_TextInput("name.first", "Jim")
75 #
76
77 import testlib
78
79
80 class DialogHelpers:
81 def __init__(self, b: testlib.Browser, dialogSelector: str):
82 self.browser = b
83 self.dialogSelector = dialogSelector
84
85 def ouia(self, component_id: str) -> str:
86 return f"{self.dialogSelector} [data-ouia-component-id='{component_id}']"
87
88 def id(self, path: str, tag: str) -> str:
89 return self.ouia(f"dialog-{tag}-{path}")
90
91 def field(self, path: str) -> str:
92 return self.id(path, "field")
93
94 def helper_text(self, path: str) -> str:
95 return self.id(path, "helper-text")
96
97 def error(self) -> str:
98 return self.ouia("dialog-error-message")
99
100 def apply_button(self) -> str:
101 return self.ouia("dialog-apply")
102
103 def cancel_button(self) -> str:
104 return self.ouia("dialog-cancel")
105
106 # TextInput
107
108 def get_TextInput(self, path: str) -> str:
109 return self.browser.val(self.field(path))
110
111 def wait_TextInput(self, path: str, val: str):
112 self.browser.wait_val(self.field(path), val)
113
114 def set_TextInput(self, path: str, val: str) -> None:
115 self.browser.set_input_text(self.field(path), val)
116
117 # Checkbox
118
119 def get_Checkbox(self, path: str) -> bool:
120 return self.browser.get_checked(self.field(path))
121
122 def wait_Checkbox(self, path: str, val: bool) -> None:
123 # XXX - implement Browser.wait_checked and use it here
124 x = ":checked" if val else ":not(:checked)"
125 self.browser.wait_visible(self.field(path) + x)
126
127 def set_Checkbox(self, path: str, val: bool) -> None:
128 self.browser.set_checked(self.field(path), val)
129
130 # RadioSelect
131
132 def get_RadioSelect(self, path: str) -> str:
133 return self.browser.attr(self.field(path), "data-value")
134
135 def wait_RadioSelect(self, path: str, val: str) -> None:
136 self.browser.wait_attr(self.field(path), "data-value", val)
137
138 def set_RadioSelect(self, path: str, val: str) -> None:
139 self.browser.click(self.id(path, val))
140
141 # DropdownSelect
142
143 def get_DropdownSelect(self, path: str) -> str:
144 return self.browser.val(self.field(path))
145
146 def wait_DropdownSelect(self, path: str, val: str) -> None:
147 self.browser.wait_val(self.field(path), val)
148
149 def set_DropdownSelect(self, path: str, val: str) -> None:
150 self.browser.select_from_dropdown(self.field(path), val)
151
152 # FileChooserInput
153
154 def get_FileChooserInput(self, path: str) -> str:
155 return self.browser.val(self.field(path) + " input")
156
157 def wait_FileChooserInput(self, path: str, val: str):
158 self.browser.wait_val(self.field(path) + " input", val)
159
160 def set_FileChooserInput(self, path: str, val: str) -> None:
161 self.browser.set_input_text(self.field(path) + " input", val)
162
163
164 class FileChooserHelpers(DialogHelpers):
165
166 def file(self, name: str) -> str:
167 return f"{self.dialogSelector} .file-chooser-listing-body tr[data-name='{name}']"
168
169 def sidebar(self, name: str) -> str:
170 return f"{self.dialogSelector} .file-chooser-sidebar tr:contains('{name}')"
171
172 def wait_path(self, path: str) -> None:
173 self.browser.wait_js_func(
174 "((sel, path) => ph_select(sel).map(e => e.textContent).join('/') === path)",
175 f"{self.dialogSelector} .file-chooser-listing-breadcrumbs li a", path)
176
177 def set_path(self, path: str) -> None:
178 for p in path.split("/"):
179 if p == "":
180 self.browser.click(self.sidebar("Filesystem"))
181 else:
182 self.browser.mouse(self.file(p), "dblclick")