EzDevInfo.com

Codeception

Full-stack testing PHP framework Codeception - BDD-style PHP testing.

Select an option in a dynamic select with codeception?

<select>
    <option value=''>-- Select an Option --</option>
    @foreach ($options as $option)
        <option value='{{ $option->value }}'>{{ $option->name }}</option>
    @endforeach
</select> 

Select the first dynamic option


Source: (StackOverflow)

Codeception: How can I run only one test from a suite?

I have this test class below, and I want to run only one test from it, for example the "aboutPage". Any ideas how?

This is how I run only this file:

codecept run tests/acceptance/VisitorCest.php

But now I want to run only one test from the file.

<?php
use \AcceptanceTester;

class VisitorCest
{
    public function _before(){}
    public function _after(){}

    public function aboutPage(AcceptanceTester $I)
    {
        $I->wantTo('check about page');
    }

    public function contactPage(AcceptanceTester $I)
    { 
        $I->wantTo('check contact page');
    }
}

Source: (StackOverflow)

Advertisements

Failing to run Codeception tests with code coverage

I'm getting an error when I try to run tests using the --coverage flag.


Input

php codecept.phar run acceptance testCest.php --coverage

Output

[ErrorException] file_get_contents(http://project.local/c3/report/clear): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

codeception.yml

coverage:
    remote: false
    enabled: true
    include:
        - application/*

public_html/index.php

<?php require __DIR__.'/../c3.php'; ?>

Versions

PHP: 5.5.12
Xdebug: 2.5.0
Codeception: 2.0.7

Thanks for any help!

J


Source: (StackOverflow)

Referencing fixture record in yii2/codeception data files

Is there a way to specify a related row of another fixture in fixture data file in Yii2/Codeception ActiveFixture? Consider this example of user/profile relation:

user.php:

return [
    'user1' => [
        'email' => 'user1@example.net',
     ]
];

profile.php:

use common\models\User;
return [
    'profile1' => [
        'user_id' => User::findOne(['email' => 'user1@example.net'])->id;
        'name' => 'My Name',
     ]
];

The documentation states that "You may give an alias to a row so that later in your test, you may refer to the row via the alias." Is there a way to reference rows inside another fixture? For example, use something like $this->user('user1')->id in profile.php? I could not find any mention on how to do that. How do you create this kind of related fixtures?


Source: (StackOverflow)

How to setup Bamboo to work with codeception?

I have been trying to get Bamboo working with codeception tests. I am using codeception to test my code in a symfony project.

After some research I found an article on how to setup Jenkins with codeception.

Once read I figured out that I should use Ant to run the codeception commands that run the tests.

The problem is I don't really know where to put everything. This article explains all the fields for a new Ant task but nothing seem to work.

Can someone please help me?


Source: (StackOverflow)

Laravel 5 / Codeception not routing correctly

I'm trying to write an API test case for a controller function using codeception, and I'm hitting an issue where the route to the controller function does not appear to be evaluated correctly, and the evaluation seems to be different depending on what I have in my test case.

Here is a code sample from my test case:

use \ApiTester;

class CustomerRegisterCest
{
    // tests
    public function testGetRegister(ApiTester $I)
    {
        $I->sendGET('register');
        $I->seeResponseCodeIs(200);
    }

    public function testPostRegister(ApiTester $I)
    {
        $I->sendPOST('register', [
            // set the data in here
        ]);
        $I->seeResponseCodeIs(200);
    }

I have a routes.php file containing these routes:

Route::get('/', ['as' => 'home', 'uses' => 'HomeController@getIndex']);
Route::get('register', ['as' => 'getRegister', 'uses' =>'RegistrationController@getRegister']);
Route::post('register', ['as' => 'postRegister', 'uses' => 'RegistrationController@postRegister']);

I have inserted some debug statements into my controller classes so that I can see what routes get run, like this:

    Log::debug('GET register');  // or GET index or POST register, etc

At the moment I have stripped down everything from my controller classes so that ONLY the debug statements are included.

When I run the test case as above, I get the following debug output:

GET register
GET index

... so it appears that sendPOST('register', ...) actually routes to the GET route for "/" instead of the POST route for "/register". Outside of the test case everything works normally -- I can POST to the register routes fine, routing appears to work OK, the problem only appears inside a codeception test case.

If I change the test case so that I am doing the sendGET and the sendPOST inside the same function call, for example like this:

    // tests
    public function testPostRegister(ApiTester $I)
    {
        $I->sendGET('register');
        $I->seeResponseCodeIs(200);
        $I->sendPOST('register', [
            // set the data in here
        ]);
        $I->seeResponseCodeIs(200);
    }

then I see this debug output:

GET register
GET register

... so that by inserting the sendGET into the same function as the sendPOST, it has changed the sendPOST behaviour so that it now routes to the GET route for register instead of the GET route for index (but still won't route to the correct POST route).

I have tried turning xdebug on and don't have any clues from the xdebug output as to what's going on either.


Source: (StackOverflow)

Can Travis-CI run Codeception tests?

I'm creating my tests (though I'm a beginner, learning) using Codeception. This includes acceptance and unit tests for now.

I want to add my repo to Travis CI so I can automate testing process after each commit and put build-status tag.

I would like to ask;

  1. Can Travis-CI run codeception tests?
  2. Can Travis-CI run codeception acceptance tests emulating browser?
  3. If both answers are no, is there any other CI tool which can?

Thank you.


Source: (StackOverflow)

Codeception: Keep a logged in state

I want to keep or run the login before most of my tests. But if I try to move the login code to _before it doesn't work since there is no webguy instance available to me.

What is the best way to keep the session between multiple tests? This is my code so far, would be glad to receive some help. I have googled and checked the documentation but I cannot find anything about session stuff.

<?php
use \WebGuy;

class ProductCest
{

    private $product_id = '1';

    public function _before()
    {
    }

    public function _after()
    {
    }

    // tests
    public function login(WebGuy $I) {
        $I->seeInCurrentUrl('/auth/login');
        $I->fillField("//input[@type='email']", "username@email.com");
        $I->fillField("//input[@type='password']", "1234");
        $I->click('#signIn .submit');
        $I->wait(500);

        $I->seeInCurrentUrl('/account');
    }

    /**
     * @depends login
     */
    public function chooseProduct(WebGuy $I) {
        $I->wantTo('go to products and choose one');
        $I->amOnPage('/?product=' . $this->client_id);
    }

}

