DomTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. $this->assertNotNull($element);
  36. }
  37. public function testSearchByCssSelectorAll(): void
  38. {
  39. $page = $this->openSitePage('domForm.html');
  40. $elements = $page->dom()->querySelectorAll('div');
  41. $this->assertCount(2, $elements);
  42. }
  43. public function testSearchByXpath(): void
  44. {
  45. $page = $this->openSitePage('domForm.html');
  46. $elements = $page->dom()->search('//*/div');
  47. $this->assertCount(2, $elements);
  48. }
  49. public function testClick(): void
  50. {
  51. $page = $this->openSitePage('domForm.html');
  52. $element = $page->dom()->querySelector('#myinput');
  53. $value = $page
  54. ->evaluate('document.activeElement === document.querySelector("#myinput");')
  55. ->getReturnValue();
  56. $this->assertFalse($value);
  57. // press the Tab key
  58. $element->click();
  59. // test the the focus switched to #myinput
  60. $value = $page
  61. ->evaluate('document.activeElement === document.querySelector("#myinput");')
  62. ->getReturnValue();
  63. $this->assertTrue($value);
  64. }
  65. public function testType(): void
  66. {
  67. $page = $this->openSitePage('domForm.html');
  68. $element = $page->dom()->querySelector('#myinput');
  69. $element->click();
  70. $element->sendKeys('bar');
  71. $value = $page
  72. ->evaluate('document.querySelector("#myinput").value;')
  73. ->getReturnValue();
  74. // checks if the input contains the typed text
  75. $this->assertEquals('bar', $value);
  76. }
  77. public function testGetText(): void
  78. {
  79. $page = $this->openSitePage('domForm.html');
  80. $element = $page->dom()->querySelector('#div1');
  81. $value = $element->getText();
  82. $this->assertEquals('bar', $value);
  83. }
  84. public function testGetAttribute(): void
  85. {
  86. $page = $this->openSitePage('domForm.html');
  87. $element = $page->dom()->querySelector('#div1');
  88. $value = $element->getAttribute('type');
  89. $this->assertEquals('foo', $value);
  90. }
  91. public function testSetAttribute(): void
  92. {
  93. $page = $this->openSitePage('domForm.html');
  94. $element = $page->dom()->querySelector('#div1');
  95. $element->setAttributeValue('type', 'hello');
  96. $value = $element->getAttribute('type');
  97. $this->assertEquals('hello', $value);
  98. }
  99. public function testUploadFile(): void
  100. {
  101. $page = $this->openSitePage('domForm.html');
  102. $file = self::sitePath('domForm.html');
  103. $element = $page->dom()->querySelector('#myfile');
  104. $element->sendFile($file);
  105. $value = $page
  106. ->evaluate('document.querySelector("#myfile").value;')
  107. ->getReturnValue();
  108. // check if file was uploaded
  109. $this->assertNotEmpty($value);
  110. }
  111. }