2014-10-17 06:53:52 +02:00
title: Functional Testing
summary: Test controllers, forms and HTTP responses.
# Functional Testing
2017-07-03 03:22:12 +02:00
[FunctionalTest ](api:SilverStripe\Dev\FunctionalTest ) test your applications `Controller` logic and anything else which requires a web request. The
2014-10-17 06:53:52 +02:00
core idea of these tests is the same as `SapphireTest` unit tests but `FunctionalTest` adds several methods for
2017-07-03 03:22:12 +02:00
creating [HTTPRequest ](api:SilverStripe\Control\HTTPRequest ), receiving [HTTPResponse ](api:SilverStripe\Control\HTTPResponse ) objects and modifying the current user session.
2014-10-17 06:53:52 +02:00
## Get
2017-08-03 02:51:32 +02:00
```php
2014-10-17 06:53:52 +02:00
$page = $this->get($url);
2017-08-03 02:51:32 +02:00
```
2017-07-03 03:22:12 +02:00
Performs a GET request on $url and retrieves the [HTTPResponse ](api:SilverStripe\Control\HTTPResponse ). This also changes the current page to the value
2014-10-17 06:53:52 +02:00
of the response.
## Post
2017-08-03 02:51:32 +02:00
```php
2014-10-17 06:53:52 +02:00
$page = $this->post($url);
2017-08-03 02:51:32 +02:00
```
2017-07-03 03:22:12 +02:00
Performs a POST request on $url and retrieves the [HTTPResponse ](api:SilverStripe\Control\HTTPResponse ). This also changes the current page to the value
2014-10-17 06:53:52 +02:00
of the response.
## Submit
2017-08-03 02:51:32 +02:00
```php
2014-10-17 06:53:52 +02:00
$submit = $this->submitForm($formID, $button = null, $data = array());
2017-08-03 02:51:32 +02:00
```
2014-10-17 06:53:52 +02:00
2017-07-03 03:22:12 +02:00
Submits the given form (`#ContactForm`) on the current page and returns the [HTTPResponse ](api:SilverStripe\Control\HTTPResponse ).
2014-10-17 06:53:52 +02:00
## LogInAs
2017-08-03 02:51:32 +02:00
```php
2014-10-17 06:53:52 +02:00
$this->logInAs($member);
2017-08-03 02:51:32 +02:00
```
2014-10-17 06:53:52 +02:00
Logs a given user in, sets the current session. To log all users out pass `null` to the method.
2017-08-03 02:51:32 +02:00
```php
2014-10-17 06:53:52 +02:00
$this->logInAs(null);
2017-08-03 02:51:32 +02:00
```
2014-10-17 06:53:52 +02:00
## Assertions
The `FunctionalTest` class also provides additional asserts to validate your tests.
### assertPartialMatchBySelector
2017-08-03 02:51:32 +02:00
```php
2014-10-17 06:53:52 +02:00
$this->assertPartialMatchBySelector('p.good',array(
'Test save was successful'
));
2017-08-03 02:51:32 +02:00
```
2014-10-17 06:53:52 +02:00
Asserts that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS
selector will be applied to the HTML of the most recent page. The content of every matching tag will be examined. The
assertion fails if one of the expectedMatches fails to appear.
### assertExactMatchBySelector
2017-08-03 02:51:32 +02:00
```php
2014-10-17 06:53:52 +02:00
$this->assertExactMatchBySelector("#MyForm_ID p.error", array(
"That email address is invalid."
));
2017-08-03 02:51:32 +02:00
```
2014-10-17 06:53:52 +02:00
Asserts that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS
selector will be applied to the HTML of the most recent page. The full HTML of every matching tag will be examined. The
assertion fails if one of the expectedMatches fails to appear.
### assertPartialHTMLMatchBySelector
2017-08-03 02:51:32 +02:00
```php
2014-10-17 06:53:52 +02:00
$this->assertPartialHTMLMatchBySelector("#MyForm_ID p.error", array(
"That email address is invalid."
));
2017-08-03 02:51:32 +02:00
```
2014-10-17 06:53:52 +02:00
Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS
selector will be applied to the HTML of the most recent page. The content of every matching tag will be examined. The
assertion fails if one of the expectedMatches fails to appear.
< div class = "notice" markdown = "1" >
`&nbsp;` characters are stripped from the content; make sure that your assertions take this into account.
< / div >
### assertExactHTMLMatchBySelector
2017-08-03 02:51:32 +02:00
```php
2014-10-17 06:53:52 +02:00
$this->assertExactHTMLMatchBySelector("#MyForm_ID p.error", array(
"That email address is invalid."
));
2017-08-03 02:51:32 +02:00
```
2014-10-17 06:53:52 +02:00
Assert that the most recently queried page contains a number of content tags specified by a CSS selector. The given CSS
selector will be applied to the HTML of the most recent page. The full HTML of every matching tag will be examined. The
assertion fails if one of the expectedMatches fails to appear.
< div class = "notice" markdown = "1" >
`&nbsp;` characters are stripped from the content; make sure that your assertions take this into account.
< / div >
## Related Documentation
* [How to write a FunctionalTest ](how_tos/write_a_functionaltest )
## API Documentation
2017-07-03 03:22:12 +02:00
* [FunctionalTest ](api:SilverStripe\Dev\FunctionalTest )