Source: (StackOverflow)

Are acceptance tests in Codeception supposed to run in testing environment? (Laravel4 + Codeception)

I am trying to run some acceptance tests in my Laravel application. While functional tests trigger testing environment, acceptance tests do not. Is it a bug or a feature of acceptance tests? The main problem why this is bothering me is the fact, that it is not using(+populating+cleanup) testing database, it only connects to dev database (which is used, when no other ENV is specified e.g. testing, production) and this often fails those tests when I run them multiple times.

This is my configuration:

codeception.yml

paths:
    tests: app/tests
    log: app/tests/_log
    data: app/tests/_data
    helpers: app/tests/_helpers
settings:
    bootstrap: _bootstrap.php
    suite_class: \PHPUnit_Framework_TestSuite
    colors: true
    memory_limit: 1024M
    log: true
modules:
    config:
        Db:
            dsn: 'mysql:host=localhost;dbname=testdb'
            user: 'root'
            password: 'root'
            dump: 'app/tests/_data/dump.sql'
            populate: true
            cleanup: true

acceptance.suite.yml

class_name: WebGuy
modules:
    enabled:
        - PhpBrowser
        - WebHelper
        - Db
    config:
        PhpBrowser:
            url: 'http://localhost/'

functional.suite.yml

class_name: TestGuy
modules:
    enabled: [Filesystem, TestHelper, Laravel4, Db]

Thanks for your help!


Source: (StackOverflow)

Using different codeception environments

I am working on some Unit Tests for an API using Codeception. The idea is to make sure that each API call returns the expected response codes and a JSON object in a desired format.

The problem that I have is that I need to use different URLs depending on whether the server is localhost, the test server or the production one.

I can't use the values of $_SERVER['SERVER_NAME'] because the tests are not ran through the web browser.

Here http://codeception.com/docs/07-AdvancedUsage#Environments they explain that some environments can be set by modifying the configuration file. The documentation doesn't explain how to modify the configuration file to use it within your own unit tests.

Id like to set some environments like local, test, production and then, inside my unit test classes, to know what URLs to use. Each environment would have different URLs.

I've read the documentation but I can't find the way to do it.

Do you know any way to achieve what I need?


Source: (StackOverflow)

Laravel 4 model unit test with Codeception - Class 'Eloquent' not found

I'm trying to use Codeception to run my Laravel 4 unit tests.

Running a test for a simple class with no dependencies works fine. But when I instantiate a model which depends on Eloquent, I get this fatal error:

PHP Fatal error: Class 'Eloquent' not found in /var/www/project/app/models/Role.php on line 4

Unit test:

<?php
use Codeception\Util\Stub;
class TestModel extends \Codeception\TestCase\Test 
{
  public function testExample()
  {
    $role = new Role;
    $role->name = 'superuser';
    $this->assertEquals('superuser', $role->name);
  }
}

Model:

<?php
class Role extends Eloquent
{
  public function users()
  {
    return $this->belongsToMany('User');
  }
}

Project structure:

I'm running vendor/bin/codecept run unit from the project root, with this file structure:

/project
  codeception.yml
  /vendor/bin/codecept
  /app
    /tests
      unit.suite.yml
      /unit
         ExampleTest.php
    /models
       Role.php
                ...etc

