123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace HeadlessChromium\Test;
- use HeadlessChromium\Browser;
- use HeadlessChromium\BrowserFactory;
- /**
- * @covers \HeadlessChromium\Dom\Dom
- */
- class DomTest extends BaseTestCase
- {
- /**
- * @var Browser\ProcessAwareBrowser
- */
- public static $browser;
- public static function setUpBeforeClass(): void
- {
- parent::setUpBeforeClass();
- $factory = new BrowserFactory();
- self::$browser = $factory->createBrowser();
- }
- public static function tearDownAfterClass(): void
- {
- parent::tearDownAfterClass();
- self::$browser->close();
- }
- private function openSitePage($file)
- {
- $page = self::$browser->createPage();
- $page->navigate(self::sitePath($file))->waitForNavigation();
- return $page;
- }
- public function testSearchByCssSelector(): void
- {
- $page = $this->openSitePage('domForm.html');
- $element = $page->dom()->querySelector('button');
- $notFoundElement = $page->dom()->querySelector('img');
- self::assertNotNull($element);
- self::assertNull($notFoundElement);
- }
- public function testSearchByCssSelectorAll(): void
- {
- $page = $this->openSitePage('domForm.html');
- $elements = $page->dom()->querySelectorAll('div');
- self::assertCount(2, $elements);
- $notFoundElements = $page->dom()->querySelectorAll('img');
- self::assertCount(0, $notFoundElements);
- }
- public function testSearchByXpath(): void
- {
- $page = $this->openSitePage('domForm.html');
- $elements = $page->dom()->search('//*/div');
- self::assertCount(2, $elements);
- }
- public function testClick(): void
- {
- $page = $this->openSitePage('domForm.html');
- $element = $page->dom()->querySelector('#myinput');
- $value = $page
- ->evaluate('document.activeElement === document.querySelector("#myinput");')
- ->getReturnValue();
- self::assertFalse($value);
- // press the Tab key
- $element->click();
- // test the the focus switched to #myinput
- $value = $page
- ->evaluate('document.activeElement === document.querySelector("#myinput");')
- ->getReturnValue();
- self::assertTrue($value);
- }
- public function testType(): void
- {
- $page = $this->openSitePage('domForm.html');
- $element = $page->dom()->querySelector('#myinput');
- $element->click();
- $element->sendKeys('bar');
- $value = $page
- ->evaluate('document.querySelector("#myinput").value;')
- ->getReturnValue();
- // checks if the input contains the typed text
- self::assertEquals('bar', $value);
- }
- public function testGetText(): void
- {
- $page = $this->openSitePage('domForm.html');
- $element = $page->dom()->querySelector('#div1');
- $value = $element->getText();
- self::assertEquals('bar', $value);
- }
- public function testGetAttribute(): void
- {
- $page = $this->openSitePage('domForm.html');
- $element = $page->dom()->querySelector('#div1');
- $value = $element->getAttribute('type');
- self::assertEquals('foo', $value);
- }
- public function testSetAttribute(): void
- {
- $page = $this->openSitePage('domForm.html');
- $element = $page->dom()->querySelector('#div1');
- $element->setAttributeValue('type', 'hello');
- $value = $element->getAttribute('type');
- self::assertEquals('hello', $value);
- }
- public function testUploadFile(): void
- {
- $page = $this->openSitePage('domForm.html');
- $file = self::sitePath('domForm.html');
- $element = $page->dom()->querySelector('#myfile');
- $element->sendFile($file);
- $value = $page
- ->evaluate('document.querySelector("#myfile").value;')
- ->getReturnValue();
- // check if the file was selected
- self::assertStringEndsWith(\basename($file), $value);
- }
- public function testUploadFiles(): void
- {
- $page = $this->openSitePage('domForm.html');
- $files = [
- self::sitePath('domForm.html'),
- self::sitePath('form.html'),
- ];
- $element = $page->dom()->querySelector('#myfiles');
- $element->sendFiles($files);
- $value1 = $page->evaluate('document.querySelector("#myfiles").files[0].name;')->getReturnValue();
- $value2 = $page->evaluate('document.querySelector("#myfiles").files[1].name;')->getReturnValue();
- // check if the files were selected
- self::assertStringEndsWith(\basename($files[0]), $value1);
- self::assertStringEndsWith(\basename($files[1]), $value2);
- }
- }
|