hidden API method, expected to be used from Helper classes
param mixed $locator
return iterable
Locates element using available Codeception locator types:
XPath
Strict Locator
Use it in Helpers or GroupObject or Extension classes:
$els=$this->getModule('PhpBrowser')->_findElements('.items');$els=$this->getModule('PhpBrowser')->_findElements(['name'=>'username']);$editLinks=$this->getModule('PhpBrowser')->_findElements(['link'=>'Edit']);// now you can iterate over $editLinks and check that all them have valid hrefs
hidden API method, expected to be used from Helper classes
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.
// in Helper class
public
function
seeResponseContains
(
$text
)
$this
->
assertStringContainsString
(
$text
,
$this
->
getModule
(
'PhpBrowser'
)
->
_getResponseContent
(),
"response contains"
);
_loadPage
hidden API method, expected to be used from Helper classes
param string
$method
param string
$uri
param array
$parameters
param array
$files
param array
$server
param ?string
$content
return void
Opens a page with arbitrary request parameters.
Useful for testing multi-step forms on a specific step.
// in Helper class
public
function
openCheckoutFormStep2
(
$orderId
)
{
$this
->
getModule
(
'PhpBrowser'
)
->
_loadPage
(
'POST'
,
'/checkout/step2'
,
[
'order'
=>
$orderId
]);
_request
hidden API method, expected to be used from Helper classes
see
_loadPage
param string
$method
param string
$uri
param array
$parameters
param array
$files
param array
$server
param ?string
$content
throws 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.
// in Helper class
public
function
createUserByApi
(
$name
)
{
$userData
=
$this
->
getModule
(
'PhpBrowser'
)
->
_request
(
'POST'
,
'/api/v1/users'
,
[
'name'
=>
$name
]);
$user
=
json_decode
(
$userData
);
return
$user
->
id
;
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.
_savePageSource
hidden API method, expected to be used from Helper classes
Changes the subdomain for the ‘url’ configuration parameter.
Does not open a page; use
amOnPage
for that.
// If config is: 'https://mysite.com'
// or config is: 'https://www.mysite.com'
// or config is: 'https://company.mysite.com'
$I
->
amOnSubdomain
(
'user'
);
$I
->
amOnPage
(
'/'
);
// moves to https://user.mysite.com/
amOnUrl
param
$url
return void
Open web page at the given absolute URL and sets its hostname as the base host.
Attaches a file relative to the Codeception
_data
directory to the given file upload field.
// file is stored in 'tests/_data/prices.xls'
$I
->
attachFile
(
'input[@type="file"]'
,
'prices.xls'
);
checkOption
param
$option
return void
Ticks a checkbox. For radio buttons, use the
selectOption
method instead.
$I
->
checkOption
(
'#agree'
);
click
param string|array
$link
param
$context
return 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.
For checking the raw source code, use
seeInSource()
.
dontSeeCheckboxIsChecked
param
$checkbox
return void
Check that the specified checkbox is unchecked.
$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.
dontSeeCookie
param
$cookie
param
$params
return 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.
dontSeeCurrentUrlEquals
param string
$uri
return void
Checks that the current URL doesn’t equal the given string.
Unlike
dontSeeInCurrentUrl
, this only matches the full URL.
// current url is not root
$I
->
dontSeeCurrentUrlEquals
(
'/'
);
dontSeeCurrentUrlMatches
param string
$uri
return void
Checks that current url doesn’t match the given regular expression.
// to match root url
$I
->
dontSeeCurrentUrlMatches
(
'~^/users/(\d+)~'
);
dontSeeElement
param
$selector
param array
$attributes
return void
Checks that the given element is invisible or not present on the page.
You can also specify expected attributes of this element.
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.
$I
->
grabAttributeFrom
(
'#tooltip'
,
'title'
);
grabCookie
param string
$cookie
param array
$params
return 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)
.
grabFromCurrentUrl
param ?string
$uri
return 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.
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’
Sets SERVER parameter valid for all next requests.
$I->haveServerParameter('name','value');
makeHtmlSnapshot
param ?string
$name
return void
Use this method within an
interactive pause
to save the HTML source code of the current page.
$I
->
makeHtmlSnapshot
(
'edit_page'
);
// saved to: tests/_output/debug/edit_page.html
$I
->
makeHtmlSnapshot
();
// saved to: tests/_output/debug/2017-05-26_14-24-11_4b3403665fea6.html
moveBack
param int
$numberOfSteps (default value 1)
return void
Moves back in history.
resetCookie
param
$cookie
param
$params
return 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
$text
param array|string
$selector optional
return 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.
$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
$I
->
see
(
'Sign Up'
,
[
'css'
=>
'body h1'
]);
// with strict CSS locator
Note that the search is done after stripping all HTML tags from the body,
so
$I->see('strong')
will return true for strings like:
For checking the raw source code, use
seeInSource()
.
seeCheckboxIsChecked
param
$checkbox
return void
Checks that the specified checkbox is checked.
$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]'
);
seeCookie
param
$cookie
param
$params
return 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.
$I
->
seeCookie
(
'PHPSESSID'
);
seeCurrentUrlEquals
param string
$uri
return void
Checks that the current URL is equal to the given string.
Unlike
seeInCurrentUrl
, this only matches the full URL.
// to match root url
$I
->
seeCurrentUrlEquals
(
'/'
);
seeCurrentUrlMatches
param string
$uri
return void
Checks that the current URL matches the given regular expression.
// to match root url
$I
->
seeCurrentUrlMatches
(
'~^/users/(\d+)~'
);
seeElement
param
$selector
param array
$attributes
return 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.
Provide an array for the second argument to select multiple options:
$I
->
selectOption
(
'Which OS do you use?'
,
[
'Windows'
,
'Linux'
]);
Or provide an associative array for the second argument to specifically define which selection method should be used:
$I
->
selectOption
(
'Which OS do you use?'
,
[
'text'
=>
'Windows'
]);
// Only search by text 'Windows'
$I
->
selectOption
(
'Which OS do you use?'
,
[
'value'
=>
'windows'
]);
// Only search by value 'windows'
sendAjaxGetRequest
param string
$uri
param array
$params
return void
Sends an ajax GET request with the passed parameters.
See
sendAjaxPostRequest()
sendAjaxPostRequest
param string
$uri
param array
$params
return 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:
Sets the maximum number of redirects that the Client can follow.
$I
->
setMaxRedirects
(
2
);
setServerParameters
param array
$params
return void
Sets SERVER parameters valid for all next requests.
this will remove old ones.
$I->setServerParameters([]);
startFollowingRedirects
return void
Enables automatic redirects to be followed by the client.
$I
->
startFollowingRedirects
();
stopFollowingRedirects
return void
Prevents automatic redirects to be followed by the client.
$I
->
stopFollowingRedirects
();
submitForm
param
$selector
param array
$params
param ?string
$button
return 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:
Only field
names
may be used, not CSS/XPath selectors nor field labels
If a field is sent to this function that does
not
exist on the page,
it will silently be added to the HTTP request. This is helpful for testing
some types of forms, but be aware that you will
not
get an exception
like you would if you called
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.
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.
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:
// This will NOT work correctly
$I
->
submitForm
(
'#my-form'
,
[
'field[]'
=>
'value'
,
'field[]'
=>
'another value'
,
// 'field[]' is already a defined key
The solution is to pass an array value:
// This way both values are submitted
$I
->
submitForm
(
'#my-form'
,
[
'field'
=>
[
'value'
,
'another value'
,
Don't know how to write tests on your own? We will build or improve them for you.
Request a quote for official
enterprise
support
or
trainings
Codeception is a BDD-styled PHP testing framework, brought to you by
Codeception Team
.
OpenSource
MIT
Licensed
.