Search

💡 You are reading docs for latest Codeception 5. Read for 4.x

Acceptance Testing

Acceptance testing can be performed by a non-technical person. That person can be your tester, manager or even client. If you are developing a web-application (and you probably are) the tester needs nothing more than a web browser to check that your site works correctly. You can reproduce an acceptance tester’s actions in scenarios and run them automatically. Codeception keeps tests clean and simple as if they were recorded from the words of an actual acceptance tester.

It makes no difference what (if any) CMS or framework is used on the site. You can even test sites created with different languages, like Java, .NET, etc. It’s always a good idea to add tests to your website. At least you will be sure that site features work after the latest changes were made.

Sample Scenario

Let’s say the first test you would want to run, would be signing in. In order to write such a test, we still require basic knowledge of PHP and HTML:

$I->amOnPage('/login');
$I->fillField('username', 'davert');
$I->fillField('password', 'qwerty');
$I->click('LOGIN');
$I->see('Welcome, Davert!');

This scenario can be performed either by PhpBrowser or by a real browser through WebDriver.

  PhpBrowser WebDriver
Browser Engine Guzzle + Symfony BrowserKit Chrome or Firefox
JavaScript No Yes
see/seeElement checks if… …text is present in the HTML source …text is actually visible to the user
Access to HTTP response headers and status codes Yes No
System requirements PHP with ext-curl Chrome or Firefox; optionally with Selenium Standalone Server
Speed Fast Slow

We will start writing our first acceptance tests with PhpBrowser.

PhpBrowser

This is the fastest way to run acceptance tests since it doesn’t require running an actual browser. We use a PHP web scraper, which acts like a browser: It sends a request, then receives and parses the response. Codeception uses Guzzle and Symfony BrowserKit to interact with HTML web pages.

Common PhpBrowser drawbacks:

  • You can only click on links with valid URLs or form submit buttons
  • You can’t fill in fields that are not inside a form

We need to specify the url parameter in the acceptance suite config:

# Acceptance.suite.yml
actor: AcceptanceTester
modules:
    enabled:
        - PhpBrowser:
            url: http://www.example.com/

We should start by creating a test with the next command:

php vendor/bin/codecept g:cest Acceptance Signin

It will be placed into tests/Acceptance directory.

class SigninCest
{
    public function tryToTest(AcceptanceTester $I)
    {
    }
}

The $I object is used to write all interactions. The methods of the $I object are taken from the PhpBrowser Module. We will briefly describe them here:

$I->amOnPage('/login');

We will assume that all actions starting with am and have describe the initial environment. The amOnPage action sets the starting point of a test to the /login page.

With the PhpBrowser you can click the links and fill in the forms. That will probably be the majority of your actions.

Click

Emulates a click on valid anchors. The URL referenced in the href attribute will be opened. As a parameter, you can specify the link name or a valid CSS or XPath selector.

$I->click('Log in');
// CSS selector applied
$I->click('#login a');
// XPath
$I->click('//a[@id=login]');
// Using context as second argument
$I->click('Login', '.nav');

Codeception tries to locate an element by its text, name, CSS or XPath. You can specify the locator type manually by passing an array as a parameter. We call this a strict locator. Available strict locator types are:

  • id
  • name
  • css
  • xpath
  • link
  • class
// By specifying locator type
$I->click(['link' => 'Login']);
$I->click(['class' => 'btn']);

There is a special class Codeception\Util\Locator which may help you to generate complex XPath locators. For instance, it can easily allow you to click an element on the last row of a table:

$I->click('Edit' , \Codeception\Util\Locator::elementAt('//table/tr', -1));

Forms

Clicking links is probably not what takes the most time during the testing of a website. The most routine waste of time goes into the testing of forms. Codeception provides several ways of testing forms.

Let’s submit this sample form inside the Codeception test:

<form method="post" action="/update" id="update_form">
     <label for="user_name">Name</label>
     <input type=