Laravel4 Module

For additional reference, please review the source

This module allows you to run functional tests for Laravel 4. Module is very fresh and should be improved with Laravel testing capabilities. Please try it and leave your feedbacks. If you want to maintain it - connect Codeception team.

Uses 'bootstrap/start.php' to launch.

Demo Project

https://github.com/Codeception/sample-l4-app

Status

  • Maintainer: Davert
  • Stability: stable
  • Contact: davert.codeception@mailican.com

Config

  • cleanup: boolean, default true - all db queries will be run in transaction, which will be rolled back at the end of test.
  • unit: boolean, default true - Laravel will run in unit testing mode.
  • environment: string, default testing - When running in unit testing mode, we will set a different environment.
  • start: string, default bootstrap/start.php - Relative path to start.php config file.
  • root: string, default - Root path of our application.
  • filters: boolean, default: false - enable or disable filters for testing.

API

  • kernel - Illuminate\Foundation\Application instance
  • client - BrowserKit client

amHttpAuthenticated

Authenticates user for HTTP_AUTH

  • param $username
  • param $password

amLoggedAs

Set the currently logged in user for the application. Takes either UserInterface instance or array of credentials.

  • param \Illuminate\Auth\UserInterface|array $user
  • param string $driver @return void

amOnAction

Opens web page by action name

<?php
$I->amOnAction('PostsController@index');
?>
  • param $action
  • param array $params

amOnPage

Opens the page. Requires relative uri as parameter

Example:

<?php
// opens front page
$I->amOnPage('/');
// opens /register page
$I->amOnPage('/register');
?>
  • param $page

amOnRoute

Opens web page using route name and parameters.

<?php
$I->amOnRoute('posts.create');
?>
  • param $route
  • param array $params

attachFile

Attaches file from Codeception data directory to upload field.

Example:

<?php
// file is stored in 'tests/_data/prices.xls'
$I->attachFile('input[@type="file"]', 'prices.xls');
?>
  • param $field
  • param $filename

checkOption

Ticks a checkbox. For radio buttons use selectOption method.

Example:

<?php
$I->checkOption('#agree');
?>
  • param $option

click

Perform a click on link or button. Link or button are found by their names or CSS selector. Submits a form if button is a submit type.

If link is an image it's found by alt attribute value of image. If button is image button is found by it's value If link or button can't be found by name they are searched by CSS selector.

The second parameter is a context: CSS or XPath locator to narrow the search.

Examples:

<?php
// simple link
$I->click('Logout');
// button of form
$I->click('Submit');
// CSS button
$I->click('#form input[type=submit]');
// XPath
$I->click('//form/*[@type=submit]');
// link in context
$I->click('Logout', '#nav');
// using strict locator
$I->click(['link' => 'Login']);
?>
  • param $link
  • param $context

dontSee

Check if current page doesn't contain the text specified. Specify the css selector to match only specific region.

Examples:

<?php
$I->dontSee('Login'); // I can suppose user is already logged in
$I->dontSee('Sign Up','h1'); // I can suppose it's not a signup page
$I->dontSee('Sign Up','//body/h1'); // with XPath
?>
  • param $text
  • param null $selector

dontSeeAuthentication

Check that user is not authenticated

dontSeeCheckboxIsChecked

Assert if the specified checkbox is unchecked. Use css selector or xpath to match.

Example:

<?php
$I->dontSeeCheckboxIsChecked('#agree'); // I suppose user didn't agree to terms
$I->seeCheckboxIsChecked('#signup_form input[type=checkbox]'); // I suppose user didn't check the first checkbox in form.
?>
  • param $checkbox

dontSeeCookie

Checks that cookie doesn't exist

  • param $cookie

dontSeeCurrentUrlEquals

Checks that current url is not equal to value. Unlike dontSeeInCurrentUrl performs a strict check.

<?php
// current url is not root
$I->dontSeeCurrentUrlEquals('/');
?>
  • param $uri

dontSeeCurrentUrlMatches

Checks that current url does not match a RegEx value

<?php
// to match root url
$I->dontSeeCurrentUrlMatches('~$/users/(\d+)~');
?>
  • param $uri

dontSeeElement

Checks if element does not exist (or is visible) on a page, matching it by CSS or XPath You can also specify expected attributes of this element.

Example:

<?php
$I->dontSeeElement('.error');
$I->dontSeeElement('//form/input[1]');
$I->dontSeeElement('input', ['name' => 'login']);
$I->dontSeeElement('input', ['value' => '123456']);
?>
  • param $selector

dontSeeInCurrentUrl

Checks that current uri does not contain a value

<?php
$I->dontSeeInCurrentUrl('/users/');
?>
  • param $uri

dontSeeInField

Checks that an input field or textarea doesn't contain value. Field is matched either by label or CSS or Xpath Example:

<?php
$I->dontSeeInField('Body','Type your comment here');
$I->dontSeeInField('form textarea[name=body]','Type your comment here');
$I->dontSeeInField('form input[type=hidden]','hidden_value');
$I->dontSeeInField('#searchform input','Search');
$I->dontSeeInField('//form/*[@name=search]','Search');
$I->seeInField(['name' => 'search'], 'Search');
?>
  • param $field
  • param $value

