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 Python recorder code #1167

Merged
merged 1 commit into from
Nov 2, 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
195 changes: 117 additions & 78 deletions app/renderer/lib/client-frameworks/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ class PythonFramework extends Framework {
getPythonVal (jsonVal) {
if (typeof jsonVal === 'boolean') {
return jsonVal ? 'True' : 'False';
} else if (Array.isArray(jsonVal)) {
const convertedItems = jsonVal.map((item) => this.getPythonVal(item));
return `[${convertedItems.join(', ')}]`;
} else if (typeof jsonVal === 'object') {
const convertedItems = Object.keys(jsonVal).map((k) =>
`${JSON.stringify(k)}: ${this.getPythonVal(jsonVal[k])}`
);
return `{${convertedItems.join(', ')}}`;
}
return JSON.stringify(jsonVal);
}
Expand Down Expand Up @@ -43,17 +51,13 @@ driver.quit()`;
return `# ${comment}`;
}

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

codeFor_findAndAssign (strategy, locator, localVar, isArray) {
let suffixMap = {
xpath: 'AppiumBy.XPATH',
'accessibility id': 'AppiumBy.ACCESSIBILITY_ID',
'id': 'AppiumBy.ID',
'name': 'AppiumBy.NAME',
id: 'AppiumBy.ID',
'class name': 'AppiumBy.CLASS_NAME',
name: 'AppiumBy.NAME',
'-android uiautomator': 'AppiumBy.ANDROID_UIAUTOMATOR',
'-android datamatcher': 'AppiumBy.ANDROID_DATA_MATCHER',
'-android viewtag': 'AppiumBy.ANDROID_VIEWTAG',
Expand Down Expand Up @@ -82,36 +86,43 @@ 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 `actions = ActionChains(driver)
actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
actions.w3c_actions.pointer_action.move_to_location(${x}, ${y})
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.pause(0.1)
actions.w3c_actions.pointer_action.release()
actions.perform()
`;
`;
}

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

return `actions = ActionChains(driver)
actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
actions.w3c_actions.pointer_action.move_to_location(${x1}, ${y1})
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.move_to_location(${x2}, ${y2})
actions.w3c_actions.pointer_action.release()
actions.perform()
`;
`;
}

// Execute Script

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


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

// App Management

codeFor_getCurrentActivity () {
return `activity_name = driver.current_activity`;
}
Expand All @@ -121,25 +132,35 @@ actions.perform()
}

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

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

codeFor_background (varNameIgnore, varIndexIgnore, timeout) {
return `driver.background_app(${timeout})`;
}

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

Choose a reason for hiding this comment

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

I wondered if this command ideally needs to be mobile: command -> https://github.com/appium/appium-uiautomator2-driver?tab=readme-ov-file#mobile-activateapp
but for now, looks good to me. the argument name should be changed for mobile command for android/ios and other drivers

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is fine, because the implementation in the Python client calls mobile: activateApp anyway: https://github.com/appium/python-client/blob/master/appium/webdriver/extensions/applications.py#L187

}

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

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

codeFor_getStrings (varNameIgnore, varIndexIgnore, language, stringFile) {
return `appStrings = driver.app_strings(${language ? `${language}, ` : ''}${stringFile ? `"${stringFile}` : ''})`;
return `app_strings = driver.app_strings(${language ? `'${language}'` : 'None'}, ${stringFile ? `'${stringFile}'` : 'None'})`;
}

// Clipboard

codeFor_getClipboard () {
return `clipboard_text = driver.get_clipboard_text()`;
}
Expand All @@ -148,34 +169,74 @@ actions.perform()
return `driver.set_clipboard_text('${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 `file_base64 = driver.pull_file('${pathToPullFrom}')`;
}

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

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

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

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

codeFor_pullFile (varNameIgnore, varIndexIgnore, pathToPullFrom) {
return `file_base64 = self.driver.pull_file('${pathToPullFrom}');`;
codeFor_unlock () {
return `driver.unlock()`;
}

codeFor_pullFolder (varNameIgnore, varIndexIgnore, folderToPullFrom) {
return `file_base64 = self.driver.pull_folder('${folderToPullFrom}');`;
codeFor_isLocked () {
return `is_locked = driver.is_locked()`;
}

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

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

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

codeFor_toggleEnrollTouchId () {
return `driver.toggle_touch_id_enrollment()`;
}

// Keyboard

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

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

codeFor_hideKeyboard () {
return `driver.hide_keyboard()`;
}

codeFor_isKeyboardShown () {
return `is_keyboard_shown = driver.is_keyboard_shown()`;
}

// Connectivity

codeFor_toggleAirplaneMode () {
return `# Not supported: toggleAirplaneMode`;
}
Expand All @@ -189,111 +250,85 @@ actions.perform()
}

codeFor_toggleLocationServices () {
return `driver.toggle_location_services();`;
return `driver.toggle_location_services()`;
}

codeFor_sendSMS () {
return `# Not supported: sendSMS`;
codeFor_sendSMS (varNameIgnore, varIndexIgnore, phoneNumber, text) {
return `driver.send_sms('${phoneNumber}', '${text}')`;
}

codeFor_gsmCall (varNameIgnore, varIndexIgnore, phoneNumber, action) {
return `driver.make_gsm_call(${phoneNumber}, ${action})`;
return `driver.make_gsm_call('${phoneNumber}', '${action}')`;
}

codeFor_gsmSignal (varNameIgnore, varIndexIgnore, signalStrength) {
return `driver.set_gsm_signal(${signalStrength})`;
}

codeFor_gsmVoice (varNameIgnore, varIndexIgnore, state) {
return `driver.set_gsm_voice(${state})`;
}

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

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

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

codeFor_isLocked () {
return `driver.is_locked()`;
return `driver.set_gsm_voice('${state}')`;
}

codeFor_rotateDevice () {
return `# Not supported: rotate device`;
}
// Performance Data

codeFor_getPerformanceData (varNameIgnore, varIndexIgnore, packageName, dataType, dataReadTimeout) {
return `driver.get_performance_data('${packageName}', '${dataType}', ${dataReadTimeout})`;
return `performance_data = driver.get_performance_data('${packageName}', '${dataType}', ${dataReadTimeout})`;
}

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

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

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

codeFor_openNotifications () {
return `driver.open_notifications();`;
}

codeFor_getDeviceTime () {
return `time = self.driver.device_time()`;
return `time = driver.device_time()`;
}

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

codeFor_getSession () {
return `desired_caps = self.driver.desired_capabilities()`;
return `desired_caps = driver.desired_capabilities()`;
}

codeFor_setTimeouts (/*varNameIgnore, varIndexIgnore, timeoutsJson*/) {
return '# TODO implement setTimeouts';
}

codeFor_getOrientation () {
return `orientation = self.driver.orientation()`;
return `orientation = driver.orientation`;
}

codeFor_setOrientation (varNameIgnore, varIndexIgnore, orientation) {
return `driver.orientation = "${orientation}"`;
return `driver.orientation = '${orientation}'`;
}

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

codeFor_setGeoLocation (varNameIgnore, varIndexIgnore, latitude, longitude, altitude) {
return `driver.set_location(${latitude}, ${longitude}, ${altitude})`;
}

codeFor_getLogTypes () {
return `log_types = driver.log_types();`;
return `log_types = driver.log_types()`;
}

codeFor_getLogs (varNameIgnore, varIndexIgnore, logType) {
return `logs = driver.get_log('${logType}');`;
return `logs = driver.get_log('${logType}')`;
}

codeFor_updateSettings (varNameIgnore, varIndexIgnore, settingsJson) {
return `driver.update_settings(${settingsJson}))`;
}

codeFor_getSettings () {
return `settings = driver.get_settings`;
return `settings = driver.get_settings()`;
}

// Web
Expand All @@ -306,6 +341,10 @@ actions.perform()
return `current_url = driver.current_url`;
}

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

codeFor_forward () {
return `driver.forward()`;
}
Expand All @@ -317,11 +356,11 @@ actions.perform()
// Context

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

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

codeFor_switchContext (varNameIgnore, varIndexIgnore, name) {
Expand Down