Skip to content

Commit 5e70e17

Browse files
committed
implement all finder APIs
1 parent 125b7bb commit 5e70e17

File tree

2 files changed

+136
-21
lines changed

2 files changed

+136
-21
lines changed

example/src/index.js

+61-20
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22
// import * as assert from 'assert';
33
const wdio = require('webdriverio');
44
const assert = require('assert');
5-
const { byValueKey } = require('appium-flutter-finder');
6-
7-
const osSpecificOps = process.env.APPIUM_OS === 'android' ? {
8-
platformName: 'Android',
9-
deviceName: 'Pixel 2',
10-
// @todo support non-unix style path
11-
app: __dirname + '/../apps/app-free-debug.apk',
12-
}: process.env.APPIUM_OS === 'ios' ? {
13-
platformName: 'iOS',
14-
platformVersion: '12.2',
15-
deviceName: 'iPhone X',
16-
noReset: true,
17-
app: __dirname + '/../apps/Runner.zip',
18-
19-
} : {};
5+
const find = require('appium-flutter-finder');
6+
7+
const osSpecificOps =
8+
process.env.APPIUM_OS === 'android'
9+
? {
10+
platformName: 'Android',
11+
deviceName: 'Pixel 2',
12+
// @todo support non-unix style path
13+
app: __dirname + '/../apps/app-free-debug.apk'
14+
}
15+
: process.env.APPIUM_OS === 'ios'
16+
? {
17+
platformName: 'iOS',
18+
platformVersion: '12.2',
19+
deviceName: 'iPhone X',
20+
noReset: true,
21+
app: __dirname + '/../apps/Runner.zip'
22+
}
23+
: {};
2024

2125
const opts = {
2226
port: 4723,
@@ -27,19 +31,22 @@ const opts = {
2731
};
2832

2933
(async () => {
30-
const counterTextFinder = byValueKey('counter');
31-
const buttonFinder = byValueKey('increment');
34+
const counterTextFinder = find.byValueKey('counter');
35+
const buttonFinder = find.byValueKey('increment');
3236

3337
const driver = await wdio.remote(opts);
3438

39+
/* new example
3540
if (process.env.APPIUM_OS === 'android') {
3641
await driver.switchContext('NATIVE_APP');
3742
await (await driver.$('~fab')).click();
3843
await driver.switchContext('FLUTTER');
3944
} else {
40-
console.log('Switching context to `NATIVE_APP` is currently only applicable to Android demo app.')
45+
console.log(
46+
'Switching context to `NATIVE_APP` is currently only applicable to Android demo app.'
47+
);
4148
}
42-
49+
*/
4350

4451
assert.strictEqual(await driver.getElementText(counterTextFinder), '0');
4552

@@ -49,8 +56,42 @@ const opts = {
4956
element: { elementId: buttonFinder }
5057
});
5158

52-
5359
assert.strictEqual(await driver.getElementText(counterTextFinder), '2');
5460

61+
await driver.elementClick(find.byTooltip('Increment'));
62+
63+
assert.strictEqual(
64+
await driver.getElementText(
65+
find.descendant({
66+
of: find.byTooltip('counter_tooltip'),
67+
matching: find.byValueKey('counter')
68+
})
69+
),
70+
'3'
71+
);
72+
73+
await driver.elementClick(find.byType('FlatButton'));
74+
// await driver.waitForAbsent(byTooltip('counter_tooltip'));
75+
76+
assert.strictEqual(
77+
await driver.getElementText(find.byText('This is 2nd route')),
78+
'This is 2nd route'
79+
);
80+
81+
await driver.elementClick(find.pageBack());
82+
83+
assert.strictEqual(
84+
await driver.getElementText(
85+
find.descendant({
86+
of: find.ancestor({
87+
of: find.bySemanticsLabel(RegExp('counter_semantic')),
88+
matching: find.byType('Tooltip')
89+
}),
90+
matching: find.byType('Text')
91+
})
92+
),
93+
'3'
94+
);
95+
5596
driver.deleteSession();
5697
})();

finder/lib/serializer.ts

+75-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,86 @@
1+
import { deserialize } from './deserializer';
2+
13
// @todo consider using protobuf
24
function serialize(obj: object) {
35
return Buffer.from(JSON.stringify(obj)).toString(`base64`);
46
}
57

8+
export type SerializableFinder = string;
9+
export type Pattern = string | RegExp;
10+
11+
export const ancestor = (args: {
12+
of: SerializableFinder;
13+
matching: SerializableFinder;
14+
matchRoot: boolean;
15+
}) => {
16+
const { of, matching } = args;
17+
const matchRoot = args.matchRoot || false;
18+
const a: any = {
19+
finderType: `Ancestor`,
20+
matchRoot,
21+
};
22+
Object.entries(deserialize(of)).forEach(
23+
([key, value]) => (a[`of_${key}`] = value),
24+
);
25+
Object.entries(deserialize(matching)).forEach(
26+
([key, value]) => (a[`matching_${key}`] = value),
27+
);
28+
return serialize(a);
29+
};
30+
31+
export const bySemanticsLabel = (label: Pattern) =>
32+
serialize({
33+
finderType: `BySemanticsLabel`,
34+
isRegExp: label instanceof RegExp ? true : false,
35+
label: label.toString().slice(1, -1),
36+
});
37+
38+
export const byTooltip = (text: string) =>
39+
serialize({
40+
finderType: `ByTooltipMessage`,
41+
text,
42+
});
43+
44+
export const byType = (type: string) =>
45+
serialize({
46+
finderType: `ByType`,
47+
type,
48+
});
49+
650
export const byValueKey = (key: string | number) =>
751
serialize({
852
finderType: `ByValueKey`,
953
keyValueString: key,
10-
// @todo is `int` correct?
1154
keyValueType: typeof key === `string` ? `String` : `int`,
1255
});
56+
57+
export const descendant = (args: {
58+
of: SerializableFinder;
59+
matching: SerializableFinder;
60+
matchRoot: boolean;
61+
}) => {
62+
const { of, matching } = args;
63+
const matchRoot = args.matchRoot || false;
64+
const a: any = {
65+
finderType: `Descendant`,
66+
matchRoot,
67+
};
68+
Object.entries(deserialize(of)).forEach(
69+
([key, value]) => (a[`of_${key}`] = value),
70+
);
71+
Object.entries(deserialize(matching)).forEach(
72+
([key, value]) => (a[`matching_${key}`] = value),
73+
);
74+
return serialize(a);
75+
};
76+
77+
export const pageBack = () =>
78+
serialize({
79+
finderType: `PageBack`,
80+
});
81+
82+
export const byText = (text: string) =>
83+
serialize({
84+
finderType: `ByText`,
85+
text,
86+
});

0 commit comments

Comments
 (0)