Testomatio - Test Management for Codeception
This module provides integration with Yii framework (2.0).
It initializes the Yii framework in a test environment and provides actions for functional testing.
This section details what you can expect when using this module.
\Yii::$app
at the start of each test
(available in the test and in _before()
).recreateApplication
.request
and response
component are both recreated.configFile
required - path to the application config file. The file
should be configured for the test environment and return a configuration
array.applicationClass
- Fully qualified class name for the application. There are
several ways to define the application class. Either via a class
key in the Yii
config, via specifying this codeception module configuration value or let codeception
use its default value yii\web\Application
. In a standard Yii application, this
value should be either yii\console\Application
, yii\web\Application
or unset.entryUrl
- initial application url (default: http://localhost/index-test.php).entryScript
- front script title (like: index-test.php). If not set itâs
taken from entryUrl
.transaction
- (default: true
) wrap all database connection inside a
transaction and roll it back after the test. Should be disabled for
acceptance testing.cleanup
- (default: true
) cleanup fixtures after the testignoreCollidingDSN
- (default: false
) When 2 database connections use
the same DSN but different settings an exception will be thrown. Set this to
true to disable this behavior.fixturesMethod
- (default: _fixtures
) Name of the method used for
creating fixtures.responseCleanMethod
- (default: clear
) Method for cleaning the
response object. Note that this is only for multiple requests inside a
single test case. Between test cases the whole application is always
recreated.requestCleanMethod
- (default: recreate
) Method for cleaning the
request object. Note that this is only for multiple requests inside a single
test case. Between test cases the whole application is always recreated.recreateComponents
- (default: []
) Some components change their state
making them unsuitable for processing multiple requests. In production
this is usually not a problem since web apps tend to die and start over
after each request. This allows you to list application components that
need to be recreated before each request. As a consequence, any
components specified here should not be changed inside a test since those
changes will get discarded.recreateApplication
- (default: false
) whether to recreate the whole
application before each requestYou can use this module by setting params in your functional.suite.yml
:
By default all available methods are loaded, but you can also use the part
option to select only the needed actions and to avoid conflicts. The
available parts are:
init
- use the module only for initialization (for acceptance tests).orm
- include only haveRecord/grabRecord/seeRecord/dontSeeRecord
actions.fixtures
- use fixtures inside tests with haveFixtures/grabFixture/grabFixtures
actions.email
- include email actions seeEmailsIsSent/grabLastSentEmail/...
See WebDriver module for general information on how to load parts of a framework module.
acceptance.suite.yml
)This module allows to use fixtures inside a test. There are two ways to do that. Fixtures can either be loaded with the haveFixtures method inside a test:
or, if you need to load fixtures before the test, you
can specify fixtures in the _fixtures
method of a test case:
With this module you can also use Yii2âs URL format for all codeception commands that expect a URL:
Maintainer: samdark Stability: stable
@property \Codeception\Lib\Connector\Yii2 $client
hidden API method, expected to be used from Helper classes
api
param mixed
$locatorreturn iterable
Locates element using available Codeception locator types:
Use it in Helpers or GroupObject or Extension classes:
WebDriver module returns Facebook\WebDriver\Remote\RemoteWebElement
instances
PhpBrowser and Framework modules return Symfony\Component\DomCrawler\Crawler
instances
hidden API method, expected to be used from Helper classes
api
throws ModuleException
return string
Returns content of the last response Use it in Helpers when you want to retrieve response of request performed by another module.
hidden API method, expected to be used from Helper classes
api
param string
$methodparam string
$uriparam array
$parametersparam array
$filesparam array
$serverparam ?string
$contentreturn void
Opens a page with arbitrary request parameters.
Useful for testing multi-step forms on a specific step.
hidden API method, expected to be used from Helper classes
api
see
_loadPage
param string
$methodparam string
$uriparam array
$parametersparam array
$filesparam array
$serverparam ?string
$contentthrows ExternalUrlException|ModuleException
return ?string
Send custom request to a backend using method, uri, parameters, etc.
Use it in Helpers to create special request actions, like accessing API Returns a string with response body.
Does not load the response into the module so you canât interact with response page (click, fill forms).
To load arbitrary page for interaction, use _loadPage
method.
hidden API method, expected to be used from Helper classes
api
param string
$filenamereturn void
Saves page source of to a file
param string
$usernameparam string
$passwordreturn void
Authenticates user for HTTP_AUTH
param
$userthrows \Codeception\Exception\ModuleException
Authenticates a user on a site without submitting a login form.
Use it for fast pragmatic authorization in functional tests.
Requires the user
component to be enabled and configured.
param string
$page the page URIreturn void
Opens the page for the given relative URI or route.
param string
$route A routeparam array
$params Additional route parametersreturn void
Similar to amOnPage
but accepts a route as first argument and params as second
param
$fieldparam string
$filenamereturn void
Attaches a file relative to the Codeception _data
directory to the given file upload field.
param
$optionreturn void
Ticks a checkbox. For radio buttons, use the selectOption
method instead.
param string|array
$linkparam
$contextreturn void
Perform a click on a link or a button, given by a locator.
If a fuzzy locator is given, the page will be searched for a button, link, or image matching the locator string. For buttons, the âvalueâ attribute, ânameâ attribute, and inner text are searched. For links, the link text is searched. For images, the âaltâ attribute and inner text of any parent links are searched.
The second parameter is a context (CSS or XPath locator) to narrow the search.
Note that if the locator matches a button of type submit
, the form will be submitted.
param string
$val The value of the CSRF tokenreturn string[]
Returns an array containing the name of the CSRF param and the masked CSRF token.Creates the CSRF Cookie.
@deprecated
param string
$namereturn void
param string
$textparam array|string
$selector optionalreturn void
Checks that the current page doesnât contain the text specified (case insensitive).
Give a locator as the second parameter to match a specific region.
Note that the search is done after stripping all HTML tags from the body,
so $I->dontSee('strong')
will fail on strings like:
<p>I am Stronger than thou</p>
<script>document.createElement('strong');</script>
But will ignore strings like:
<strong>Home</strong>
<div class="strong">Home</strong>
<!-- strong -->
For checking the raw source code, use seeInSource()
.
param
$checkboxreturn void
Check that the specified checkbox is unchecked.
param
$cookieparam
$paramsreturn mixed|void
Checks that there isnât a cookie with the given name.
You can set additional cookie params like domain
, path
as array passed in last argument.
param string
$urireturn void
Checks that the current URL doesnât equal the given string.
Unlike dontSeeInCurrentUrl
, this only matches the full URL.
param string
$urireturn void
Checks that current url doesnât match the given regular expression.
param
$selectorparam array
$attributesreturn void
Checks that the given element is invisible or not present on the page.
You can also specify expected attributes of this element.
part
emailreturn void
Checks that no email was sent
param string
$urireturn void
Checks that the current URI doesnât contain the given string.
param string|array
$fieldparam
$valuereturn void
Checks that an input field or textarea doesnât contain the given value.
For fuzzy locators, the field is matched by label text, CSS and XPath.
param
$formSelectorparam array
$paramsreturn void
Checks if the array of form parameters (name => value) are not set on the form matched with the passed selector.
To check that an element hasnât been assigned any one of many values, an array can be passed as the value:
Additionally, checkbox values can be checked with a boolean.
param string
$rawreturn void
Checks that the current page contains the given string in its raw source code.
param
$titlereturn mixed|void
Checks that the page title does not contain the given string.
param string
$textparam string
$urlreturn void
Checks that the page doesnât contain a link with the given string.
If the second parameter is given, only links with a matching âhrefâ attribute will be checked.
param
$selectorparam
$optionTextreturn mixed|void
Checks that the given option is not selected.
part
ormparam
$modelparam array
$attributesreturn void
Checks that a record does not exist in the database.
param int
$codereturn void
Checks that response code is equal to value provided.
param
$fieldparam
$valuereturn void
Fills a text field or textarea with the given string.
return void
Follow pending redirect if there is one.
return array
Returns a list of regex patterns for recognized domain names
param
$cssOrXpathparam string
$attributereturn mixed
Returns the value of the given attribute value from the given HTML element. For some attributes, the string true
is returned instead of their literal value (e.g. disabled="disabled"
or required="required"
).
Fails if the element is not found. Returns null
if the attribute is not present on the element.
@deprecated
param
$componentthrows \Codeception\Exception\ModuleException
return mixed
Gets a component from the Yii container. Throws an exception if the component is not available
param string
$cookieparam array
$paramsreturn mixed
Grabs a cookie value.
You can set additional cookie params like domain
, path
in array passed as last argument.
If the cookie is set by an ajax request (XMLHttpRequest), there might be some delay caused by the browser, so try $I->wait(0.1)
.
part
fixturesparam
$nameparam
$indexthrows \Codeception\Exception\ModuleException
if the fixture is not foundreturn mixed
Gets a fixture by name.
Returns a Fixture instance. If a fixture is an instance of
\yii\test\BaseActiveFixture
a second parameter can be used to return a
specific model:
part
fixturesreturn array
Returns all loaded fixtures.
Array of fixture instances
param ?string
$urireturn mixed
Executes the given regular expression against the current URI and returns the first capturing group.
If no parameters are provided, the full URI is returned.
part
emailreturn object
Returns the last sent email:
param
$cssOrXpathparam ?string
$attributereturn string[]
Grabs either the text content, or attribute values, of nodes matched by $cssOrXpath and returns them as an array.
throws \Codeception\Exception\ModuleException
if no page was opened.return string
Current page source code.Grabs current page source code.
part
ormparam
$modelparam array
$attributesreturn mixed
Retrieves a record from the database
part
emailthrows \Codeception\Exception\ModuleException
return array
Returns array of all sent email messages.
Each message implements the yii\mail\MessageInterface
interface.
Useful to perform additional checks using the Asserts
module:
param
$cssOrXPathOrRegexreturn mixed
Finds and returns the text contents of the given element.
If a fuzzy locator is used, the element is found using CSS, XPath, and by matching the full page source by regular expression.
param
$fieldreturn mixed
Finds the value for the given form field.
If a fuzzy locator is used, the field is found by field name, CSS, and XPath.
part
fixturesparam
$fixturesCreates and loads fixtures from a config.
The signature is the same as for the fixtures()
method of yii\test\FixtureTrait
Note: if you need to load fixtures before a test (probably before the
cleanup transaction is started; cleanup
option is true
by default),
you can specify the fixtures in the _fixtures()
method of a test case
instead of calling haveFixtures
in Cest _before
param string
$name the name of the request headerparam string
$value the value to set it to for subsequent
requestsreturn void
Sets the HTTP header to the passed value - which is used on subsequent HTTP requests through PhpBrowser.
Example:
To use special chars in Header Key use HTML Character Entities: Example: Header with underscore - âClient_Idâ should be represented as - âClient_Idâ or âClient_Idâ
part
ormparam
$modelparam array
$attributesreturn mixed
Inserts a record into the database.
param string
$nameparam string
$valuereturn void
Sets SERVER parameter valid for all next requests.
param ?string
$namereturn void
Use this method within an interactive pause to save the HTML source code of the current page.
param int
$numberOfSteps (default value 1)return void
Moves back in history.
param
$cookieparam
$paramsreturn mixed|void
Unsets cookie with the given name.
You can set additional cookie params like domain
, path
in array passed as last argument.
param string
$textparam array|string
$selector optionalreturn void
Checks that the current page contains the given string (case insensitive).
You can specify a specific HTML element (via CSS or XPath) as the second parameter to only search within that element.
Note that the search is done after stripping all HTML tags from the body,
so $I->see('strong')
will return true for strings like:
<p>I am Stronger than thou</p>
<script>document.createElement('strong');</script>
But will not be true for strings like:
<strong>Home</strong>
<div class="strong">Home</strong>
<!-- strong -->
For checking the raw source code, use seeInSource()
.
param
$checkboxreturn void
Checks that the specified checkbox is checked.
param
$cookieparam
$paramsreturn mixed|void
Checks that a cookie with the given name is set.
You can set additional cookie params like domain
, path
as array passed in last argument.
param string
$urireturn void
Checks that the current URL is equal to the given string.
Unlike seeInCurrentUrl
, this only matches the full URL.
param string
$urireturn void
Checks that the current URL matches the given regular expression.
param
$selectorparam array
$attributesreturn void
Checks that the given element exists on the page and is visible.
You can also specify expected attributes of this element.
Only works if <html>
tag is present.
part
emailparam int
$numthrows \Codeception\Exception\ModuleException
return void
Checks that an email is sent.
param string
$urireturn void
Checks that current URI contains the given string.
param string|array
$fieldparam
$valuereturn void
Checks that the given input field or textarea equals (i.e. not just contains) the given value.
Fields are matched by label text, the ânameâ attribute, CSS, or XPath.
param
$formSelectorparam array
$paramsreturn void
Checks if the array of form parameters (name => value) are set on the form matched with the passed selector.
For multi-select elements, or to check values of multiple elements with the same name, an array may be passed:
Additionally, checkbox values can be checked with a boolean.
Pair this with submitForm for quick testing magic.
param string
$rawreturn void
Checks that the current page contains the given string in its raw source code.
param
$titlereturn mixed|void
Checks that the page title contains the given string.
param string
$textparam ?string
$urlreturn void
Checks that thereâs a link with the specified text.
Give a full URL as the second parameter to match links with that exact URL.
param
$selectorparam int|int[]
$expectedreturn void
Checks that there are a certain number of elements matched by the given locator on the page.
param
$selectorparam
$optionTextreturn mixed|void
Checks that the given option is selected.
return void
Asserts that current page has 404 response status code.
part
ormparam
$modelparam array
$attributesreturn void
Checks that a record exists in the database.
param int
$codereturn void
Checks that response code is equal to value provided.
param int
$fromparam int
$toreturn void
Checks that response code is between a certain range. Between actually means [from <= CODE <= to]
return void
Checks that the response code is 4xx
return void
Checks that the response code 3xx
return void
Checks that the response code is 5xx
return void
Checks that the response code 2xx
param
$selectparam
$optionreturn void
Selects an option in a select tag or in radio button group.
Provide an array for the second argument to select multiple options:
Or provide an associative array for the second argument to specifically define which selection method should be used:
param string
$uriparam array
$paramsreturn void
Sends an ajax GET request with the passed parameters.
See sendAjaxPostRequest()
param string
$uriparam array
$paramsreturn void
Sends an ajax POST request with the passed parameters.
The appropriate HTTP header is added automatically:
X-Requested-With: XMLHttpRequest
Example:
Some frameworks (e.g. Symfony) create field names in the form of an âarrayâ:
<input type="text" name="form[task]">
In this case you need to pass the fields like this:
param string
$methodparam string
$uriparam array
$paramsreturn void
Sends an ajax request, using the passed HTTP method.
See sendAjaxPostRequest()
Example:
param string
$name The name of the cookieparam string
$val The value of the cookieparam array
$params Additional cookie params like domain
, path
, expires
and secure
.Sets a cookie and, if validation is enabled, signs it.
param int
$maxRedirectsreturn void
Sets the maximum number of redirects that the Client can follow.
param array
$paramsreturn void
Sets SERVER parameters valid for all next requests.
this will remove old ones.
return void
Enables automatic redirects to be followed by the client.
return void
Prevents automatic redirects to be followed by the client.
param
$selectorparam array
$paramsparam ?string
$buttonreturn void
Submits the given form on the page, with the given form values. Pass the form fieldâs values as an array in the second parameter.
Although this function can be used as a short-hand version of
fillField()
, selectOption()
, click()
etc. it has some important
differences:
fillField()
or selectOption()
with
a missing field.Fields that are not provided will be filled by their values from the page,
or from any previous calls to fillField()
, selectOption()
etc.
You donât need to click the âSubmitâ button afterwards.
This command itself triggers the request to formâs action.
You can optionally specify which buttonâs value to include in the request with the last parameter (as an alternative to explicitly setting its value in the second parameter), as button values are not otherwise included in the request.
Examples:
For example, given this sample âSign Upâ form:
You could write the following to submit it:
Note that â2â will be the submitted value for the âplanâ field, as it is the selected option.
To uncheck the pre-checked checkbox ânewsletterâ, call $I->uncheckOption(['name' => 'user[newsletter]']);
before,
then submit the form as shown here (i.e. without the ânewsletterâ field in the $params
array).
You can also emulate a JavaScript submission by not specifying any buttons in the third parameter to submitForm.
This function works well when paired with seeInFormFields()
for quickly testing CRUD interfaces and form validation logic.
Parameter values can be set to arrays for multiple input fields
of the same name, or multi-select combo boxes. For checkboxes,
you can use either the string value or boolean true
/false
which will
be replaced by the checkboxâs value in the DOM.
Mixing string and boolean values for a checkboxâs value is not supported and may produce unexpected results.
Field names ending in []
must be passed without the trailing square
bracket characters, and must contain an array for its value. This allows
submitting multiple values with the same name, consider:
The solution is to pass an array value:
param string
$namereturn void
Switch to iframe or frame on the page.
Example:
param
$optionreturn void
Unticks a checkbox.
param string
$name the name of the header to unset.return void
Unsets a HTTP header (that was originally added by haveHttpHeader()), so that subsequent requests will not send it anymore.
Example: