appium.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. /**
  3. * Custom appium commands
  4. *
  5. * In this file we define all the custom commands which are part of the appium API but will probably
  6. * never be part of the webdriver spec or JsonWireProtocol.
  7. */
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. const fs = require("fs");
  10. const selenium_mock_1 = require("selenium-mock");
  11. const helpers_1 = require("./helpers");
  12. let app = {};
  13. let device = {};
  14. exports.appium = {
  15. app: app,
  16. device: device
  17. };
  18. app.toBackground =
  19. new selenium_mock_1.Command('POST', 'appium/app/background', (session, params) => {
  20. return new Promise((resolve) => {
  21. setTimeout(resolve, (params['seconds'] || 0) * 1000);
  22. });
  23. });
  24. app.closeApp = helpers_1.noopFactory('appium/app/close');
  25. app.getStrings = helpers_1.constFactory('POST', '/appium/app/strings', ['Hello', 'World']);
  26. app.launch = helpers_1.noopFactory('appium/app/launch');
  27. app.reset = helpers_1.noopFactory('appium/app/reset');
  28. device.getActivity = helpers_1.getterFactory('/appium/device/current_activity', 'activity');
  29. device.startActivity = helpers_1.setterFactory('/appium/device/start_activity', 'activity', 'appActivity');
  30. device.hideKeyboard = helpers_1.noopFactory('/appium/device/hide_keyboard');
  31. device.sendKeyEvent = helpers_1.noopFactory('/appium/device/keyevent');
  32. device.pressKeyCode = helpers_1.noopFactory('/appium/device/press_keycode');
  33. device.longPressKeyCode = helpers_1.noopFactory('/appium/device/long_press_keycode');
  34. device.installApp =
  35. new selenium_mock_1.Command('POST', 'appium/device/install_app', (session, params) => {
  36. fs.readFile(params['appPath'], (err, contents) => {
  37. if (err) {
  38. throw 'Error while trying to read "' + params['appPath'] + ': ' + err;
  39. }
  40. session.installedApps.push(contents.toString().trim());
  41. });
  42. });
  43. device.isAppInstalled =
  44. new selenium_mock_1.Command('POST', 'appium/device/app_installed', (session, params) => {
  45. return session.installedApps.some((app) => {
  46. return app === params['bundleId'] || app === params['appId'];
  47. });
  48. });
  49. device.removeApp =
  50. new selenium_mock_1.Command('POST', '/appium/device/remove_app', (session, params) => {
  51. session.installedApps = session.installedApps.filter((app) => {
  52. return app !== params['bundleId'] && app !== params['appId'];
  53. });
  54. });
  55. device.isLocked = helpers_1.getterFactory('/appium/device/is_locked', 'locked', 'POST');
  56. device.lock =
  57. new selenium_mock_1.Command('POST', 'appium/device/lock', (session, params) => {
  58. return new Promise((resolve) => {
  59. setTimeout(() => {
  60. session.locked = true;
  61. resolve();
  62. }, (params['seconds'] || 0) * 1000);
  63. });
  64. });
  65. device.unlock =
  66. new selenium_mock_1.Command('POST', 'appium/device/unlock', (session) => {
  67. session.locked = false;
  68. });
  69. device.pullFile =
  70. new selenium_mock_1.Command('POST', '/appium/device/pull_file', (session, params) => {
  71. let path = params['path'].split('/');
  72. if (path[0].length == 0) {
  73. path = path.slice(1);
  74. }
  75. ;
  76. let file = session.files;
  77. for (let folder of path) {
  78. file = file[folder];
  79. }
  80. return file;
  81. });
  82. device.pullFolder =
  83. new selenium_mock_1.Command('POST', '/appium/device/pull_folder', (session, params) => {
  84. let path = params['path'].split('/');
  85. if (path[0].length == 0) {
  86. path = path.slice(1);
  87. }
  88. ;
  89. let folder = session.files;
  90. for (let name of path) {
  91. folder = folder[name];
  92. }
  93. return folder;
  94. });
  95. device.pushFile =
  96. new selenium_mock_1.Command('POST', 'appium/device/push_file', (session, params) => {
  97. let path = params['path'].split('/');
  98. if (path[0].length == 0) {
  99. path = path.slice(1);
  100. }
  101. ;
  102. let folder = session.files;
  103. for (let i = 0; i < path.length - 1; i++) {
  104. if (folder[path[i]] === undefined) {
  105. folder[path[i]] = {};
  106. }
  107. folder = folder[path[i]];
  108. }
  109. folder[path[path.length - 1]] = params['data'];
  110. });
  111. device.getTime = helpers_1.constFactory('GET', '/appium/device/system_time', new Date().toString());
  112. device.openNotifications = helpers_1.noopFactory('/appium/device/open_notifications');
  113. device.rotate = helpers_1.noopFactory('appium/device/rotate');
  114. device.shake = helpers_1.noopFactory('appium/device/shake');
  115. exports.appium.getSettings = helpers_1.getterFactory('/appium/settings');
  116. exports.appium.setSettings = helpers_1.setterFactory('/appium/settings');
  117. exports.appium.setImmediateValue = helpers_1.noopFactory('/appium/element/:id/value');
  118. //# sourceMappingURL=appium.js.map