BrowsingTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Browser;
  12. use HeadlessChromium\BrowserFactory;
  13. /**
  14. * @covers \HeadlessChromium\Browser
  15. * @covers \HeadlessChromium\Page
  16. */
  17. class BrowsingTest extends BaseTestCase
  18. {
  19. /**
  20. * @var Browser\ProcessAwareBrowser
  21. */
  22. public static $browser;
  23. public static function setUpBeforeClass(): void
  24. {
  25. parent::setUpBeforeClass();
  26. $factory = new BrowserFactory();
  27. self::$browser = $factory->createBrowser();
  28. }
  29. public static function tearDownAfterClass(): void
  30. {
  31. parent::tearDownAfterClass();
  32. self::$browser->close();
  33. }
  34. private function openSitePage($file)
  35. {
  36. $page = self::$browser->createPage();
  37. $page->navigate(self::sitePath($file))->waitForNavigation();
  38. return $page;
  39. }
  40. /**
  41. * @throws \HeadlessChromium\Exception\CommunicationException
  42. * @throws \HeadlessChromium\Exception\NoResponseAvailable
  43. */
  44. public function testPageNavigateEvaluate(): void
  45. {
  46. // initial navigation
  47. $page = $this->openSitePage('index.html');
  48. $title = $page->evaluate('document.title')->getReturnValue();
  49. self::assertEquals('foo', $title);
  50. // navigate again
  51. $page->navigate(self::sitePath('a.html'))->waitForNavigation();
  52. $title = $page->evaluate('document.title')->getReturnValue();
  53. self::assertEquals('a - test', $title);
  54. }
  55. public function testFormSubmission(): void
  56. {
  57. // initial navigation
  58. $page = $this->openSitePage('form.html');
  59. $evaluation = $page->evaluate(
  60. '(() => {
  61. document.querySelector("#myinput").value = "hello";
  62. setTimeout(() => {document.querySelector("#myform").submit();}, 300)
  63. })()'
  64. );
  65. $evaluation->waitForPageReload();
  66. self::assertEquals('hello', $page->evaluate('document.querySelector("#value").innerHTML')->getReturnValue());
  67. }
  68. public function testGetCurrentUrl(): void
  69. {
  70. $page = self::$browser->createPage();
  71. $page->getSession()->getConnection()->readData();
  72. self::assertEquals('about:blank', $page->getCurrentUrl());
  73. $page->navigate(self::sitePath('a.html'))->waitForNavigation();
  74. self::assertEquals(self::sitePath('a.html'), $page->getCurrentUrl());
  75. }
  76. public function testPageNavigationLocalNotFoundUrl(): void
  77. {
  78. $factory = new BrowserFactory();
  79. $browser = $factory->createBrowser();
  80. $page = $browser->createPage();
  81. // for some reasons chrome creates a new loader when we navigate to a local non-existent file
  82. // here we are testing that feature with strict and non strict modes
  83. $page->navigate('file:///does-not-exist')->waitForNavigation();
  84. self::assertTrue(true);
  85. }
  86. public function testGetPages(): void
  87. {
  88. $initialCount = \count(self::$browser->getPages());
  89. self::$browser->createPage();
  90. $finalCount = \count(self::$browser->getPages());
  91. self::assertGreaterThan($initialCount, $finalCount);
  92. }
  93. /**
  94. * @throws \HeadlessChromium\Exception\CommunicationException
  95. * @throws \HeadlessChromium\Exception\NoResponseAvailable
  96. */
  97. public function testGetPagesNavigateEvaluate(): void
  98. {
  99. self::$browser->createPage();
  100. $pages = self::$browser->getPages();
  101. foreach ($pages as $page) {
  102. // initial navigation
  103. $page = $this->openSitePage('index.html');
  104. $title = $page->evaluate('document.title')->getReturnValue();
  105. self::assertEquals('foo', $title);
  106. // navigate again
  107. $page->navigate(self::sitePath('a.html'))->waitForNavigation();
  108. $title = $page->evaluate('document.title')->getReturnValue();
  109. self::assertEquals('a - test', $title);
  110. }
  111. }
  112. public function testGetPagesClose(): void
  113. {
  114. self::$browser->createPage();
  115. $page = self::$browser->createPage();
  116. $initialCount = \count(self::$browser->getPages());
  117. $page->close();
  118. $finalCount = \count(self::$browser->getPages());
  119. self::assertLessThan($initialCount, $finalCount);
  120. }
  121. }