Codeception: Event before run tests

Created on 15 Sep 2015  路  11Comments  路  Source: Codeception/Codeception

Hi. Sorry for my bad English.

I need to test the Rest Api . My api is protected by the access_token.

Therefore, before run all tests, I want to get the access, and use method amBearerAuthenticated.

I don't want to receive a new access token in each test of the method _before. I want to get the access token once and work with it.

How can I do this?

Most helpful comment

@nepster-web I know it's like 18 month laters, but for anyone else looking for an answer

namespace Helper;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class Api extends \Codeception\Module
{
    public function _before(\Codeception\TestInterface $test)
    {
        $I = $this->getModule('REST');

        $I->sendPOST('/oauth/token', [
            'grant_type' => 'password',
            'client_id' => '1',
            'client_secret' => 'token',
            'username' => 'user',
            'password' => 'secret',
        ]);

        $I->seeResponseCodeIs(200);
        $I->seeResponseIsJson();

        $token = $I->grabDataFromResponseByJsonPath('$..access_token');
        $token = $token[0];

        $I->amBearerAuthenticated($token);
    }
}

All 11 comments

Add a module and put in _beforeSuite()

$this->getModule('REST')->amBearerAuthenticated()

I found only this:
https://jordaneldredge.com/blog/writing-a-custom-codeception-module/

Why add the module so hard?
And why codeception has no standard methods for this ?

Custom modules are called Helpers: http://codeception.com/docs/06-ReusingTestCode#Modules-and-Helpers

I use yii2, codeception yii2 module.

I create a file tests/codeception/_support/Helper/Oauth2.php:

<?php

namespace Helper;

/**
 * Class Oauth2
 * @package Helper
 */
class Oauth2 extends \Codeception\Module
{
    public function _before()
    {
        $I = $this->getModule('REST');

        $I->sendPOST("http://site.com/oauth2/user", [
            'username' => 'admin',
            'password' => 'admin',
        ]);

        $I->seeResponseCodeIs(200);
        $I->seeResponseIsJson();

        $token = $I->grabDataFromResponseByJsonPath('$..access_token');
        $token = $token[0];

        $I->amBearerAuthenticated($token);
    }

}

Config:

class_name: ApiTester

modules:
    enabled:
        - REST
        - Helper\Oauth2
    config:
        REST:
          url: http://site.com/v1/
          depends: PhpBrowser
          part: Json

And it really works. Pay attention: works only method _before, method _beforeSuite() gives an error message.

Many thanks.

Client is instantiated by _before methods of PhpBrowser and REST,

You have to call _before of PhpBrowser and REST before making any requests.

$this->getModule('PhpBrowswer')->_before();
$this->getModule('REST')->_before();
$this->getModule('REST')->sendPOST();

I'm sorry but I did not understand you.

I use api helper, and this work:

namespace Helper;
class Api extends \Codeception\Module
{
    /**
     * @inheritdoc
     */
    public function _before()
    {
        // get access token
        $I->amBearerAuthenticated($token);
    }
}

But method _before() is performed before each test.
Method _beforeSuite() is performed only once, but shows an error:

PHP Fatal error:  Call to a member function request() on null in phar:///usr/local/bin/codecept/src/Codeception/Module/REST.php on line 476

Why is this happening ?

It happens, because _beforeSuite is executed before _before.

what should I do? =)

Do you have some sample code for me? I will be very grateful!

@nepster-web I know it's like 18 month laters, but for anyone else looking for an answer

namespace Helper;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class Api extends \Codeception\Module
{
    public function _before(\Codeception\TestInterface $test)
    {
        $I = $this->getModule('REST');

        $I->sendPOST('/oauth/token', [
            'grant_type' => 'password',
            'client_id' => '1',
            'client_secret' => 'token',
            'username' => 'user',
            'password' => 'secret',
        ]);

        $I->seeResponseCodeIs(200);
        $I->seeResponseIsJson();

        $token = $I->grabDataFromResponseByJsonPath('$..access_token');
        $token = $token[0];

        $I->amBearerAuthenticated($token);
    }
}

How to see the value present in json file

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maxibello picture maxibello  路  3Comments

DavertMik picture DavertMik  路  4Comments

Renkas picture Renkas  路  3Comments

sasha-x picture sasha-x  路  3Comments

allen0817 picture allen0817  路  3Comments