ResourceInstantiator.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Nesk\Puphpeteer\Tests;
  3. use Nesk\Rialto\Data\JsFunction;
  4. class ResourceInstantiator
  5. {
  6. protected $resources = [];
  7. public function __construct(
  8. public array $browserOptions,
  9. public string $url
  10. ) {
  11. $this->resources = [
  12. 'Accessibility' => function ($puppeteer) {
  13. return $this->Page($puppeteer)->accessibility;
  14. },
  15. 'Browser' => function ($puppeteer) {
  16. return $puppeteer->launch($this->browserOptions);
  17. },
  18. 'BrowserContext' => function ($puppeteer) {
  19. return $this->Browser($puppeteer)->createIncognitoBrowserContext();
  20. },
  21. 'CDPSession' => function ($puppeteer) {
  22. return $this->Target($puppeteer)->createCDPSession();
  23. },
  24. 'ConsoleMessage' => function () {
  25. return new UntestableResource;
  26. },
  27. 'Coverage' => function ($puppeteer) {
  28. return $this->Page($puppeteer)->coverage;
  29. },
  30. 'Dialog' => function () {
  31. return new UntestableResource;
  32. },
  33. 'ElementHandle' => function ($puppeteer) {
  34. return $this->Page($puppeteer)->querySelector('body');
  35. },
  36. 'EventEmitter' => function ($puppeteer) {
  37. return $puppeteer->launch($this->browserOptions);
  38. },
  39. 'ExecutionContext' => function ($puppeteer) {
  40. return $this->Frame($puppeteer)->executionContext();
  41. },
  42. 'FileChooser' => function () {
  43. return new UntestableResource;
  44. },
  45. 'Frame' => function ($puppeteer) {
  46. return $this->Page($puppeteer)->mainFrame();
  47. },
  48. 'HTTPRequest' => function ($puppeteer) {
  49. return $this->HTTPResponse($puppeteer)->request();
  50. },
  51. 'HTTPResponse' => function ($puppeteer) {
  52. return $this->Page($puppeteer)->goto($this->url);
  53. },
  54. 'JSHandle' => function ($puppeteer) {
  55. return $this->Page($puppeteer)->evaluateHandle(JsFunction::createWithBody('window'));
  56. },
  57. 'Keyboard' => function ($puppeteer) {
  58. return $this->Page($puppeteer)->keyboard;
  59. },
  60. 'Mouse' => function ($puppeteer) {
  61. return $this->Page($puppeteer)->mouse;
  62. },
  63. 'Page' => function ($puppeteer) {
  64. return $this->Browser($puppeteer)->newPage();
  65. },
  66. 'SecurityDetails' => function ($puppeteer) {
  67. return new RiskyResource(function () use ($puppeteer) {
  68. return $this->Page($puppeteer)->goto('https://example.com')->securityDetails();
  69. });
  70. },
  71. 'Target' => function ($puppeteer) {
  72. return $this->Page($puppeteer)->target();
  73. },
  74. 'TimeoutError' => function () {
  75. return new UntestableResource;
  76. },
  77. 'Touchscreen' => function ($puppeteer) {
  78. return $this->Page($puppeteer)->touchscreen;
  79. },
  80. 'Tracing' => function ($puppeteer) {
  81. return $this->Page($puppeteer)->tracing;
  82. },
  83. 'WebWorker' => function ($puppeteer) {
  84. $page = $this->Page($puppeteer);
  85. $page->goto($this->url, ['waitUntil' => 'networkidle0']);
  86. return $page->workers()[0];
  87. },
  88. ];
  89. }
  90. public function getResourceNames(): array
  91. {
  92. return array_keys($this->resources);
  93. }
  94. public function __call(string $name, array $arguments)
  95. {
  96. if (!isset($this->resources[$name])) {
  97. throw new \InvalidArgumentException("The $name resource is not supported.");
  98. }
  99. return $this->resources[$name](...$arguments);
  100. }
  101. }