dontSeeInTitle

Checks that page title does not contain text.

  • param $title

dontSeeLink

Checks if page doesn't contain the link with text specified. Specify url to narrow the results.

Examples:

<?php
$I->dontSeeLink('Logout'); // I suppose user is not logged in
?>
  • param $text
  • param null $url

dontSeeOptionIsSelected

Checks if option is not selected in select field.

<?php
$I->dontSeeOptionIsSelected('#form input[name=payment]', 'Visa');
?>
  • param $selector
  • param $optionText

dontSeeRecord

Checks that record does not exist in database.

<?php
$I->dontSeeRecord('users', array('name' => 'davert'));
?>
  • param $model
  • param array $attributes

fillField

Fills a text field or textarea with value.

Example:

<?php
$I->fillField("//input[@type='text']", "Hello World!");
$I->fillField(['name' => 'email'], 'jon@mail.com');
?>
  • param $field
  • param $value

grabAttributeFrom

Grabs attribute value from an element. Fails if element is not found.

<?php
$I->grabAttributeFrom('#tooltip', 'title');
?>
  • param $cssOrXpath
  • param $attribute
  • internal param $element

grabCookie

Grabs a cookie value.

  • param $cookie

grabFromCurrentUrl

Takes a parameters from current URI by RegEx. If no url provided returns full URI.

<?php
$user_id = $I->grabFromCurrentUrl('~$/user/(\d+)/~');
$uri = $I->grabFromCurrentUrl();
?>
  • param null $uri

  • internal param $url

grabRecord

Retrieves record from database

<?php
$category = $I->grabRecord('users', array('name' => 'davert'));
?>
  • param $model
  • param array $attributes

grabService

