All earlier answers are old, now it's done directly in _before method which takes Actor class as argument.
<?php
namespace Test\Api;
use ApiTester;
class TrainingCest
{
public function _before(ApiTester $I)
{
$I->amLoggedInAs('kgkg');
}
public function _after(ApiTester $I)
{
}
// tests
public function testForLoggedInUser(ApiTester $I)
{
}
public function anotherTestForLoggedInUser(ApiTester $I)
{
}
}
And if you want to log in just once for all CEST files, you can use global Registry class implementing Registry design pattern (see https://dzone.com/articles/practical-php-patterns/basic/practical-php-patterns-0) along with some lazyloading. Here is working code for my api integration tests defined in Actor class (in my case ApiTester):
public function amLoggedInAs($userLogin)
{
$I = $this;
if (Registry::getInstance()->exists($userLogin)) {
// get data from registry
$storedUserData = Registry::getInstance()->get($userLogin);
$newAccessToken = $storedUserData['accessToken'];
$playerId = $storedUserData['playerId'];
}
else {
// no registry data - log in and save data in registry
$I->tryToLogin($userLogin);
$newAccessToken = $I->grabDataFromResponseByJsonPath('data.newToken');
$playerId = (int)$I->grabDataFromResponseByJsonPath('data.userId');
Registry::getInstance()->set($userLogin, [
'accessToken' => $newAccessToken,
'playerId' => $playerId
]);
}
// finally set headers and some other data
$I->haveHttpHeader('X-Token', $newAccessToken);
$I->havePlayerId($playerId);
}
protected function tryToLogin($userLogin)
{
$I = $this;
$I->wantTo('login into api');
$I->amGoingTo('try to log to API using login and password');
$I->sendPOST('/system/login', ['login' => $userLogin, 'password' => self::getPassword($userLogin)]);
// ...some other checking if user was correctly logged in ...
}
This code basically stores accessToken along with some additional data in Registry after user logged for first time. If another call to $I->amLoggedInAs('kgkg') is invoked, it gets these values from registry. You can have many logged users this way, each of them logged only once per suite.
You can use another method for autorization instead of custom token, logic will still be the same.
Also if you're using WebDriver (not PhpBrowser), you can use loadSessionSnapshot and saveSessionSnapshot instead of Registry to get quite the same result.