New package unittest.web
Status: implemented, Sat Aug 23 12:20:04 2008 Created: 2008-07-13 12:38:18+0200 Categories:
unittest Author:
friebe
Scope of Change
A new package "unittest.web" will be created. It will contain the base
class "WebTestCase" with which web tests can be written.
Rationale
Integration testing.
Functionality
As with regular unittests, web tests are created by extending a base class,
in this case unittest.web.WebTestCase .
Example
class PlanetXpTestCase extends WebTestCase { protected function getConnection ( $url = NULL ) { return new HttpConnection ( $url ? $url : 'http://planet-xp.net/' ) ; } #[@test] public function homePage () { $this ->beginAt ( '/xml/home' ) ; $this ->assertStatus ( HTTP_OK ) ; $this ->assertTitleEquals ( 'XP Technology' ) ; $this ->assertTextPresent ( 'Credits' ) ; } }
Assertion Methods
In addition to the assertion methods from the unittest.TestCase
class (which the WebTestCase class extends), the following assertion
methods are available:
public function assertStatus ( $status , $message = 'not_equals' ) ; public function assertUrlEquals ( URL $url , $message = 'not_equals' ) ; public function assertContentType ( $ctype , $message = 'not_equals' ) ; public function assertHeader ( $header , $value , $message = 'not_equals' ) ; public function assertElementPresent ( $id , $message = 'not_present' ) ; public function assertElementNotPresent ( $id , $message = 'present' ) ; public function assertTextPresent ( $text , $message = 'not_present' ) ; public function assertTextNotPresent ( $text , $message = 'present' ) ; public function assertImagePresent ( $src , $message = 'not_present' ) ; public function assertImageNotPresent ( $src , $message = 'present' ) ; public function assertLinkPresent ( $url , $message = 'not_present' ) ; public function assertLinkNotPresent ( $url , $message = 'present' ) ; public function assertLinkPresentWithText ( $text , $message = 'not_present' ) ; public function assertLinkNotPresentWithText ( $text , $message = 'present' ) ; public function assertFormPresent ( $name = NULL, $message = 'not_present' ) ; public function assertFormNotPresent ( $name = NULL, $message = 'present' ) ; public function assertTitleEquals ( $title , $message = 'not_equals' ) ;
Navigation
To follow links inside a page, a web test can utilize the click methods:
protected function clickLink ( $id ) ; protected function clickLinkWithText ( $text ) ;
Forms
To work with forms, the getForm() method can be used:
class XpSearchTestCase extends WebTestCase { protected function getConnection ( $url = NULL ) { return new HttpConnection ( $url ? $url : 'http://planet-xp.net/' ) ; } #[@test] public function searchFunction () { $this ->beginAt ( '/xml/home' ) ; $form = $this ->getForm () ; $form ->getField ( 'query' ) ->setValue ( 'Unittest' ) ; $form ->submit () ; $this ->assertStatus ( HTTP_OK ) ; $this ->assertTitleEquals ( 'Search for "Unittest" - XP Framework' ) ; } }
Security considerations
n/a
Speed impact
No existing infrastructure is changed, thus none.
Dependencies
RFC #0162 (implemented)
Related documents
Comments
<EOF>