What am I doing wrong?


Source: (StackOverflow)

Codeception. Output variable while scenario is running

Is it possible to log or output any user data while the scenario is running? I know that the php code is executed two times at each run, how can I see a variable's value during the second step?


Source: (StackOverflow)

Use CodeCeption assertion in conditional (if) statement

I'm totally new with CodeCeption.

I want to do an action/assertion depending on another assertion result, like this:

if ($I->see('message')){

    $I->click('button_close');

}

Is something like that possible? I tried, but doesn't work. Probably the assertion result doesn't apply to IF, but is there any alternative?

Thanks in advance!


Source: (StackOverflow)

Codeception with PhpBrowser seems to not be following redirects

This is my first time setting up a test suite, so I may be making some stupid blunder.

I just setup Codeception to write some tests for a CodeIgniter project. I have some simple tests working (yay!) but I'm now writing an acceptance test to make sure my login form works. Filling the form and clicking submit works fine, but then when I try to inspect the page after the submit, it appears the redirect is not being followed.

  • The content of the page is empty (node list is empty, log file is 0 bytes)
  • The url of the page is the page I submitted to, not the page I should be redirected to.

The redirect is a 301 header redirect. Edit: FALSE Turns out the auth library I use does a "Reload" header redirect. Using a real 301 fixes the problem.

I assume redirects should be being followed. Is there something I need to setup/configure to get 301 redirects working?

Code:

My acceptance test:

<?php
$I = new WebGuy($scenario);
$I->wantTo('Login');
$I->amOnPage('/');
$I->fillField('identity', 'manager@example.com');
$I->fillField('password', 'password');
$I->click('submit');
$I->see('Logged In Successfully');

The output when I run the test in debug mode. The InvalidArgumentException error is being thrown because the DOM of the page it's looking at is empty:

Codeception PHP Testing Framework v1.7.1
Powered by PHPUnit 3.7.27 by Sebastian Bergmann.

Acceptance Tests (1) -----------------------
Trying to login (LoginCept.php)
Scenario:
* I am on page "/"
* I fill field "identity","manager@example.com"
* I fill field "password","password"
* I click "submit"
* I see "Logged In Successfully"
 ERROR

---------------------------------------------


Time: 3.96 seconds, Memory: 9.75Mb

There was 1 error:

---------
1) Failed to login in LoginCept.php
Sorry, I couldn't see "Logged In Successfully":
InvalidArgumentException: The current node list is empty.

Scenario Steps:
5. I see "Logged In Successfully"
4. I click "submit"
3. I fill field "password","password"
2. I fill field "identity","manager@example.com"
1. I am on page "/"


  [InvalidArgumentException]

#1  phar:///Users/captbaritone/Projects/codeception/codecept.phar/vendor/symfony/dom-crawler/Symfony/Component/DomCrawler/Crawler.php:494
#2  phar:///Users/captbaritone/Projects/codeception/codecept.phar/vendor/behat/mink-browserkit-driver/src/Behat/Mink/Driver/BrowserKitDriver.php:348
#3  phar:///Users/captbaritone/Projects/codeception/codecept.phar/vendor/behat/mink/src/Behat/Mink/Element/Element.php:101
#4  phar:///Users/captbaritone/Projects/codeception/codecept.phar/src/Codeception/Util/Mink.php:208
#5  phar:///Users/captbaritone/Projects/codeception/codecept.phar/src/Codeception/Util/Mink.php:181
#6  phar:///Users/captbaritone/Projects/codeception/codecept.phar/src/Codeception/Step.php:133
#7  phar:///Users/captbaritone/Projects/codeception/codecept.phar/src/Codeception/TestCase.php:31
#8  phar:///Users/captbaritone/Projects/codeception/codecept.phar/src/Codeception/Scenario.php:87
#9  /Users/captbaritone/Projects/codeception/tests/acceptance/WebGuy.php:571
#10 /Users/captbaritone/Projects/codeception/tests/acceptance/LoginCept.php:8

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

Source: (StackOverflow)

Codeception multiple tests, 1 script

I think I might be getting the concept wrong or not thinking about something correctly. I'm looking for a way to connect to db, and then run a selenium test (in phantomjs) for every row of a table. The test is to check for broken images on a bespoke CMS, and could be applied to any CMS.

I basically want to run an acceptance test for every page (of a specific type) by loading their IDs from the db and then running a separate test for each ID.

This is what I have so far:

$I = new WebGuy($scenario);
$results = $I->getArrayFromDB('talkthrough', '`key`', array());
foreach ($results as $r) {
    $I->wantTo('Check helpfile '.$r['key'].'for broken images');
    $I->amOnPage('/talkThrough.php?id='.$r['key']);
    $I->seeAllImages();
}

This works to some extent in that it executes until the first failure (because it is running as 1 test with many assertions).

How do I make this run as individual tests?


Source: (StackOverflow)