2014-10-17 06:53:52 +02:00
title: How to write a FunctionalTest
# How to Write a FunctionalTest
2017-07-03 03:22:12 +02:00
[FunctionalTest ](api:SilverStripe\Dev\FunctionalTest ) test your applications `Controller` instances and anything else which requires a web request. The
core of these tests are the same as `SapphireTest` unit tests but add several methods for creating [HTTPRequest ](api:SilverStripe\Control\HTTPRequest )
and receiving [HTTPResponse ](api:SilverStripe\Control\HTTPResponse ) objects. In this How To, we'll see how to write a test to query a page, check the
2014-10-17 06:53:52 +02:00
response and modify the session within a test.
**mysite/tests/HomePageTest.php**
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Security\Member;
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
class HomePageTest extends FunctionalTest
{
/**
* Test generation of the view
*/
public function testViewHomePage()
2017-08-07 05:11:17 +02:00
{
2017-10-27 04:38:27 +02:00
$page = $this->get('home/');
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
// Home page should load..
$this->assertEquals(200, $page->getStatusCode());
// We should see a login form
$login = $this->submitForm("LoginFormID", null, [
'Email' => 'test@test.com',
'Password' => 'wrongpassword'
]);
// wrong details, should now see an error message
$this->assertExactHTMLMatchBySelector("#LoginForm p.error", [
"That email address is invalid."
]);
2017-08-03 05:35:09 +02:00
2017-10-27 04:38:27 +02:00
// If we login as a user we should see a welcome message
$me = Member::get()->first();
$this->logInAs($me);
$page = $this->get('home/');
$this->assertExactHTMLMatchBySelector("#Welcome", [
'Welcome Back'
]);
}
}
2017-08-03 02:51:32 +02:00
```
2014-10-17 06:53:52 +02:00
## Related Documentation
* [Functional Testing ](../functional_testing )
* [Unit Testing ](../unit_testing )
## API Documentation
2017-07-03 03:22:12 +02:00
* [FunctionalTest ](api:SilverStripe\Dev\FunctionalTest )