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

feat: update Ruby recorder code #1173

Merged
merged 2 commits into from
Nov 5, 2023
Merged
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
227 changes: 134 additions & 93 deletions app/renderer/lib/client-frameworks/ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,38 @@ class RubyFramework extends Framework {
return 'ruby';
}

// Use this instead of JSON.stringify, as it puts quotes around dictionary keys
getRubyVal (jsonVal) {
if (Array.isArray(jsonVal)) {
const convertedItems = jsonVal.map((item) => this.getRubyVal(item));
return `[${convertedItems.join(', ')}]`;
} else if (typeof jsonVal === 'object') {
const convertedItems = _.map(jsonVal, (v, k) =>
`${k}: ${this.getRubyVal(v)}`
);
return `{${convertedItems.join(', ')}}`;
}
return JSON.stringify(jsonVal);
}

wrapWithBoilerplate (code) {
let capStr = Object.keys(this.caps).map((k) => `caps[${JSON.stringify(k)}] = ${JSON.stringify(this.caps[k])}`).join('\n');
return `# This sample code uses the Appium ruby lib core client v5
const capStr = _.map(this.caps, (v, k) => `caps[${JSON.stringify(k)}] = ${this.getRubyVal(v)}`).join('\n');
return `# This sample code supports Appium Ruby lib core client >=5
# gem install appium_lib_core
# Then you can paste this into a file and simply run with Ruby

require 'appium_lib_core'

caps = {}
${capStr}
opts = {
server_url: "${this.serverUrl}"
}
driver = Appium::Core.for({caps: caps, appium_lib: opts}).start_driver

core = Appium::Core.for url: "${this.serverUrl}", caps: caps
driver = core.start_driver

${code}
driver.quit`;
}

codeFor_executeScript (varNameIgnore, varIndexIgnore, args) {
return `driver.execute_script '${args}'`;
}

addComment(comment) {
return `# ${comment}`;
}
Expand All @@ -41,7 +50,7 @@ driver.quit`;
'id': ':id',
'name': ':name',
'class name': ':class_name',
'-android uiautomator': ':uiautomation',
'-android uiautomator': ':uiautomator',
'-android datamatcher': ':datamatcher',
'-android viewtag': ':viewtag',
'-ios predicate string': ':predicate',
Expand Down Expand Up @@ -69,64 +78,83 @@ driver.quit`;
return `${this.getVarName(varName, varIndex)}.send_keys ${JSON.stringify(text)}`;
}

codeFor_back () {
return `driver.back`;
}

codeFor_tap (varNameIgnore, varIndexIgnore, pointerActions) {
const {x, y} = this.getTapCoordinatesFromPointerActions(pointerActions);

return `driver
.action
.move_to_location(${x}, ${y})
.pointer_down(:left)
.release
.perform
`;
`;
}

codeFor_swipe (varNameIgnore, varIndexIgnore, pointerActions) {
const {x1, y1, x2, y2} = this.getSwipeCoordinatesFromPointerActions(pointerActions);

return `driver
.action
.move_to_location(${x1}, ${y1})
.pointer_down(:left)
.move_to_location(${x2}, ${y2})
.release
.perform
`;
`;
}

// Execute Script

codeFor_executeScriptNoArgs (scriptCmd) {
return `driver.execute_script '${scriptCmd}'`;
}

codeFor_executeScriptWithArgs (scriptCmd, jsonArg) {
return `driver.execute_script '${scriptCmd}', ${this.getRubyVal(jsonArg[0])}`;
}

// App Management

codeFor_getCurrentActivity () {
return `current_activity = driver.current_activity`;
return `activity_name = ${this.codeFor_executeScriptNoArgs('mobile: getCurrentActivity')}`;
}

codeFor_getCurrentPackage () {
return `current_package = driver.current_package`;
return `package_name = ${this.codeFor_executeScriptNoArgs('mobile: getCurrentPackage')}`;
}


codeFor_installApp (varNameIgnore, varIndexIgnore, app) {
return `driver.app_installed? '${app}'`;
return `driver.install_app '${app}'`;
}

codeFor_isAppInstalled (varNameIgnore, varIndexIgnore, app) {
return `is_app_installed = driver.app_installed? '${app}'`;
}

