helpers.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const webdriver = require("selenium-webdriver");
  4. const commandDefinitions = require("../../lib/command_definitions");
  5. const mock_server_1 = require("../mock-server");
  6. const commands_1 = require("../mock-server/commands");
  7. const selenium_mock_1 = require("selenium-mock");
  8. const lib_1 = require("../../lib");
  9. let portfinder = require('portfinder');
  10. let commandMap = null;
  11. function buildCommandMap(commandList) {
  12. if (commandMap == null) {
  13. commandMap = {};
  14. }
  15. for (let commandName in commandList) {
  16. let command = commandList[commandName];
  17. if (command instanceof selenium_mock_1.Command) {
  18. commandMap[command.method + ':' + (command.path[0] == '/' ? '' : '/') + command.path] = command;
  19. }
  20. else {
  21. buildCommandMap(command);
  22. }
  23. }
  24. }
  25. function initMockSeleniumStandaloneServerAndGetDriverFactory(annotateCommands = false) {
  26. let server;
  27. let port;
  28. beforeAll((done) => {
  29. portfinder.getPort((err, p) => {
  30. if (err) {
  31. done.fail(err);
  32. }
  33. else {
  34. port = p;
  35. server = new mock_server_1.MockAppium(port);
  36. server.start();
  37. done();
  38. }
  39. });
  40. });
  41. if (annotateCommands && !commandMap) {
  42. buildCommandMap(commands_1.session);
  43. }
  44. return () => {
  45. let driver = lib_1.extend(new webdriver.Builder().
  46. usingServer('http://localhost:' + port + '/wd/hub').
  47. withCapabilities({ browserName: 'chrome' }).build());
  48. if (annotateCommands) {
  49. Object.keys(commandDefinitions).forEach((commandName) => {
  50. let clientCommand = commandDefinitions[commandName];
  51. let serverCommand = commandMap[clientCommand.method + ':' +
  52. (clientCommand.path[0] == '/' ? '' : '/') + clientCommand.path];
  53. let spy = spyOn(serverCommand, 'exec').and.callThrough();
  54. let oldFun = driver[commandName];
  55. driver[commandName] = function () {
  56. let oldCount = spy.calls.count();
  57. return oldFun.apply(this, arguments).then((result) => {
  58. expect(spy.calls.count()).toBe(oldCount + 1);
  59. let args = spy.calls.mostRecent().args;
  60. return {
  61. result: result,
  62. session: args[0],
  63. params: args[1]
  64. };
  65. });
  66. };
  67. });
  68. }
  69. return driver;
  70. };
  71. }
  72. exports.initMockSeleniumStandaloneServerAndGetDriverFactory = initMockSeleniumStandaloneServerAndGetDriverFactory;
  73. //# sourceMappingURL=helpers.js.map