Puppeteer.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Copyright 2017 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. const Launcher = require('./Launcher');
  17. const BrowserFetcher = require('./BrowserFetcher');
  18. const Errors = require('./Errors');
  19. const DeviceDescriptors = require('./DeviceDescriptors');
  20. module.exports = class {
  21. /**
  22. * @param {string} projectRoot
  23. * @param {string} preferredRevision
  24. * @param {boolean} isPuppeteerCore
  25. */
  26. constructor(projectRoot, preferredRevision, isPuppeteerCore) {
  27. this._projectRoot = projectRoot;
  28. this._preferredRevision = preferredRevision;
  29. this._isPuppeteerCore = isPuppeteerCore;
  30. }
  31. /**
  32. * @param {!(Launcher.LaunchOptions & Launcher.ChromeArgOptions & Launcher.BrowserOptions & {product?: string, extraPrefsFirefox?: !object})=} options
  33. * @return {!Promise<!Puppeteer.Browser>}
  34. */
  35. launch(options) {
  36. if (!this._productName && options)
  37. this._productName = options.product;
  38. return this._launcher.launch(options);
  39. }
  40. /**
  41. * @param {!(Launcher.BrowserOptions & {browserWSEndpoint?: string, browserURL?: string, transport?: !Puppeteer.ConnectionTransport})} options
  42. * @return {!Promise<!Puppeteer.Browser>}
  43. */
  44. connect(options) {
  45. return this._launcher.connect(options);
  46. }
  47. /**
  48. * @return {string}
  49. */
  50. executablePath() {
  51. return this._launcher.executablePath();
  52. }
  53. /**
  54. * @return {!Puppeteer.ProductLauncher}
  55. */
  56. get _launcher() {
  57. if (!this._lazyLauncher)
  58. this._lazyLauncher = Launcher(this._projectRoot, this._preferredRevision, this._isPuppeteerCore, this._productName);
  59. return this._lazyLauncher;
  60. }
  61. /**
  62. * @return {string}
  63. */
  64. get product() {
  65. return this._launcher.product;
  66. }
  67. /**
  68. * @return {Object}
  69. */
  70. get devices() {
  71. return DeviceDescriptors;
  72. }
  73. /**
  74. * @return {Object}
  75. */
  76. get errors() {
  77. return Errors;
  78. }
  79. /**
  80. * @param {!Launcher.ChromeArgOptions=} options
  81. * @return {!Array<string>}
  82. */
  83. defaultArgs(options) {
  84. return this._launcher.defaultArgs(options);
  85. }
  86. /**
  87. * @param {!BrowserFetcher.Options=} options
  88. * @return {!BrowserFetcher}
  89. */
  90. createBrowserFetcher(options) {
  91. return new BrowserFetcher(this._projectRoot, options);
  92. }
  93. };