export function initMockSeleniumStandaloneServerAndGetDriverFactory()

in spec/command_tests/helpers.ts [32:79]


export function initMockSeleniumStandaloneServerAndGetDriverFactory(annotateCommands = false) {
  let server: MockAppium;
  let port: number;
  beforeAll((done) => {
    portfinder.getPort((err: Error, p: number) => {
      if (err) {
        done.fail(err);
      } else {
        port = p;
        server = new MockAppium(port);
        server.start();
        done();
      }
    });
  });

  if (annotateCommands && !commandMap) {
    buildCommandMap(commandList);
  }

  return () => {
    let driver = extend(new webdriver.Builder().
        usingServer('http://localhost:' + port + '/wd/hub').
        withCapabilities({browserName: 'chrome'}).build());
    if (annotateCommands) {
      Object.keys(commandDefinitions).forEach((commandName) => {
        let clientCommand = (commandDefinitions as any)[commandName] as CommandDefinition<any>;
        let serverCommand = commandMap[clientCommand.method + ':' +
            (clientCommand.path[0] == '/' ? '' : '/') + clientCommand.path];
        let spy = spyOn(serverCommand, 'exec').and.callThrough();
        let oldFun = (driver as any)[commandName];
        (driver as any)[commandName] = function() {
          let oldCount = spy.calls.count();
          return oldFun.apply(this, arguments).then((result: any) => {
            expect(spy.calls.count()).toBe(oldCount + 1);
            let args = spy.calls.mostRecent().args;
            return {
              result: result,
              session: args[0],
              params: args[1]
            };
          });
        };
      });
    }
    return driver;
  };
}