codeFor_background (varNameIgnore, varIndexIgnore, timeout) {
return `driver.background_app ${timeout}`;
codeFor_background (varNameIgnore, varIndexIgnore, seconds) {
return this.codeFor_executeScriptWithArgs('mobile: backgroundApp', [{seconds}]);
}

codeFor_activateApp (varNameIgnore, varIndexIgnore, app) {
return `driver.activate_app '${app}'`;
}

codeFor_terminateApp (varNameIgnore, varIndexIgnore, app) {
return `driver.terminate_app '${app}'`;
}

codeFor_removeApp (varNameIgnore, varIndexIgnore, app) {
return `driver.remove_app '${app}'`;
}

codeFor_getStrings (varNameIgnore, varIndexIgnore, language, stringFile) {
return `driver.app_strings ${language ? `${language}, ` : ''}${stringFile ? `"${stringFile}` : ''}`;
codeFor_getStrings (varNameIgnore, varIndexIgnore, language) {
if (language === undefined) {
return `app_strings = ${this.codeFor_executeScriptNoArgs('mobile: getAppStrings')}`;
} else {
return `app_strings = ${this.codeFor_executeScriptWithArgs('mobile: getAppStrings', [{language}])}`;
}
}

// Clipboard

codeFor_getClipboard () {
return `clipboard_text = driver.get_clipboard`;
}
Expand All @@ -135,36 +163,79 @@ driver.quit`;
return `driver.set_clipboard content: '${clipboardText}'`;
}

codeFor_pressKeyCode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {
return `driver.press_keycode ${keyCode}, ${metaState}, ${flags}`;
// File Transfer

codeFor_pushFile (varNameIgnore, varIndexIgnore, pathToInstallTo, fileContentString) {
return `driver.push_file '${pathToInstallTo}', '${fileContentString}'`;
}

codeFor_longPressKeyCode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {
return `driver.long_press_keycode ${keyCode}, ${metaState}, ${flags}`;
codeFor_pullFile (varNameIgnore, varIndexIgnore, pathToPullFrom) {
return `driver.pull_file '${pathToPullFrom}'`;
}

codeFor_hideKeyboard () {
return `driver.hide_keyboard`;
codeFor_pullFolder (varNameIgnore, varIndexIgnore, folderToPullFrom) {
return `driver.pull_folder '${folderToPullFrom}'`;
}

codeFor_isKeyboardShown () {
return `is_keyboard_shown = driver.is_keyboard_shown`;
// Device Interaction

codeFor_shake () {
return this.codeFor_executeScriptNoArgs('mobile: shake');
}

codeFor_pushFile (varNameIgnore, varIndexIgnore, pathToInstallTo, fileContentString) {
return `driver.push_file '${pathToInstallTo}', '${fileContentString}'`;
codeFor_lock (varNameIgnore, varIndexIgnore, seconds) {
return this.codeFor_executeScriptWithArgs('mobile: lock', [{seconds}]);
}

codeFor_pullFile (varNameIgnore, varIndexIgnore, pathToPullFrom) {
return `driver.pull_file '${pathToPullFrom}'`;
codeFor_unlock () {
// TODO: UiAutomator2 requires arguments, XCUITest does not
return this.codeFor_executeScriptNoArgs('mobile: unlock');
}

codeFor_pullFolder (varNameIgnore, varIndexIgnore, folderToPullFrom) {
return `driver.pull_folder '${folderToPullFrom}'`;
codeFor_isLocked () {
return `is_locked = ${this.codeFor_executeScriptNoArgs('mobile: isLocked')}`;
}

codeFor_rotateDevice () {
return `# Not supported: rotateDevice`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for this issue itself. but maybe we no longer function this method itself.
https://github.com/search?q=org%3Aappium%20mobileRotation&type=code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they should likely be replaced with getRotation/setRotation. Not sure if the current drivers implement it though.

}

codeFor_fingerprint (varNameIgnore, varIndexIgnore, fingerprintId) {
return this.codeFor_executeScriptWithArgs('mobile: fingerprint', [{fingerprintId}]);
}

codeFor_touchId (varNameIgnore, varIndexIgnore, match) {
return `driver.touch_id ${match}`;
}

codeFor_toggleEnrollTouchId (varNameIgnore, varIndexIgnore, enroll) {
return `driver.toggle_touch_id_enrollment ${enroll}`;
}

// Keyboard

codeFor_pressKeyCode (varNameIgnore, varIndexIgnore, keycode, metastate, flags) {
return this.codeFor_executeScriptWithArgs('mobile: pressKey', [{keycode, metastate, flags}]);
}

codeFor_longPressKeyCode (varNameIgnore, varIndexIgnore, keycode, metastate, flags) {
return this.codeFor_executeScriptWithArgs('mobile: pressKey', [{keycode, metastate, flags, isLongPress: true}]);
}

codeFor_hideKeyboard () {
return this.codeFor_executeScriptNoArgs('mobile: hideKeyboard');
}

codeFor_isKeyboardShown () {
return `is_keyboard_shown = ${this.codeFor_executeScriptNoArgs('mobile: isKeyboardShown')}`;
}

// Connectivity

// TODO: use mobile: setConnectivity after adding it in GUI

codeFor_toggleAirplaneMode () {
return `driver.toggle_flight_mode`;
return `driver.toggle_airplane_mode`;
}

codeFor_toggleData () {
Expand All @@ -176,7 +247,7 @@ driver.quit`;
}

codeFor_toggleLocationServices () {
return `driver.toggle_location_services`;
return this.codeFor_executeScriptNoArgs('mobile: toggleGps');
}

codeFor_sendSMS (varNameIgnore, varIndexIgnore, phoneNumber, text) {
Expand All @@ -195,53 +266,27 @@ driver.quit`;
return `driver.gsm_voice :${state}`;
}

codeFor_shake () {
return `driver.shake`;
}

codeFor_lock (varNameIgnore, varIndexIgnore, seconds) {
return `driver.lock ${seconds}`;
}
// Performance Data

codeFor_unlock () {
return `driver.unlock`;
}

codeFor_isLocked () {
return `is_device_locked = driver.device_locked?`;
}

codeFor_rotateDevice () {
return `# Not supported: rotateDevice`;
}

codeFor_getPerformanceData (varNameIgnore, varIndexIgnore, packageName, dataType, dataReadTimeout) {
return `performance_data = driver.get_performance_data package_name: '${packageName}', data_type: '${dataType}', data_read_timeout: ${dataReadTimeout}`;
codeFor_getPerformanceData (varNameIgnore, varIndexIgnore, packageName, dataType) {
return this.codeFor_executeScriptWithArgs('mobile: getPerformanceData', [{packageName, dataType}]);
}

codeFor_getPerformanceDataTypes () {
return `performance_data = driver.get_performance_data_types`;
}

codeFor_touchId (varNameIgnore, varIndexIgnore, match) {
return `driver.touch_id ${match}`;
return this.codeFor_executeScriptNoArgs('mobile: getPerformanceDataTypes');
}

codeFor_toggleEnrollTouchId (varNameIgnore, varIndexIgnore, enroll) {
return `driver.toggle_touch_id_enrollment ${enroll}`;
}
// System

codeFor_openNotifications () {
return `driver.open_notifications`;
return this.codeFor_executeScriptNoArgs('mobile: openNotifications');
}

codeFor_getDeviceTime () {
return `device_time = driver.device_time`;
return this.codeFor_executeScriptNoArgs('mobile: getDeviceTime');
}

codeFor_fingerprint (varNameIgnore, varIndexIgnore, fingerprintId) {
return `driver.finger_print ${fingerprintId}`;
}
// Session

codeFor_getSession () {
return `session_capabilities = driver.session_capabilities`;
Expand All @@ -260,7 +305,7 @@ driver.quit`;
}

codeFor_getGeoLocation () {
return `geo_location = driver.location`;
return `location = driver.location`;
}

codeFor_setGeoLocation (varNameIgnore, varIndexIgnore, latitude, longitude, altitude) {
Expand All @@ -272,19 +317,11 @@ driver.quit`;
}

codeFor_getLogs (varNameIgnore, varIndexIgnore, logType) {
return `driver.logs.get '${logType}'`;
return `logs = driver.logs.get :${_.lowerCase(logType)}`;
}

codeFor_updateSettings (varNameIgnore, varIndexIgnore, settingsJson) {
try {
let settings = '';
for (let [settingName, settingValue] of _.toPairs(JSON.parse(settingsJson))) {
settings += `driver.settings.update ${settingName}: '${settingValue}'\n`;
}
return settings;
} catch (e) {
return `// Could not parse: ${settingsJson}`;
}
return `driver.update_settings ${this.getRubyVal(settingsJson)}`;
}

codeFor_getSettings () {
Expand All @@ -298,25 +335,29 @@ driver.quit`;
}

codeFor_getUrl () {
return `current_url = driver.current_url`;
return `current_url = driver.get_current_url`;
}

codeFor_back () {
return `driver.back`;
}

codeFor_forward () {
return `driver.navigate().forward`;
return `driver.navigate.forward`;
}

codeFor_refresh () {
return `driver.navigate().refresh`;
return `driver.navigate.refresh`;
}

// Context

codeFor_getContext () {
return `driver.current_context`;
return `context = driver.current_context`;
}

codeFor_getContexts () {
return `driver.available_contexts`;
return `contexts = driver.available_contexts`;
}

codeFor_switchContext (varNameIgnore, varIndexIgnore, name) {
Expand Down