ResourceInstanciator.php 3.8 KB

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