Skip to content
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

chore: Improve typing #135

Merged
merged 1 commit into from
Feb 2, 2025
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
14 changes: 9 additions & 5 deletions lib/commands/cookies.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const commands = {};

commands.deleteCookies = async function deleteCookies () {
/**
* @this {SafariDriver}
* @returns {Promise<any>}
*/
export async function deleteCookies () {
return await this.safari.proxy.command('/cookie', 'DELETE');
};
}

export { commands };
export default commands;
/**
* @typedef {import('../driver').SafariDriver} SafariDriver
*/
26 changes: 15 additions & 11 deletions lib/commands/find.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { util } from 'appium/support';


const commands = {};

// This is needed to make lookup by image working
commands.findElOrEls = async function findElOrEls (strategy, selector, mult, context) {
context = util.unwrapElement(context);
const endpoint = `/element${context ? `/${context}/element` : ''}${mult ? 's' : ''}`;

/**
*
* @this {SafariDriver}
* @param {string} strategy
* @param {string} selector
* @param {boolean} mult
* @param {string} [context]
* @returns {Promise<any>}
*/
export async function findElOrEls (strategy, selector, mult, context) {
const endpoint = `/element${context ? `/${util.unwrapElement(context)}/element` : ''}${mult ? 's' : ''}`;
return await this.safari.proxy.command(endpoint, 'POST', {
using: strategy,
value: selector,
});
};

}

export { commands };
export default commands;
/**
* @typedef {import('../driver').SafariDriver} SafariDriver
*/
15 changes: 0 additions & 15 deletions lib/commands/index.js

This file was deleted.

17 changes: 9 additions & 8 deletions lib/commands/record-screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { util, fs, net, tempDir } from 'appium/support';
import { waitForCondition } from 'asyncbox';
import { Simctl } from 'node-simctl';


const commands = {};

const STARTUP_INTERVAL_MS = 300;
const STARTUP_TIMEOUT_MS = 10 * 1000;
const DEFAULT_TIME_LIMIT_MS = 60 * 10 * 1000; // 10 minutes
Expand Down Expand Up @@ -235,11 +232,12 @@ async function extractSimulatorUdid (caps) {
* This method uses `xcrun simctl io recordVideo` helper under the hood.
* Check the output of `xcrun simctl io` command for more details.
*
* @this {SafariDriver}
* @param {StartRecordingOptions} options - The available options.
* @this {import('../driver').SafariDriver}
* @throws {Error} If screen recording has failed to start or is not supported for the destination device.
*/
commands.startRecordingScreen = async function startRecordingScreen (options) {
export async function startRecordingScreen (options) {
const {
timeLimit,
codec,
Expand Down Expand Up @@ -280,7 +278,7 @@ commands.startRecordingScreen = async function startRecordingScreen (options) {
this._screenRecorder = null;
throw e;
}
};
}

/**
* @typedef {Object} StopRecordingOptions
Expand All @@ -304,6 +302,7 @@ commands.startRecordingScreen = async function startRecordingScreen (options) {
* Stop recording the screen.
* If no screen recording has been started before then the method returns an empty string.
*
* @this {SafariDriver}
* @param {StopRecordingOptions} options - The available options.
* @returns {Promise<string>} Base64-encoded content of the recorded media file if 'remotePath'
* parameter is falsy or an empty string.
Expand All @@ -312,7 +311,7 @@ commands.startRecordingScreen = async function startRecordingScreen (options) {
* or the file content cannot be uploaded to the remote location
* or screen recording is not supported on the device under test.
*/
commands.stopRecordingScreen = async function stopRecordingScreen (options) {
export async function stopRecordingScreen (options) {
if (!this._screenRecorder) {
this.log.info('No screen recording has been started. Doing nothing');
return '';
Expand All @@ -329,6 +328,8 @@ commands.stopRecordingScreen = async function stopRecordingScreen (options) {
this.log.debug(`The size of the resulting screen recording is ${util.toReadableSizeString(size)}`);
}
return await uploadRecordedMedia(videoPath, options?.remotePath, options);
};
}

export default commands;
/**
* @typedef {import('../driver').SafariDriver} SafariDriver
*/
22 changes: 15 additions & 7 deletions lib/driver.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import _ from 'lodash';
import { BaseDriver } from 'appium/driver';
import SafariDriverServer from './safari';
import { SafariDriverServer } from './safari';
import { desiredCapConstraints } from './desired-caps';
import { commands } from './commands/index';
import * as cookieCommands from './commands/cookies';
import * as findCommands from './commands/find';
import * as recordScreenCommands from './commands/record-screen';
import { formatCapsForServer } from './utils';
import { newMethodMap } from './method-map';

Expand All @@ -20,6 +21,9 @@ export class SafariDriver extends BaseDriver {
/** @type {boolean} */
isProxyActive;

/** @type {SafariDriverServer} */
safari;

static newMethodMap = newMethodMap;

constructor (opts = {}) {
Expand All @@ -37,13 +41,10 @@ export class SafariDriver extends BaseDriver {
'name',
];
this.resetState();

for (const [cmd, fn] of _.toPairs(commands)) {
SafariDriver.prototype[cmd] = fn;
}
}

resetState () {
// @ts-ignore That's ok
this.safari = null;
this.proxyReqRes = null;
this.isProxyActive = false;
Expand Down Expand Up @@ -86,6 +87,13 @@ export class SafariDriver extends BaseDriver {

await super.deleteSession();
}

deleteCookies = cookieCommands.deleteCookies;

findElOrEls = findCommands.findElOrEls;

startRecordingScreen = recordScreenCommands.startRecordingScreen;
stopRecordingScreen = recordScreenCommands.stopRecordingScreen;
}

export default SafariDriver;
9 changes: 8 additions & 1 deletion lib/safari.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,16 @@ process.once('exit', () => {
}
});

class SafariDriverServer {
export class SafariDriverServer {
/** @type {SafariProxy} */
proxy;

/**
* @param {import('@appium/types').AppiumLogger} log
*/
constructor (log) {
this.log = log;
// @ts-ignore That's ok
this.proxy = null;
}

Expand Down