|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | import os
|
16 |
| -from typing import Any, Dict, Optional, Tuple, Union |
| 16 | +from typing import Any, Dict, List, Optional, Tuple, Union |
17 | 17 |
|
18 | 18 | from appium.common.helper import encode_file_to_base64
|
19 | 19 | from appium.webdriver.extensions.flutter_integration.flutter_finder import FlutterFinder
|
@@ -172,6 +172,104 @@ def activate_injected_image(self, image_id: str) -> None:
|
172 | 172 | """
|
173 | 173 | self.execute_flutter_command('activateInjectedImage', {'imageId': image_id})
|
174 | 174 |
|
| 175 | + def get_render_tree( |
| 176 | + self, |
| 177 | + widget_type: Optional[str] = None, |
| 178 | + key: Optional[str] = None, |
| 179 | + text: Optional[str] = None, |
| 180 | + ) -> List[Optional[Dict]]: |
| 181 | + """ |
| 182 | + Returns the render tree of the root widget. |
| 183 | +
|
| 184 | + Args: |
| 185 | + widget_type (Optional[str]): The type of the widget to primary filter by. |
| 186 | + key (Optional[str]): The key of the widget to filter by. |
| 187 | + text (Optional[str]): The text of the widget to filter by. |
| 188 | +
|
| 189 | + Returns: |
| 190 | + List[Optional[Dict]]: A list of dictionaries or None values representing the render tree. |
| 191 | +
|
| 192 | + The result is a nested list of dictionaries representing each widget and its properties, |
| 193 | + such as type, key, size, attribute, state, visual information, and hierarchy. |
| 194 | +
|
| 195 | + The example widget includes the following code, which is rendered as part of the widget tree: |
| 196 | + ```dart |
| 197 | + Semantics( |
| 198 | + key: const Key('add_activity_semantics'), |
| 199 | + label: 'add_activity_button', |
| 200 | + button: true, |
| 201 | + child: FloatingActionButton.small( |
| 202 | + key: const Key('add_activity_button'), |
| 203 | + tooltip: 'add_activity_button', |
| 204 | + heroTag: 'add', |
| 205 | + backgroundColor: const Color(0xFF2E2E3A), |
| 206 | + onPressed: null, |
| 207 | + child: Icon( |
| 208 | + Icons.add, |
| 209 | + size: 16, |
| 210 | + color: Colors.amber.shade200.withOpacity(0.5), |
| 211 | + semanticLabel: 'Add icon', |
| 212 | + ), |
| 213 | + ), |
| 214 | + ), |
| 215 | + ``` |
| 216 | + Example execute command: |
| 217 | + >>> flutter_command = FlutterCommand(driver) # noqa |
| 218 | + >>> flutter_command.get_render_tree(widget_type='Semantics', key='add_activity_semantics') |
| 219 | + output >> [ |
| 220 | + { |
| 221 | + "type": "Semantics", |
| 222 | + "elementType": "SingleChildRenderObjectElement", |
| 223 | + "description": "Semantics-[<'add_activity_semantics'>]", |
| 224 | + "depth": 0, |
| 225 | + "key": "[<'add_activity_semantics'>]", |
| 226 | + "attributes": { |
| 227 | + "semanticsLabel": "add_activity_button" |
| 228 | + }, |
| 229 | + "visual": {}, |
| 230 | + "state": {}, |
| 231 | + "rect": { |
| 232 | + "x": 0, |
| 233 | + "y": 0, |
| 234 | + "width": 48, |
| 235 | + "height": 48 |
| 236 | + }, |
| 237 | + "children": [ |
| 238 | + { |
| 239 | + "type": "FloatingActionButton", |
| 240 | + "elementType": "StatelessElement", |
| 241 | + "description": "FloatingActionButton-[<'add_activity_button'>]", |
| 242 | + "depth": 1, |
| 243 | + "key": "[<'add_activity_button'>]", |
| 244 | + "attributes": {}, |
| 245 | + "visual": {}, |
| 246 | + "state": {}, |
| 247 | + "rect": { |
| 248 | + "x": 0, |
| 249 | + "y": 0, |
| 250 | + "width": 48, |
| 251 | + "height": 48 |
| 252 | + }, |
| 253 | + "children": [ |
| 254 | + {...}, |
| 255 | + "children": [...] |
| 256 | + } |
| 257 | + ] |
| 258 | + } |
| 259 | + ] |
| 260 | + } |
| 261 | + ] |
| 262 | + """ |
| 263 | + opts = {} |
| 264 | + if widget_type is not None: |
| 265 | + opts['widgetType'] = widget_type |
| 266 | + if key is not None: |
| 267 | + opts['key'] = key |
| 268 | + if text is not None: |
| 269 | + opts['text'] = text |
| 270 | + |
| 271 | + return self.execute_flutter_command('renderTree', opts) |
| 272 | + |
175 | 273 | def execute_flutter_command(self, scriptName: str, params: dict) -> Any:
|
176 | 274 | """
|
177 | 275 | Executes a Flutter command by sending a script and parameters to the flutter integration driver.
|
|
0 commit comments