Skip to content
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 5 additions & 13 deletions src/android.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import AndroidUiautomator2Driver from 'appium-uiautomator2-driver';
import { log } from './logger';
import type { InitialOpts } from '@appium/types';
import { AppiumFlutterDriver } from './driver';
import ADB from 'appium-adb';

const setupNewAndroidDriver = async (
export async function startAndroidSession(
this: AppiumFlutterDriver,
...args: any[]
): Promise<AndroidUiautomator2Driver> => {
): Promise<AndroidUiautomator2Driver> {
this.log.info(`Starting an Android proxy session`);
const androiddriver = new AndroidUiautomator2Driver({} as InitialOpts);
//@ts-ignore Args are ok
await androiddriver.createSession(...args);
return androiddriver;
};

export const startAndroidSession = async (
flutterDriver: AppiumFlutterDriver,
caps: Record<string, any>,
...args: any[]
): Promise<AndroidUiautomator2Driver> => {
log.info(`Starting an Android proxy session`);
return await setupNewAndroidDriver(...args);
};
}

export async function androidPortForward(
adb: ADB,
Expand Down
35 changes: 25 additions & 10 deletions src/commands/element.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { log } from '../logger';
import _ from 'lodash';
import { getProxyDriver } from '../utils';
import { JWProxy } from '@appium/base-driver';
import AndroidUiautomator2Driver from 'appium-uiautomator2-driver';
import { W3C_ELEMENT_KEY } from '@appium/base-driver/build/lib/constants';
import type { AppiumFlutterDriver } from '../driver';

export const ELEMENT_CACHE = new Map();
export async function findElOrEls(
this: AppiumFlutterDriver,
strategy: string,
selector: string,
mult: boolean,
context: string,
): Promise<any> {
log.info('Finding element or elements', strategy, selector, mult, context);
const driver = await getProxyDriver(strategy, this.proxy, this.proxydriver);
const driver = await getProxyDriver.bind(this)(strategy);
let elementBody;
if (
!(driver instanceof JWProxy) &&
Expand Down Expand Up @@ -43,23 +44,30 @@ export async function findElOrEls(
}
}

export async function click(element: string) {
export async function click(this: AppiumFlutterDriver, element: string) {
const driver = ELEMENT_CACHE.get(element);
return await driver.command(`/element/${element}/click`, 'POST', {
element,
});
}

export async function getText(elementId: string) {
export async function getText(this: AppiumFlutterDriver, elementId: string) {
const driver = ELEMENT_CACHE.get(elementId);
return String(await driver.command(`/element/${elementId}/text`, 'GET', {}));
}

export async function elementEnabled(elementId: string) {
export async function elementEnabled(
this: AppiumFlutterDriver,
elementId: string,
) {
return toBool(await this.getAttribute('enabled', elementId));
}

export async function getAttribute(attribute: string, elementId: string) {
export async function getAttribute(
this: AppiumFlutterDriver,
attribute: string,
elementId: string,
) {
const driver = ELEMENT_CACHE.get(elementId);
return await driver.command(
`/element/${elementId}/attribute/${attribute}`,
Expand All @@ -68,21 +76,28 @@ export async function getAttribute(attribute: string, elementId: string) {
);
}

export async function setValue(text: string, elementId: string) {
export async function setValue(
this: AppiumFlutterDriver,
text: string,
elementId: string,
) {
const driver = ELEMENT_CACHE.get(elementId);
return await driver.command(`/element/${elementId}/value`, 'POST', {
text,
});
}

export async function clear(elementId: string) {
export async function clear(this: AppiumFlutterDriver, elementId: string) {
const driver = ELEMENT_CACHE.get(elementId);
return await driver.command(`/element/${elementId}/clear`, 'POST', {
elementId,
});
}

export async function elementDisplayed(elementId: string) {
export async function elementDisplayed(
this: AppiumFlutterDriver,
elementId: string,
) {
return await this.getAttribute('displayed', elementId);
}

Expand Down
27 changes: 7 additions & 20 deletions src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
type FlutterDriverConstraints = typeof desiredCapConstraints;
import XCUITestDriver from 'appium-xcuitest-driver/build/lib/driver';
import AndroidUiautomator2Driver from 'appium-uiautomator2-driver';
import { createSession } from './session';
import { createSession as createSessionMixin } from './session';
import {
findElOrEls,
click,
Expand Down Expand Up @@ -158,10 +158,9 @@ export class AppiumFlutterDriver extends BaseDriver<FlutterDriverConstraints> {
* flutterServerPort need to be passed as lauch argument using appium:processArguments
* Refer: https://appium.github.io/appium-xcuitest-driver/latest/reference/capabilities/
*/
attachAppLaunchArguments(caps, ...args);
attachAppLaunchArguments.bind(this)(caps, ...args);

let sessionCreated = await createSession.call(
this,
let sessionCreated = await createSessionMixin.bind(this)(
sessionId,
caps,
...JSON.parse(JSON.stringify(args)),
Expand Down Expand Up @@ -204,23 +203,16 @@ export class AppiumFlutterDriver extends BaseDriver<FlutterDriverConstraints> {
portcallbacks.portForwardCallback = iosPortForward;
portcallbacks.portReleaseCallback = iosRemovePortForward;
}
const flutterCaps: DriverCaps<FlutterDriverConstraints> = {
flutterServerLaunchTimeout:
this.internalCaps.flutterServerLaunchTimeout || 5000,
flutterSystemPort: isIosSimulator
? this.internalCaps.flutterSystemPort
: this.internalCaps.flutterSystemPort || (await getFreePort()),
} as DriverCaps<FlutterDriverConstraints>;

const systemPort = flutterCaps.flutterSystemPort!;
const systemPort =
this.internalCaps.flutterSystemPort ?? (await getFreePort());
const udid = this.proxydriver.opts.udid!;

this.flutterPort = await fetchFlutterServerPort({
this.flutterPort = await fetchFlutterServerPort.bind(this)({
udid,
packageName,
...portcallbacks,
systemPort,
flutterCaps,
isIosSimulator,
});

Expand Down Expand Up @@ -345,10 +337,6 @@ export class AppiumFlutterDriver extends BaseDriver<FlutterDriverConstraints> {
}

async activateApp(appId: string, bundleId: string) {
const flutterCaps: DriverCaps<FlutterDriverConstraints> = {
flutterServerLaunchTimeout:
this.internalCaps?.flutterServerLaunchTimeout || 5000,
} as DriverCaps<FlutterDriverConstraints>;
let activateAppResponse;
//run only for ios
if (
Expand All @@ -365,11 +353,10 @@ export class AppiumFlutterDriver extends BaseDriver<FlutterDriverConstraints> {
await this.proxydriver.activateApp(appId || bundleId);
}

await waitForFlutterServerToBeActive(
await waitForFlutterServerToBeActive.bind(this)(
this.proxy,
appId,
this.flutterPort,
flutterCaps,
);
await this.proxy?.command('/session', 'POST', {
capabilities: this.proxydriver.originalCaps,
Expand Down
21 changes: 6 additions & 15 deletions src/iOS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,23 @@ import { AppiumFlutterDriver } from './driver';
// @ts-ignore
import XCUITestDriver from 'appium-xcuitest-driver';
import type { InitialOpts } from '@appium/types';
import { log } from './logger';
import { DEVICE_CONNECTIONS_FACTORY } from './iProxy';

const setupNewIOSDriver = async (...args: any[]): Promise<XCUITestDriver> => {
export async function startIOSSession(
this: AppiumFlutterDriver,
...args: any[]
): Promise<XCUITestDriver> {
this.log.info(`Starting an IOS proxy session`);
const iosdriver = new XCUITestDriver({} as InitialOpts);
await iosdriver.createSession(...args);
return iosdriver;
};

export const startIOSSession = async (
flutterDriver: AppiumFlutterDriver,
caps: Record<string, any>,
...args: any[]
): Promise<XCUITestDriver> => {
log.info(`Starting an IOS proxy session`);
return await setupNewIOSDriver(...args);
};
}

export async function iosPortForward(
udid: string,
systemPort: number,
devicePort: number,
) {
log.info(
`Forwarding port ${systemPort} to device port ${devicePort} ${udid}`,
);
await DEVICE_CONNECTIONS_FACTORY.requestConnection(udid, systemPort, {
usePortForwarding: true,
devicePort: devicePort,
Expand Down
2 changes: 1 addition & 1 deletion src/iProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { logger, util, timing } from '@appium/support';
import { utilities } from 'appium-ios-device';
import { checkPortStatus } from 'portscanner';
import { waitForCondition } from 'asyncbox';
import { AppiumLogger } from '@appium/types';
import type { AppiumLogger } from '@appium/types';

const LOCALHOST = '127.0.0.1';

Expand Down
12 changes: 7 additions & 5 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ import _ from 'lodash';
import { PLATFORM } from './platform';
import { startAndroidSession } from './android';
import { startIOSSession } from './iOS';
export const createSession: any = async function (
import type { DefaultCreateSessionResult } from '@appium/types';

export async function createSession(
this: AppiumFlutterDriver,
sessionId: string,
caps: any,
...args: any[]
) {
): Promise<DefaultCreateSessionResult<any>> {
try {
switch (_.toLower(caps.platformName)) {
case PLATFORM.IOS:
this.proxydriver = await startIOSSession(this, caps, ...args);
this.proxydriver = await startIOSSession.bind(this)(...args);
this.proxydriver.relaxedSecurityEnabled =
this.relaxedSecurityEnabled;
this.proxydriver.denyInsecure = this.denyInsecure;
this.proxydriver.allowInsecure = this.allowInsecure;

break;
case PLATFORM.ANDROID:
this.proxydriver = await startAndroidSession(this, caps, ...args);
this.proxydriver = await startAndroidSession.bind(this)(...args);
this.proxydriver.relaxedSecurityEnabled =
this.relaxedSecurityEnabled;
this.proxydriver.denyInsecure = this.denyInsecure;
Expand All @@ -38,4 +40,4 @@ export const createSession: any = async function (
await this.deleteSession();
throw e;
}
};
}
Loading