123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?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');
- $this->assertNotNull($element);
- }
- public function testSearchByCssSelectorAll(): void
- {
- $page = $this->openSitePage('domForm.html');
- $elements = $page->dom()->querySelectorAll('div');
- $this->assertCount(2, $elements);
- }
- public function testSearchByXpath(): void
- {
- $page = $this->openSitePage('domForm.html');
- $elements = $page->dom()->search('//*/div');
- $this->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();
- $this->assertFalse($value);
- // press the Tab key
- $element->click();
- // test the the focus switched to #myinput
- $value = $page
- ->evaluate('document.activeElement === document.querySelector("#myinput");')
- ->getReturnValue();
- $this->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
- $this->assertEquals('bar', $value);
- }
- public function testGetText(): void
- {
- $page = $this->openSitePage('domForm.html');
- $element = $page->dom()->querySelector('#div1');
- $value = $element->getText();
- $this->assertEquals('bar', $value);
- }
- public function testGetAttribute(): void
- {
- $page = $this->openSitePage('domForm.html');
- $element = $page->dom()->querySelector('#div1');
- $value = $element->getAttribute('type');
- $this->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');
- $this->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 file was uploaded
- $this->assertNotEmpty($value);
- }
- }
|