As a follow up of my blog from yesterday which contained information howto get codeception and Laravel playing nicely together I tought it would be nice to make a small AcceptanceTest of a Login page to see how this actually works.
Lets start with generating the test, fire up your console and type:
./vendor/bin/codecept generate:cept acceptance App/Http/Controllers/Auth/AuthController
Open up your favorite IDE or just vi in your console. Navigate to your project root /acceptance/App/Http/Controllers/Auth/AuthControllerCept.php
You will see that there are some lines of code in here already, I altered the wantTo, check it:
<?php $I = new AcceptanceTester($scenario); $I->wantTo('See if the login page contains the necessary fields');
Now what I want to do is to visit the login page of my application to check if it
- Actually has an email field
- Actually has an password field
- Actually has an submit button
- Has a title
- In the code somewhere a crsf field is located
So first of all, we need to check if we can reach the login page. As Laravel has a beautiful function that generates the whole Auth stuff for you, normally our page will be located at route http://url.ext/login (If you don’t have this stuff yet, in your console execute “`php artisan generate:auth“`)
Lets check if we can navigate there:
Next, lets check if the page has an input named email and an input of type password, you can check on kinds of attributes with this seeElement function:
$I->seeElement('input', ['name' => 'email']); $I->seeElement('input', ['type' => 'password']);
Well, since we are checking elements out, we ofcourse always need an submit button;
$I->seeElement('button', ['type' => 'submit']);
What more could we check? Well, for instance if there is an Password forgotten link..
$I->seeLink('Forgot your password?'); // matches <a href="#">Forgot your password?</a>
And the title, that would be cool to test right?
Last but not least, is our csrf token somewhere?
$I->seeInSource("<input type=\"hidden\" name=\"_token\" value=");
Well, lets try to run this test now! In your console:
And BOOM errors!
Why? Well… cause we forgot to configure one thing.. Open up your acceptance.suite.yml within the tests folder and change the line
to your own url!
Lets try to run it again.
And again, an other error.
This is a fairly easy one, CodeCeption is quite strict when it comes to Capitals, change
$I->seeLink('Forgot your password?'); // To: $I->seeLink('Forgot Your Password?');
Run it for the last time, and here we are, it works.
Thats it for this post, I will continue on using wrong credentials and actually logging in in the near future ;-)
Questions? -> Comment!