BrowsingTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. $this->assertEquals('foo', $title);
  50. // navigate again
  51. $page->navigate(self::sitePath('a.html'))->waitForNavigation();
  52. $title = $page->evaluate('document.title')->getReturnValue();
  53. $this->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. $this->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. $this->assertEquals('about:blank', $page->getCurrentUrl());
  73. $page->navigate(self::sitePath('a.html'))->waitForNavigation();
  74. $this->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. $this->assertTrue(true);
  85. }
  86. }