Return an instance of a class from the IoC Container. (http://laravel.com/docs/ioc)

Example

<?php
// In Laravel
App::bind('foo', function($app)
{
    return new FooBar;
});

// Then in test
$service = $I->grabService('foo');

// Will return an instance of FooBar, also works for singletons.
?>
  • param string $class

grabTextFrom

Finds and returns text contents of element. Element is searched by CSS selector, XPath or matcher by regex.

Example:

<?php
$heading = $I->grabTextFrom('h1');
$heading = $I->grabTextFrom('descendant-or-self::h1');
$value = $I->grabTextFrom('~<input value=(.*?)]~sgi');
?>
  • param $cssOrXPathOrRegex

grabValueFrom

  • param $field

@return array|mixed|null|string

haveDisabledFilters

Disable Laravel filters for next requests.

haveEnabledFilters

Enable Laravel filters for next requests.

haveRecord

Inserts record into the database.

<?php
$user_id = $I->haveRecord('users', array('name' => 'Davert'));
?>
  • param $model
  • param array $attributes

logout

Logs user out

resetCookie

Unsets cookie

  • param $cookie

see

Check if current page contains the text specified. Specify the css selector to match only specific region.

Examples:

<?php
$I->see('Logout'); // I can suppose user is logged in
$I->see('Sign Up','h1'); // I can suppose it's a signup page
$I->see('Sign Up','//body/h1'); // with XPath
?>
  • param $text
  • param null $selector

seeAuthentication

Checks that user is authenticated

seeCheckboxIsChecked

Assert if the specified checkbox is checked. Use css selector or xpath to match.

Example:

<?php
$I->seeCheckboxIsChecked('#agree'); // I suppose user agreed to terms
$I->seeCheckboxIsChecked('#signup_form input[type=checkbox]'); // I suppose user agreed to terms, If there is only one checkbox in form.
$I->seeCheckboxIsChecked('//form/input[@type=checkbox and @name=agree]');
?>
  • param $checkbox

seeCookie

Checks that cookie is set.

  • param $cookie

seeCurrentActionIs

Checks that current url matches action

<?php
$I->seeCurrentActionIs('PostsController@index');
?>
  • param $action
  • param array $params

seeCurrentRouteIs

Checks that current url matches route

<?php
$I->seeCurrentRouteIs('posts.index');
?>
  • param $route
  • param array $params

seeCurrentUrlEquals

Checks that current url is equal to value. Unlike seeInCurrentUrl performs a strict check.

<?php
// to match root url
$I->seeCurrentUrlEquals('/');
?>
  • param $uri

seeCurrentUrlMatches

Checks that current url is matches a RegEx value

<?php
// to match root url
$I->seeCurrentUrlMatches('~$/users/(\d+)~');
?>
  • param $uri

seeElement

Checks if element exists on a page, matching it by CSS or XPath. You can also specify expected attributes of this element.

<?php
$I->seeElement('.error');
$I->seeElement('//form/input[1]');
$I->seeElement('input', ['name' => 'login']);
$I->seeElement('input', ['value' => '123456']);

// strict locator in first arg, attributes in second
$I->seeElement(['css' => 'form input'], ['name' => 'login']);
?>
  • param $selector
  • param array $attributes @return

seeInCurrentUrl

Checks that current uri contains a value

<?php
// to match: /home/dashboard
$I->seeInCurrentUrl('home');
// to match: /users/1
$I->seeInCurrentUrl('/users/');
?>
  • param $uri

seeInField

Checks that an input field or textarea contains value. Field is matched either by label or CSS or Xpath

Example:

<?php
$I->seeInField('Body','Type your comment here');
$I->seeInField('form textarea[name=body]','Type your comment here');
$I->seeInField('form input[type=hidden]','hidden_value');
$I->seeInField('#searchform input','Search');
$I->seeInField('//form/*[@name=search]','Search');
$I->seeInField(['name' => 'search'], 'Search');
?>
  • param $field
  • param $value

seeInSession

Assert that the session has a given list of values.

  • param string|array $key
  • param mixed $value @return void

seeInTitle

Checks that page title contains text.

<?php
$I->seeInTitle('Blog - Post #1');
?>
  • param $title

seeLink

Checks if there is a link with text specified. Specify url to match link with exact this url.

Examples:

<?php
$I->seeLink('Logout'); // matches <a href="#">Logout</a>
$I->seeLink('Logout','/logout'); // matches <a href="/logout">Logout</a>
?>
  • param $text
  • param null $url

seeNumberOfElements

Tests number of $elements on page

<?php
$I->seeNumberOfElements('tr', 10);
$I->seeNumberOfElements('tr', [0,10]); //between 0 and 10 elements
?>
  • param $selector
  • param mixed $expected:
  • string: strict number
  • array: range of numbers [0,10]

seeOptionIsSelected

Checks if option is selected in select field.

<?php
$I->seeOptionIsSelected('#form input[name=payment]', 'Visa');
?>
  • param $selector
  • param $optionText

seePageNotFound

Asserts that current page has 404 response status code.

seeRecord

Checks that record exists in database.

$I->seeRecord('users', array('name' => 'davert'));
  • param $model
  • param array $attributes

seeResponseCodeIs

Checks that response code is equal to value provided.

  • param $code

seeSessionErrorMessage

Assert that Session has error messages The seeSessionHasValues cannot be used, as Message bag Object is returned by Laravel4

Useful for validation messages and generally messages array e.g. return Redirect::to('register')->withErrors($validator);

Example of Usage

<?php
$I->seeSessionErrorMessage(array('username'=>'Invalid Username'));
?>
  • param array $bindings

seeSessionHasErrors

Assert that the session has errors bound.

@return bool

seeSessionHasValues

Assert that the session has a given list of values.

  • param array $bindings @return void

selectOption

Selects an option in select tag or in radio button group.

Example:

<?php
$I->selectOption('form select[name=account]', 'Premium');
$I->selectOption('form input[name=payment]', 'Monthly');
$I->selectOption('//form/select[@name=account]', 'Monthly');
?>

Can select multiple options if second argument is array:

<?php
$I->selectOption('Which OS do you use?', array('Windows','Linux'));
?>
  • param $select
  • param $option

sendAjaxGetRequest

If your page triggers an ajax request, you can perform it manually. This action sends a GET ajax request with specified params.

See ->sendAjaxPostRequest for examples.

  • param $uri
  • param $params

sendAjaxPostRequest

If your page triggers an ajax request, you can perform it manually. This action sends a POST ajax request with specified params. Additional params can be passed as array.

Example:

Imagine that by clicking checkbox you trigger ajax request which updates user settings. We emulate that click by running this ajax request manually.

<?php
$I->sendAjaxPostRequest('/updateSettings', array('notifications' => true)); // POST
$I->sendAjaxGetRequest('/updateSettings', array('notifications' => true)); // GET
  • param $uri
  • param $params

sendAjaxRequest

If your page triggers an ajax request, you can perform it manually. This action sends an ajax request with specified method and params.

Example:

You need to perform an ajax request specifying the HTTP method.

<?php
$I->sendAjaxRequest('PUT', /posts/7', array('title' => 'new title');
  • param $method
  • param $uri
  • param $params

setCookie

Sets a cookie.

  • param $cookie
  • param $value

submitForm

Submits a form located on page. Specify the form by it's css or xpath selector. Fill the form fields values as array.

Skipped fields will be filled by their values from page. You don't need to click the 'Submit' button afterwards. This command itself triggers the request to form's action.

Examples:

<?php
$I->submitForm('#login', array('login' => 'davert', 'password' => '123456'));

For sample Sign Up form:

<form action="/sign_up">
    Login: <input type="text" name="user[login]" /><br/>
    Password: <input type="password" name="user[password]" /><br/>
    Do you agree to out terms? <input type="checkbox" name="user[agree]" /><br/>
    Select pricing plan <select name="plan"><option value="1">Free</option><option value="2" selected="selected">Paid</option></select>
    <input type="submit" value="Submit" />
</form>

I can write this:

<?php
$I->submitForm('#userForm', array('user' => array('login' => 'Davert', 'password' => '123456', 'agree' => true)));

Note, that pricing plan will be set to Paid, as it's selected on page.

  • param $selector
  • param $params

uncheckOption

Unticks a checkbox.

Example:

<?php
$I->uncheckOption('#notify');
?>
  • param $option

 

Module reference is taken from the source code. Help us to improve documentation. Edit module reference