inProcessFactory.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createInProcessPlaywright = createInProcessPlaywright;
  6. var _server = require("./server");
  7. var _connection = require("./client/connection");
  8. var _browserServerImpl = require("./browserServerImpl");
  9. var _androidServerImpl = require("./androidServerImpl");
  10. /**
  11. * Copyright (c) Microsoft Corporation.
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the 'License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. function createInProcessPlaywright() {
  26. const playwright = (0, _server.createPlaywright)({
  27. sdkLanguage: process.env.PW_LANG_NAME || 'javascript'
  28. });
  29. const clientConnection = new _connection.Connection(undefined, undefined);
  30. clientConnection.useRawBuffers();
  31. const dispatcherConnection = new _server.DispatcherConnection(true /* local */);
  32. // Dispatch synchronously at first.
  33. dispatcherConnection.onmessage = message => clientConnection.dispatch(message);
  34. clientConnection.onmessage = message => dispatcherConnection.dispatch(message);
  35. const rootScope = new _server.RootDispatcher(dispatcherConnection);
  36. // Initialize Playwright channel.
  37. new _server.PlaywrightDispatcher(rootScope, playwright);
  38. const playwrightAPI = clientConnection.getObjectWithKnownName('Playwright');
  39. playwrightAPI.chromium._serverLauncher = new _browserServerImpl.BrowserServerLauncherImpl('chromium');
  40. playwrightAPI.firefox._serverLauncher = new _browserServerImpl.BrowserServerLauncherImpl('firefox');
  41. playwrightAPI.webkit._serverLauncher = new _browserServerImpl.BrowserServerLauncherImpl('webkit');
  42. playwrightAPI._android._serverLauncher = new _androidServerImpl.AndroidServerLauncherImpl();
  43. // Switch to async dispatch after we got Playwright object.
  44. dispatcherConnection.onmessage = message => setImmediate(() => clientConnection.dispatch(message));
  45. clientConnection.onmessage = message => setImmediate(() => dispatcherConnection.dispatch(message));
  46. clientConnection.toImpl = x => x ? dispatcherConnection._dispatchers.get(x._guid)._object : dispatcherConnection._dispatchers.get('');
  47. playwrightAPI._toImpl = clientConnection.toImpl;
  48. return playwrightAPI;
  49. }