This extensions allows to run functionaln tests using WebDriver functions from Selenium Server 2.0. WebDriver runs as a plugin in remote browser, so it is much more reliable than standard Selenium test injected through JavaScript.
Extension is using PHP WebDriver Bindings project from http://code.google.com/p/php-webdriver-bindings/
Requires PHP WebDriver Bindings (they are packaged in this extension) and Selenium Server 2.0 as tests runtime environment. Should work with Firefox and Chrome browsers (they are supported by Selenium WebDriver)
Extract archive in your extensions directory. Write functional test that extend CWebDriverTestCase in your tests/functional directory as usual.
Example:
Yii::import( 'ext.webdriver-bindings.CWebDriverTestCase' ); class ExampleTest extends CWebDriverTestCase { protected function setUp() { parent::setUp( 'localhost', 4444, 'firefox' ); } public function testGoogle() { $this->get( 'http://www.google.com/' ); $qElem = $this->findElementBy( LocatorStrategy::name, 'q' ); $this->assertNotNull( $qElem, 'There is no "query" element!' ); $qElem->sendKeys( array( 'yii framework' ) ); $qElem->submit(); sleep( 1 ); $elem = $this->findElementBy( LocatorStrategy::className, 'vsc' ); $this->assertNotNull( $elem, 'Results not found!' ); $this->assertTrue( $this->isTextPresent( 'Yii Framework' ), 'The is no "Yii Framework" text on result page!' ); } }
or other way (based on CDbTestCase instead of WebTestCase):
define( 'TEST_BASE_URL', 'http://www.google.com/' ); Yii::import( 'ext.webdriver-bindings.CWebDriverDbTestCase' ); class ExampleDbTest extends CWebDriverDbTestCase { public $baseUrl = TEST_BASE_URL; public function testGoogle() { $this->get( 'http://www.google.com/' ); $qElem = $this->findElementBy( LocatorStrategy::name, 'q' ); $this->assertNotNull( $qElem, 'There is no "query" element!' ); $qElem->sendKeys( array( 'yii framework' ) ); $qElem->submit(); sleep( 1 ); $elem = $this->findElementBy( LocatorStrategy::className, 'vsc' ); $this->assertNotNull( $elem, 'Results not found!' ); $this->assertTrue( $this->isTextPresent( 'Yii Framework' ), 'The is no "Yii Framework" text on result page!' ); } }
Total 10 comments
Does anyone had this problem?
I just started the server by: java -jar selenium-server-standalone-2.25.0.jar
And runned the test by: phpunit functional/SiteTestWebDriver.php
Am I missing something?
Running on ubuntu 12.04 / firefox 15.0.1.
Thank you very much.
Just posted version with proper __call handling.
Hi,
i have encountered a problem with database fixtures and CWebDriverDbTestCase this morning.
CWebDriverDbTestCase is overwriting the magic __call method to call methods on its webdriver object.
But CDbTestCase is using __call to redirect calls to fixtures as well. CWebDriverDbTestCase should redirect unknown methods to parent::__call.
Change method __call in CWebDriverDbTestCase (~line 95) to
Just uploaded new version of our extension.
Changelog:
I have been experimenting with this extension and I found that it opens a new browser session for each test method of a test case. This is because new session is created in setUp() and destroyed in tearDown().
I find this not convenient because of two reasons:
First, it takes time to close and open a new browser window. The time overhead is not too big though, compared to the time it takes to load the db fixtures.
Second, it seems that cookies are not preserved across the browser sessions. This might be a good thing because you always start from the same environment. But it also means that you have to simulate user login in the beginning of every test method, or, alternatively, put all assertions in a big single test method rather than breaking it into separate tests.
If you want to share the browser session between all tests, here's solution that worked for me:
1. I changed the definition of CWebDriverTestCase:
IMPORTANT: Please note that it is extending from
CDbTestCaserather than fromCWebTestCase. I could do this because I was writing tests from scratch and was not going to use any of the old Selenium 1 API calls, so I didn't need to extend from PHPUnit_Extension_SeleniumTestCase. Please note that extending fromCWebTestCasewon't work, because PHPUnit will not call setUpBeforeClass() and tearDownAfterClass() in this case.2. Then I modified WebTestCase class in my project like this:
I modified it to extend from CWebDriverTestCase. In your project you can override the host, port and browser in this class if needed. I didn't need to override, so this class is essentially empty.
3. Now I can create test cases like this:
4. A couple more minor changes:
I removed the
<selenium>.. </selenium>section from thephpuni.xmlbecause it won't work properly with the new extension.Finally, I made a little change in the bootstrap.php, because WebTestCase.php needs to be included after the application is created, otherwise 'ext' path alias won't work:
Hope, it helps!
I found the list of implemented API methods here:
extensions/webdriver-bindings/phpwebdriver/status.html
We are waiting for both WebDriver API for PHP providers to merge their code and then we will provide support for this final bindings. I will inform you about new version when available.
Great work!
It would be nice to see which methods are already implemented. I see there's a page place holder here http://code.google.com/p/php-webdriver-bindings/wiki/implemented_methods but it says "todo" as of the moment...
Selenium 1 used Javascript to control browser. It was fine for simple web apps, but the more ajax/other javascript logic was embedded in application the more problems it created.
Webdriver (now integral part of Selenium2) uses native browser extension (each browser needs its own implementation) which is controlled by test script. Webdriver tests script can connect directly to controll browser (for example using FirefoxDriver) or remotely through Selenium Server (using JsonWireProtocol).
Generally WebDriver approach is much faster and reliable.
I am not sure if there was possibility to make screenshots in standard Selenium, but WebDriver provides function to do this (i.e. when test fail). You can find more information at http://google-opensource.blogspot.com/2009/05/introducing-webdriver.html and http://code.google.com/p/php-webdriver-bindings/wiki/DifferenceBetweenWebdriverAndSelenium1?ts=1310633014&updated=DifferenceBetweenWebdriverAndSelenium1
Hi could you please tell us a bit more about the differences between WebDriver and the current WebTestCase ? what are the added values ? is the API richer ?
Leave a comment
Please login to leave your comment.