diff options
author | Fabio Bas <ctrlaltca@gmail.com> | 2014-02-28 10:39:25 +0100 |
---|---|---|
committer | Fabio Bas <ctrlaltca@gmail.com> | 2014-02-28 10:39:25 +0100 |
commit | 82774aa0a41562a325f31c901584ec01c8238573 (patch) | |
tree | 97931786ae69c7bf2b63fc87f232080e4b6c4b04 /tests/test_tools | |
parent | d21f0991c5920ae8432ba4ad561f843bec718d9c (diff) |
Ported all tests from Selenium1 (RC) to Selenium2 (WebDriver) using a compatibility layer
Diffstat (limited to 'tests/test_tools')
-rw-r--r-- | tests/test_tools/PradoGenericSelenium2Test.php | 281 | ||||
-rw-r--r-- | tests/test_tools/PradoGenericSeleniumTest.php | 50 | ||||
-rw-r--r-- | tests/test_tools/phpunit_bootstrap.php | 1 |
3 files changed, 268 insertions, 64 deletions
diff --git a/tests/test_tools/PradoGenericSelenium2Test.php b/tests/test_tools/PradoGenericSelenium2Test.php index 77dd3ece..ec948f7f 100644 --- a/tests/test_tools/PradoGenericSelenium2Test.php +++ b/tests/test_tools/PradoGenericSelenium2Test.php @@ -11,15 +11,14 @@ class PradoGenericSelenium2Test extends PHPUnit_Extensions_Selenium2TestCase 'browserName' => '*firefox', 'host' => '127.0.0.1', 'port' => 4444, - 'timeout' => 30000, ), */ array( 'name' => 'Chrome on OSX', - 'browserName' => '*googlechrome', + 'browserName' => 'chrome', + 'sessionStrategy' => 'shared', 'host' => '127.0.0.1', 'port' => 4444, - 'timeout' => 30000, ), /* array( @@ -37,40 +36,296 @@ class PradoGenericSelenium2Test extends PHPUnit_Extensions_Selenium2TestCase */ ); - static $baseurl='http://192.168.44.82/prado-master/tests/FunctionalTests/'; + static $baseurl='http://127.0.0.1/prado-master/tests/FunctionalTests/'; static $timeout=5; //seconds static $wait=1000; //msecs protected function setUp() { + self::shareSession(true); $this->setBrowserUrl(static::$baseurl); $this->setSeleniumServerRequestsTimeout(static::$timeout); } - public function setUpPage() + protected function verifyTitle($txt) { - $this->timeouts()->implicitWait(static::$wait); + $this->assertEquals($txt, $this->title()); } - protected function open($url) + protected function assertTextPresent($txt) { - $this->url($url); + if(strpos($txt, 'regexp:')===0) + { + $this->assertRegExp('/'.substr($txt, 7).'/', $this->source()); + } else { + $this->assertContains($txt, $this->source()); + } } - protected function tearDown() + protected function verifyAttribute($idattr, $txt) { + list($id, $attr) = explode('@', $idattr); + + $element = $this->getElement($id); + $value=$element->attribute($attr); + + if(strpos($txt, 'regexp:')===0) + { + $this->assertRegExp('/'.substr($txt, 7).'/', $value); + } else { + $this->assertEquals($txt, $value); + } + } + + protected function assertTextNotPresent($txt) + { + $this->assertNotContains($txt, $this->source()); + } + + protected function assertChecked($id) + { + $this->assertTrue($this->getElement($id)->selected()); + } + + protected function assertNotChecked($id) + { + $this->assertFalse($this->getElement($id)->selected()); } - protected function verifyTextPresent($txt) + protected function getElement($id) { - $this->assertContains($txt, $this->source()); + if(strpos($id, 'xpath=')===0) + { + return $this->byXPath(substr($id, 6)); + } elseif(strpos($id, 'css=')===0) { + return $this->byCssSelector(substr($id, 4)); + } elseif(strpos($id, 'id=')===0) { + return $this->byId(substr($id, 3)); + } elseif(strpos($id, 'link=')===0) { + return $this->byLinkText(substr($id, 5)); + } elseif(strpos($id, 'name=')===0) { + return $this->byName(substr($id, 5)); + } elseif(strpos($id, '//')===0) { + return $this->byXPath($id); + } elseif(strpos($id, '$')!==false) { + return $this->byName($id); + } else { + return $this->byId($id); + } } protected function assertText($id, $txt) { - $element = $this->byId($id); - $this->assertEquals($txt, $element->text()); + $this->assertEquals($txt, $this->getElement($id)->text()); + } + + protected function assertValue($id, $txt) + { + $this->assertEquals($txt, $this->getElement($id)->value()); + } + + protected function assertVisible($id) + { + $this->assertTrue($this->getElement($id)->displayed()); + } + + protected function assertNotVisible($id) + { + $this->assertFalse($this->getElement($id)->displayed()); + } + + protected function assertElementPresent($id) + { + $this->assertTrue($this->getElement($id)!==null); + } + + protected function assertAlert($txt) + { + $this->assertEquals($txt, $this->alertText()); + $this->acceptAlert(); + } + + protected function verifyConfirmation($txt) + { + $this->assertAlert($txt); + } + + protected function verifyConfirmationDismiss($txt) + { + $this->assertEquals($txt, $this->alertText()); + $this->dismissAlert(); + } + + protected function assertElementNotPresent($id) + { + try { + $el = $this->getElement($id); + } catch (PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) { + $this->assertEquals(PHPUnit_Extensions_Selenium2TestCase_WebDriverException::NoSuchElement, $e->getCode()); + return; + } + $this->fail('The element '.$id.' shouldn\'t exist.'); + } + + protected function type($id, $txt='') + { + $element = $this->getElement($id); + // clear the textbox without using clear() that triggers onchange() + // the idea is to focus the input, move to the end of the text and hit + // backspace until the input is empty. + // on multiline textareas, line feeds can make this difficult, so we mix + // sequences of end+backspace and start+delete + + $element->click(); + while(strlen($element->value())>0) + { + $this->keys(PHPUnit_Extensions_Selenium2TestCase_Keys::END); + // the number 100 is purely empiric + for($i=0;$i<100;$i++) + $this->keys(PHPUnit_Extensions_Selenium2TestCase_Keys::BACKSPACE); + + $this->keys(PHPUnit_Extensions_Selenium2TestCase_Keys::HOME); + // the number 100 is purely empiric + for($i=0;$i<100;$i++) + $this->keys(PHPUnit_Extensions_Selenium2TestCase_Keys::DELETE); + } + + $element->value($txt); + // trigger onblur() event + $this->clickAt('css=body', '1,1'); + } + + protected function click($id, $foo='bar') + { + $this->getElement($id)->click(); + } + + protected function clickAt($id, $coords) + { +// list($x, $y) = explode(',', $coords); + $this->moveto(array( + 'element' => $this->getElement($id), +// 'xoffset' => intval($x), +// 'yoffset' => intval($y), + )); + + parent::click(); + } + + protected function mouseOver($id) + { + $this->moveto(array( + 'element' => $this->getElement($id), +// 'xoffset' => 1, +// 'yoffset' => 1, + )); + } + + protected function mouseOut($id) + { + $this->moveto(array( + 'element' => $this->getElement('css=body'), +// 'xoffset' => 0, +// 'yoffset' => 0, + )); + } + + protected function clickAndWait($id, $foo='bar') + { + $this->click($id, $foo); + } + + protected function select($id, $value) + { + $select = parent::select($this->getElement($id)); + $select->clearSelectedOptions(); + + if(strpos($value, 'label=')===0) + { + $select->selectOptionByLabel(substr($value, 6)); + } else { + $select->selectOptionByLabel($value); + } + } + + protected function selectAndWait($id, $value) + { + $this->select($id, $value); + } + + protected function addSelection($id, $value) + { + $select = parent::select($this->getElement($id)); + + if(strpos($value, 'label=')===0) + { + $select->selectOptionByLabel(substr($value, 6)); + } else { + $select->selectOptionByLabel($value); + } + } + + protected function getSelectedLabels($id) + { + return parent::select($this->getElement($id))->selectedLabels(); + } + + protected function getSelectOptions($id) + { + return parent::select($this->getElement($id))->selectOptionLabels(); + } + + protected function assertSelectedIndex($id, $value) + { + $options=parent::select($this->getElement($id))->selectOptionValues(); + $curval=parent::select($this->getElement($id))->selectedValue(); + + $i=0; + foreach($options as $option) + { + if($option==$curval) + { + $this->assertEquals($i, $value); + return; + } + $i++; + } + $this->fail('Current value '.$curval.' not found in: '.implode(',', $options)); + } + + protected function assertSelected($id, $label) + { + $this->assertSame($label, parent::select($this->getElement($id))->selectedLabel()); + } + + protected function assertNotSomethingSelected($id) + { + $this->assertSame(array(), $this->getSelectedLabels($id)); + } + + protected function assertSelectedValue($id, $index) + { + $this->assertSame($index, parent::select($this->getElement($id))->selectedValue()); + } + + protected function runScript($script) + { + $this->execute(array( + 'script' => $script, + 'args' => array() + )); + } + + protected function assertAlertNotPresent() + { + try { + $foo=$this->alertText(); + } catch (PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) { + $this->assertEquals(PHPUnit_Extensions_Selenium2TestCase_WebDriverException::NoAlertOpenError, $e->getCode()); + return; + } + $this->fail('Failed asserting no alert is open'); } protected function pause($msec) diff --git a/tests/test_tools/PradoGenericSeleniumTest.php b/tests/test_tools/PradoGenericSeleniumTest.php deleted file mode 100644 index c3d3f4f0..00000000 --- a/tests/test_tools/PradoGenericSeleniumTest.php +++ /dev/null @@ -1,50 +0,0 @@ -<?php -require_once 'PHPUnit/Extensions/SeleniumTestCase.php'; - -class PradoGenericSeleniumTest extends PHPUnit_Extensions_SeleniumTestCase -{ - public static $browsers = array( -/* - array( - 'name' => 'Firefox on OSX', - 'browser' => '*firefox', - 'host' => '127.0.0.1', - 'port' => 4444, - 'timeout' => 30000, - ), -*/ - array( - 'name' => 'Chrome on OSX', - 'browser' => '*googlechrome', - 'host' => '127.0.0.1', - 'port' => 4444, - 'timeout' => 30000, - ), -/* - array( - 'name' => 'Firefox on WindowsXP', - 'browser' => '*firefox', - 'host' => '127.0.0.1', - 'port' => 4445, - ), - array( - 'name' => 'Internet Explorer 8 on WindowsXP', - 'browser' => '*iehta', - 'host' => '127.0.0.1', - 'port' => 4445, - ), -*/ - ); - - static $baseurl='http://192.168.44.82/prado-master/tests/FunctionalTests/'; - - protected function setUp() - { - $this->shareSession(true); - $this->setBrowserUrl(static::$baseurl); - } - - protected function tearDown() - { - } -}
\ No newline at end of file diff --git a/tests/test_tools/phpunit_bootstrap.php b/tests/test_tools/phpunit_bootstrap.php index 4357f99f..c1cb423f 100644 --- a/tests/test_tools/phpunit_bootstrap.php +++ b/tests/test_tools/phpunit_bootstrap.php @@ -24,5 +24,4 @@ if (!@include_once VENDOR_DIR.'/autoload.php') { require_once(PRADO_FRAMEWORK_DIR.'/prado.php'); // for FunctionalTests -require_once(__DIR__.'/PradoGenericSeleniumTest.php'); require_once(__DIR__.'/PradoGenericSelenium2Test.php');
\ No newline at end of file |