DomTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace HeadlessChromium\Test;
  3. use HeadlessChromium\Browser;
  4. use HeadlessChromium\BrowserFactory;
  5. /**
  6. * @covers \HeadlessChromium\Dom\Dom
  7. */
  8. class DomTest extends BaseTestCase
  9. {
  10. /**
  11. * @var Browser\ProcessAwareBrowser
  12. */
  13. public static $browser;
  14. public static function setUpBeforeClass(): void
  15. {
  16. parent::setUpBeforeClass();
  17. $factory = new BrowserFactory();
  18. self::$browser = $factory->createBrowser();
  19. }
  20. public static function tearDownAfterClass(): void
  21. {
  22. parent::tearDownAfterClass();
  23. self::$browser->close();
  24. }
  25. private function openSitePage($file)
  26. {
  27. $page = self::$browser->createPage();
  28. $page->navigate(self::sitePath($file))->waitForNavigation();
  29. return $page;
  30. }
  31. public function testSearchByCssSelector(): void
  32. {
  33. $page = $this->openSitePage('domForm.html');
  34. $element = $page->dom()->querySelector('button');
  35. $notFoundElement = $page->dom()->querySelector('img');
  36. self::assertNotNull($element);
  37. self::assertNull($notFoundElement);
  38. }
  39. public function testSearchByCssSelectorAll(): void
  40. {
  41. $page = $this->openSitePage('domForm.html');
  42. $elements = $page->dom()->querySelectorAll('div');
  43. self::assertCount(2, $elements);
  44. $notFoundElements = $page->dom()->querySelectorAll('img');
  45. self::assertCount(0, $notFoundElements);
  46. }
  47. public function testSearchByXpath(): void
  48. {
  49. $page = $this->openSitePage('domForm.html');
  50. $elements = $page->dom()->search('//*/div');
  51. self::assertCount(2, $elements);
  52. }
  53. public function testClick(): void
  54. {
  55. $page = $this->openSitePage('domForm.html');
  56. $element = $page->dom()->querySelector('#myinput');
  57. $value = $page
  58. ->evaluate('document.activeElement === document.querySelector("#myinput");')
  59. ->getReturnValue();
  60. self::assertFalse($value);
  61. // press the Tab key
  62. $element->click();
  63. // test the the focus switched to #myinput
  64. $value = $page
  65. ->evaluate('document.activeElement === document.querySelector("#myinput");')
  66. ->getReturnValue();
  67. self::assertTrue($value);
  68. }
  69. public function testType(): void
  70. {
  71. $page = $this->openSitePage('domForm.html');
  72. $element = $page->dom()->querySelector('#myinput');
  73. $element->click();
  74. $element->sendKeys('bar');
  75. $value = $page
  76. ->evaluate('document.querySelector("#myinput").value;')
  77. ->getReturnValue();
  78. // checks if the input contains the typed text
  79. self::assertEquals('bar', $value);
  80. }
  81. public function testGetText(): void
  82. {
  83. $page = $this->openSitePage('domForm.html');
  84. $element = $page->dom()->querySelector('#div1');
  85. $value = $element->getText();
  86. self::assertEquals('bar', $value);
  87. }
  88. public function testGetAttribute(): void
  89. {
  90. $page = $this->openSitePage('domForm.html');
  91. $element = $page->dom()->querySelector('#div1');
  92. $value = $element->getAttribute('type');
  93. self::assertEquals('foo', $value);
  94. }
  95. public function testSetAttribute(): void
  96. {
  97. $page = $this->openSitePage('domForm.html');
  98. $element = $page->dom()->querySelector('#div1');
  99. $element->setAttributeValue('type', 'hello');
  100. $value = $element->getAttribute('type');
  101. self::assertEquals('hello', $value);
  102. }
  103. public function testUploadFile(): void
  104. {
  105. $page = $this->openSitePage('domForm.html');
  106. $file = self::sitePath('domForm.html');
  107. $element = $page->dom()->querySelector('#myfile');
  108. $element->sendFile($file);
  109. $value = $page
  110. ->evaluate('document.querySelector("#myfile").value;')
  111. ->getReturnValue();
  112. // check if the file was selected
  113. self::assertStringEndsWith(\basename($file), $value);
  114. }
  115. public function testUploadFiles(): void
  116. {
  117. $page = $this->openSitePage('domForm.html');
  118. $files = [
  119. self::sitePath('domForm.html'),
  120. self::sitePath('form.html'),
  121. ];
  122. $element = $page->dom()->querySelector('#myfiles');
  123. $element->sendFiles($files);
  124. $value1 = $page->evaluate('document.querySelector("#myfiles").files[0].name;')->getReturnValue();
  125. $value2 = $page->evaluate('document.querySelector("#myfiles").files[1].name;')->getReturnValue();
  126. // check if the files were selected
  127. self::assertStringEndsWith(\basename($files[0]), $value1);
  128. self::assertStringEndsWith(\basename($files[1]), $value2);
  129. }
  130. }