Skip to content

Commit

Permalink
feat: renamed and fixed to direct function call for dragAndDropWithCo…
Browse files Browse the repository at this point in the history
…mmandExtension, and moved documentation to the top level
  • Loading branch information
Alpaca00 committed Nov 4, 2024
1 parent 48ff33b commit b34e23c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 87 deletions.
56 changes: 52 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,11 @@ Please replace them properly with your client.
| :question: | :ok: | `driver.execute('flutter:waitForFirstFrame')` | Widget |
| - | :ok: | (Ruby) `driver.execute_script 'flutter:connectObservatoryWsUrl'` | Flutter Driver |
| - | :ok: | (Ruby) `driver.execute_script 'flutter:launchApp', 'bundleId', {arguments: ['arg1'], environment: {ENV1: 'env'}}` | Flutter Driver |
| dragAndDrop | :ok: | (Python) `driver.execute_script('flutter:commandExtension', payload)` | Command Extension |
| dragAndDropWithCommandExtension | :ok: | (Python) `driver.execute_script('flutter:dragAndDropWithCommandExtension', payload)` | Command Extension |

**NOTE**
>`flutter:launchApp` launches an app via instrument service. `mobile:activateApp` and `driver.activate_app` are via XCTest API. They are a bit different.
- `flutter:launchApp` launches an app via instrument service. `mobile:activateApp` and `driver.activate_app` are via XCTest API. They are a bit different.

- `flutter:commandExtension` is a command extension to flutter driver, which uses [CommandExtension-class](https://api.flutter.dev/flutter/flutter_driver_extension/CommandExtension-class.html) in the `ext.flutter.driver`, how to use it is [here](example/dart/README.md).

### `isolate` handling
#### Change the flutter engine attache to
Expand Down Expand Up @@ -351,6 +349,56 @@ These Appium commands can work across context
- `getClipboard`
- `setClipboard`

## Command Extension (Flutter Driver)

This is a command extension for Flutter Driver, utilizing the [CommandExtension-class](https://api.flutter.dev/flutter/flutter_driver_extension/CommandExtension-class.html) within `ext.flutter.driver`

Available commands:

- `dragAndDropWithCommandExtension` – performs a drag-and-drop action on the screen by specifying the start and end coordinates and the action duration.

### How to use

Copy the [extended_commands.dart](extended_commands.dart) file to the `lib` folder of your Flutter project.

The entry point must include the `List<CommandExtension>?` commands argument in either `main.dart` or `test_main.dart` to properly handle the command extension.


```dart
import 'extended_commands.dart';
void main() {
enableFlutterDriverExtension(
commands: [DragCommandExtension()]);
runApp(const MyApp());
}
```

#### Simple example using `dragAndDropWithCommandExtension` command in Python

```python
# python
coord_item_1 = driver.execute_script("flutter:getCenter", item_1)
coord_item_2 = driver.execute_script("flutter:getCenter", item_2)
start_x = coord_item_1["dx"]
start_y = coord_item_1["dy"]
end_y = coord_item_2["dy"]

payload = {
"startX": start_x,
"startY": start_y,
"endX": "0",
"endY": end_y,
"duration": "15000" # minimum 15000ms needed to drag n drop
}

driver.execute_script("flutter:dragAndDropWithCommandExtension", payload)
```

For debugging or testing in other programming languages, you can use the APK available in this [repository](https://github.com/Alpaca00/command-driven-list) or build an IPA.


## Troubleshooting

- Input texts https://github.com/appium/appium-flutter-driver/issues/417
Expand Down
36 changes: 4 additions & 32 deletions driver/lib/commands/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export const execute = async function(
return await setFrameSync(this, args[0], args[1]);
case `clickElement`:
return await clickElement(this, args[0], args[1]);
case `commandExtension`:
return await commandExtension(this, args[0]);
case `dragAndDropWithCommandExtension`:
return await dragAndDropWithCommandExtension(this, args[0]);
default:
throw new Error(`Command not support: "${rawCommand}"`);
}
Expand Down Expand Up @@ -221,35 +221,7 @@ const clickElement = async (self:FlutterDriver, elementBase64: string, opts) =>
});
};

const commandExtension = async (
self: FlutterDriver,
commandPayload: { command: string; [key: string]: any }
) => {
const { command, ...params } = commandPayload;
const commandMapping: {
[key: string]: (self: FlutterDriver, params: any) => Promise<any>
} = {
'dragAndDrop': dragAndDropCommand,
'commandExtension': async (self, params) => {
const innerCommand = Object.keys(params)[0];
const innerParams = params[innerCommand];
if (commandMapping[innerCommand]) {
return await commandMapping[innerCommand](self, innerParams);
} else {
throw new Error(`Inner command not supported: '${innerCommand}'`);
}
},
};

const commandHandler = commandMapping[command];
if (commandHandler) {
return await commandHandler(self, params);
} else {
throw new Error(`Command not supported`);
}
};

const dragAndDropCommand = async (
const dragAndDropWithCommandExtension = async (
self: FlutterDriver,
params: {
startX: string;
Expand All @@ -261,7 +233,7 @@ const dragAndDropCommand = async (
) => {
const { startX, startY, endX, endY, duration } = params;
const commandPayload = {
command: 'dragAndDrop',
command: 'dragAndDropWithCommandExtension',
startX,
startY,
endX,
Expand Down
49 changes: 0 additions & 49 deletions example/dart/README.md

This file was deleted.

4 changes: 2 additions & 2 deletions example/dart/extended_commands.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DragCommand extends Command {
DragCommand(this.startX, this.startY, this.endX, this.endY, this.duration);

@override
String get kind => 'dragAndDrop';
String get kind => 'dragAndDropWithCommandExtension';

DragCommand.deserialize(Map<String, String> params)
: startX = double.parse(params['startX']!),
Expand Down Expand Up @@ -56,7 +56,7 @@ class DragCommandExtension extends CommandExtension {
}

@override
String get commandKind => 'dragAndDrop';
String get commandKind => 'dragAndDropWithCommandExtension';

@override
Command deserialize(
Expand Down

0 comments on commit b34e23c

Please sign in to comment.