Skip to content

Commit

Permalink
implement all finder APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
truongsinh committed Jul 23, 2019
1 parent 125b7bb commit 5e70e17
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 21 deletions.
81 changes: 61 additions & 20 deletions example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
// import * as assert from 'assert';
const wdio = require('webdriverio');
const assert = require('assert');
const { byValueKey } = require('appium-flutter-finder');

const osSpecificOps = process.env.APPIUM_OS === 'android' ? {
platformName: 'Android',
deviceName: 'Pixel 2',
// @todo support non-unix style path
app: __dirname + '/../apps/app-free-debug.apk',
}: process.env.APPIUM_OS === 'ios' ? {
platformName: 'iOS',
platformVersion: '12.2',
deviceName: 'iPhone X',
noReset: true,
app: __dirname + '/../apps/Runner.zip',

} : {};
const find = require('appium-flutter-finder');

const osSpecificOps =
process.env.APPIUM_OS === 'android'
? {
platformName: 'Android',
deviceName: 'Pixel 2',
// @todo support non-unix style path
app: __dirname + '/../apps/app-free-debug.apk'
}
: process.env.APPIUM_OS === 'ios'
? {
platformName: 'iOS',
platformVersion: '12.2',
deviceName: 'iPhone X',
noReset: true,
app: __dirname + '/../apps/Runner.zip'
}
: {};

const opts = {
port: 4723,
Expand All @@ -27,19 +31,22 @@ const opts = {
};

(async () => {
const counterTextFinder = byValueKey('counter');
const buttonFinder = byValueKey('increment');
const counterTextFinder = find.byValueKey('counter');
const buttonFinder = find.byValueKey('increment');

const driver = await wdio.remote(opts);

/* new example
if (process.env.APPIUM_OS === 'android') {
await driver.switchContext('NATIVE_APP');
await (await driver.$('~fab')).click();
await driver.switchContext('FLUTTER');
} else {
console.log('Switching context to `NATIVE_APP` is currently only applicable to Android demo app.')
console.log(
'Switching context to `NATIVE_APP` is currently only applicable to Android demo app.'
);
}

*/

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

Expand All @@ -49,8 +56,42 @@ const opts = {
element: { elementId: buttonFinder }
});


assert.strictEqual(await driver.getElementText(counterTextFinder), '2');

await driver.elementClick(find.byTooltip('Increment'));

assert.strictEqual(
await driver.getElementText(
find.descendant({
of: find.byTooltip('counter_tooltip'),
matching: find.byValueKey('counter')
})
),
'3'
);

await driver.elementClick(find.byType('FlatButton'));
// await driver.waitForAbsent(byTooltip('counter_tooltip'));

assert.strictEqual(
await driver.getElementText(find.byText('This is 2nd route')),
'This is 2nd route'
);

await driver.elementClick(find.pageBack());

assert.strictEqual(
await driver.getElementText(
find.descendant({
of: find.ancestor({
of: find.bySemanticsLabel(RegExp('counter_semantic')),
matching: find.byType('Tooltip')
}),
matching: find.byType('Text')
})
),
'3'
);

driver.deleteSession();
})();
76 changes: 75 additions & 1 deletion finder/lib/serializer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,86 @@
import { deserialize } from './deserializer';

// @todo consider using protobuf
function serialize(obj: object) {
return Buffer.from(JSON.stringify(obj)).toString(`base64`);
}

export type SerializableFinder = string;
export type Pattern = string | RegExp;

export const ancestor = (args: {
of: SerializableFinder;
matching: SerializableFinder;
matchRoot: boolean;
}) => {
const { of, matching } = args;
const matchRoot = args.matchRoot || false;
const a: any = {
finderType: `Ancestor`,
matchRoot,
};
Object.entries(deserialize(of)).forEach(
([key, value]) => (a[`of_${key}`] = value),
);
Object.entries(deserialize(matching)).forEach(
([key, value]) => (a[`matching_${key}`] = value),
);
return serialize(a);
};

export const bySemanticsLabel = (label: Pattern) =>
serialize({
finderType: `BySemanticsLabel`,
isRegExp: label instanceof RegExp ? true : false,
label: label.toString().slice(1, -1),
});

export const byTooltip = (text: string) =>
serialize({
finderType: `ByTooltipMessage`,
text,
});

export const byType = (type: string) =>
serialize({
finderType: `ByType`,
type,
});

export const byValueKey = (key: string | number) =>
serialize({
finderType: `ByValueKey`,
keyValueString: key,
// @todo is `int` correct?
keyValueType: typeof key === `string` ? `String` : `int`,
});

export const descendant = (args: {
of: SerializableFinder;
matching: SerializableFinder;
matchRoot: boolean;
}) => {
const { of, matching } = args;
const matchRoot = args.matchRoot || false;
const a: any = {
finderType: `Descendant`,
matchRoot,
};
Object.entries(deserialize(of)).forEach(
([key, value]) => (a[`of_${key}`] = value),
);
Object.entries(deserialize(matching)).forEach(
([key, value]) => (a[`matching_${key}`] = value),
);
return serialize(a);
};

export const pageBack = () =>
serialize({
finderType: `PageBack`,
});

export const byText = (text: string) =>
serialize({
finderType: `ByText`,
text,
});

0 comments on commit 5e70e17

Please sign in to comment.