BrowserFactoryTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. self::assertMatchesRegularExpression('#^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. self::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. self::assertEquals('foo bar baz', $response);
  44. }
  45. public function testAddHeaders(): void
  46. {
  47. $factory = new BrowserFactory();
  48. $factory->addHeader('header_name', 'header_value');
  49. $factory->addHeaders(['header_name2' => 'header_value2']);
  50. $factory->createBrowser()->createPage();
  51. $expected = [
  52. 'header_name' => 'header_value',
  53. 'header_name2' => 'header_value2',
  54. ];
  55. self::assertSame($expected, $factory->getOptions()['headers']);
  56. }
  57. public function testOptions(): void
  58. {
  59. $factory = new BrowserFactory();
  60. $headers = ['header_name' => 'header_value'];
  61. $options = ['userAgent' => 'foo bar baz'];
  62. $modifiedOptions = ['userAgent' => 'foo bar'];
  63. $factory->addHeaders($headers);
  64. $factory->addOptions($options);
  65. $factory->createBrowser()->createPage();
  66. $expected = \array_merge(['headers' => $headers], $options);
  67. self::assertSame($expected, $factory->getOptions());
  68. // test overwriting
  69. $factory->addOptions($modifiedOptions);
  70. $factory->createBrowser()->createPage();
  71. $expected['userAgent'] = 'foo bar';
  72. self::assertSame($expected, $factory->getOptions());
  73. // test removing options
  74. $factory->setOptions($modifiedOptions);
  75. $factory->createBrowser()->createPage();
  76. self::assertSame($modifiedOptions, $factory->getOptions());
  77. $factory->setOptions([]);
  78. $factory->createBrowser()->createPage();
  79. self::assertSame([], $factory->getOptions());
  80. }
  81. public function testConnectToBrowser(): void
  82. {
  83. // create a browser
  84. $factory = new BrowserFactory();
  85. $browser = $factory->createBrowser();
  86. // TODO test existing pages propagation
  87. // create a new connectionn to the existing browser
  88. $browser2 = BrowserFactory::connectToBrowser($browser->getSocketUri());
  89. // create a page on the first browser after 2d connection
  90. $page2 = $browser->createPage();
  91. $page2TargetId = $page2->getSession()->getTargetId();
  92. // update 2d browser
  93. $browser2->getConnection()->readData();
  94. // make sure 2nd browser received the new page
  95. $target = $browser2->getTarget($page2TargetId);
  96. self::assertInstanceOf(Target::class, $target);
  97. }
  98. }