BrowserFactoryTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * This file is part of Chrome PHP.
  4. *
  5. * (c) Soufiane Ghzal <sghzal@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace HeadlessChromium\Test;
  11. use HeadlessChromium\BrowserFactory;
  12. use HeadlessChromium\Communication\Target;
  13. /**
  14. * @covers \HeadlessChromium\BrowserFactory
  15. * @covers \HeadlessChromium\Browser\BrowserProcess
  16. */
  17. class BrowserFactoryTest extends BaseTestCase
  18. {
  19. public function testBrowserFactory(): void
  20. {
  21. $factory = new BrowserFactory();
  22. $browser = $factory->createBrowser();
  23. $this->assertRegExp('#^ws://#', $browser->getSocketUri());
  24. }
  25. public function testWindowSizeOption(): void
  26. {
  27. $factory = new BrowserFactory();
  28. $browser = $factory->createBrowser([
  29. 'windowSize' => [1212, 333],
  30. ]);
  31. $page = $browser->createPage();
  32. $response = $page->evaluate('[window.outerHeight, window.outerWidth]')->getReturnValue();
  33. $this->assertEquals([333, 1212], $response);
  34. }
  35. public function testUserAgentOption(): void
  36. {
  37. $factory = new BrowserFactory();
  38. $browser = $factory->createBrowser([
  39. 'userAgent' => 'foo bar baz',
  40. ]);
  41. $page = $browser->createPage();
  42. $response = $page->evaluate('navigator.userAgent')->getReturnValue();
  43. $this->assertEquals('foo bar baz', $response);
  44. }
  45. public function testConnectToBrowser(): void
  46. {
  47. // create a browser
  48. $factory = new BrowserFactory();
  49. $browser = $factory->createBrowser();
  50. // TODO test existing pages propagation
  51. // create a new connectionn to the existing browser
  52. $browser2 = BrowserFactory::connectToBrowser($browser->getSocketUri());
  53. // create a page on the first browser after 2d connection
  54. $page2 = $browser->createPage();
  55. $page2TargetId = $page2->getSession()->getTargetId();
  56. // update 2d browser
  57. $browser2->getConnection()->readData();
  58. // make sure 2nd browser received the new page
  59. $target = $browser2->getTarget($page2TargetId);
  60. $this->assertInstanceOf(Target::class, $target);
  61. }
  62. }