Skip to content

feat: add method for interacting with the Flutter integration driver #1123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 99 additions & 1 deletion appium/webdriver/extensions/flutter_integration/flutter_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import os
from typing import Any, Dict, Optional, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple, Union

from appium.common.helper import encode_file_to_base64
from appium.webdriver.extensions.flutter_integration.flutter_finder import FlutterFinder
Expand Down Expand Up @@ -172,6 +172,104 @@ def activate_injected_image(self, image_id: str) -> None:
"""
self.execute_flutter_command('activateInjectedImage', {'imageId': image_id})

def get_render_tree(
self,
widget_type: Optional[str] = None,
key: Optional[str] = None,
text: Optional[str] = None,
) -> List[Optional[Dict]]:
"""
Returns the render tree of the root widget.

Args:
widget_type (Optional[str]): The type of the widget to primary filter by.
key (Optional[str]): The key of the widget to filter by.
text (Optional[str]): The text of the widget to filter by.

Returns:
List[Optional[Dict]]: A list of dictionaries or None values representing the render tree.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might probably be useful to provide some examples of the output

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the main comment with the output examples

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure the recent changes have been committed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean that the output examples should be included in the function's docstring?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, one or two would be enough for the returned value docstring

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added example for the returned value in the docstring


The result is a nested list of dictionaries representing each widget and its properties,
such as type, key, size, attribute, state, visual information, and hierarchy.

The example widget includes the following code, which is rendered as part of the widget tree:
```dart
Semantics(
key: const Key('add_activity_semantics'),
label: 'add_activity_button',
button: true,
child: FloatingActionButton.small(
key: const Key('add_activity_button'),
tooltip: 'add_activity_button',
heroTag: 'add',
backgroundColor: const Color(0xFF2E2E3A),
onPressed: null,
child: Icon(
Icons.add,
size: 16,
color: Colors.amber.shade200.withOpacity(0.5),
semanticLabel: 'Add icon',
),
),
),
```
Example execute command:
>>> flutter_command = FlutterCommand(driver) # noqa
>>> flutter_command.get_render_tree(widget_type='Semantics', key='add_activity_semantics')
output >> [
{
"type": "Semantics",
"elementType": "SingleChildRenderObjectElement",
"description": "Semantics-[<'add_activity_semantics'>]",
"depth": 0,
"key": "[<'add_activity_semantics'>]",
"attributes": {
"semanticsLabel": "add_activity_button"
},
"visual": {},
"state": {},
"rect": {
"x": 0,
"y": 0,
"width": 48,
"height": 48
},
"children": [
{
"type": "FloatingActionButton",
"elementType": "StatelessElement",
"description": "FloatingActionButton-[<'add_activity_button'>]",
"depth": 1,
"key": "[<'add_activity_button'>]",
"attributes": {},
"visual": {},
"state": {},
"rect": {
"x": 0,
"y": 0,
"width": 48,
"height": 48
},
"children": [
{...},
"children": [...]
}
]
}
]
}
]
"""
opts = {}
if widget_type is not None:
opts['widgetType'] = widget_type
if key is not None:
opts['key'] = key
if text is not None:
opts['text'] = text

return self.execute_flutter_command('renderTree', opts)

def execute_flutter_command(self, scriptName: str, params: dict) -> Any:
"""
Executes a Flutter command by sending a script and parameters to the flutter integration driver.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env python

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json

import httpretty

from appium.webdriver.extensions.flutter_integration.flutter_commands import (
FlutterCommand,
)
from test.unit.helper.test_helper import (
appium_command,
flutter_w3c_driver,
get_httpretty_request_body,
)


class TestFlutterRenderTree:
@httpretty.activate
def test_get_render_tree_with_all_filters(self):
expected_body = [{'<partial_tree>': 'LoginButton'}]
httpretty.register_uri(
httpretty.POST,
appium_command('/session/1234567890/execute/sync'),
body=json.dumps({'value': expected_body}),
)

driver = flutter_w3c_driver()
flutter = FlutterCommand(driver)

result = flutter.get_render_tree(widget_type='ElevatedButton', key='LoginButton', text='Login')

request_body = get_httpretty_request_body(httpretty.last_request())
assert request_body['script'] == 'flutter: renderTree'
assert request_body['args'] == [
{
'widgetType': 'ElevatedButton',
'key': 'LoginButton',
'text': 'Login',
}
]

assert result == expected_body

@httpretty.activate
def test_get_render_tree_with_partial_filters(self):
expected_body = [{'<partial_tree>': 'LoginScreen'}]

httpretty.register_uri(
httpretty.POST,
appium_command('/session/1234567890/execute/sync'),
body=json.dumps({'value': expected_body}),
)

driver = flutter_w3c_driver()
flutter = FlutterCommand(driver)

result = flutter.get_render_tree(widget_type='LoginScreen')

request_body = get_httpretty_request_body(httpretty.last_request())
assert request_body['script'] == 'flutter: renderTree'
assert request_body['args'] == [{'widgetType': 'LoginScreen'}]

assert result == expected_body

@httpretty.activate
def test_get_render_tree_with_no_filters(self):
expected_body = [{'<full_tree>': 'RootWidget'}]
httpretty.register_uri(
httpretty.POST,
appium_command('/session/1234567890/execute/sync'),
body=json.dumps({'value': expected_body}),
)

driver = flutter_w3c_driver()
flutter = FlutterCommand(driver)

result = flutter.get_render_tree()

request_body = get_httpretty_request_body(httpretty.last_request())
assert request_body['script'] == 'flutter: renderTree'
assert request_body['args'] == [{}]

assert result == expected_body