Run a test as loged in user

Hi there,

I have created new Acceptance test ClientCrudCest same copy of SignupCest test, but when I run this test I got this error

Client controller needs first login, how I can loged in using command?

How I fix [Response] 302 ?

Also I double checked Add Client is there with <h3>.


 $ codecept run -vv acceptance ClientCrudCest

Codeception PHP Testing Framework v2.1.8

Powered by PHPUnit 4.8.26 by Sebastian Bergmann and contributors.


Tests\codeception\frontend.acceptance Tests (1) --------------------------------------------------------------------------------------------

Modules: PhpBrowser, \tests\codeception\common\_support\FixtureHelper

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

Ensure that clients create works (acceptance\ClientCrudCest::testClientCreate)

Scenario:

* yii\codeception\BasePage: open by

  I am on page "/frontend/web/index-test.php/clients/create"


  [Page] /frontend/web/index-test.php/clients/create

  [Response] 302

  [Request Cookies] []

  [Response Headers] {"Host":["localhost:8080"],"Connection":["close"],"X-Powered-By":["PHP/5.5.12"],"Set-Cookie":["PHPSESSID=33qdpnpiarpdoj203aeg0l9ho6; path=/; HttpOnly","_csrf=8f965820fd9abe050fde6de0eccb1b885a04c33a1805bcd25542361b13a31468a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22INOS7SnLvNZNdKzDk_N3EID2hx2KjE_M%22%3B%7D; path=/; httponly"],"Expires":["Thu, 19 Nov 1981 08:52:00 GMT"],"Cache-Control":["no-store, no-cache, must-revalidate, post-check=0, pre-check=0"],"Pragma":["no-cache"],"Location":["http://localhost:8080/frontend/web/index-test.php/site/login"],"Content-Type":["text/html; charset=UTF-8"]}

  [Redirecting to] http://localhost:8080/frontend/web/index-test.php/site/login

  [Page] http://localhost:8080/frontend/web/index-test.php/site/login

  [Response] 200

  [Request Cookies] {"PHPSESSID":"33qdpnpiarpdoj203aeg0l9ho6","_csrf":"8f965820fd9abe050fde6de0eccb1b885a04c33a1805bcd25542361b13a31468a:2:{i:0;s:5:"_csrf";i:1;s:32:"INOS7SnLvNZNdKzDk_N3EID2hx2KjE_M";}"}

  [Response Headers] {"Host":["localhost:8080"],"Connection":["close"],"X-Powered-By":["PHP/5.5.12"],"Expires":["Thu, 19 Nov 1981 08:52:00 GMT"],"Cache-Control":["no-store, no-cache, must-revalidate, post-check=0, pre-check=0"],"Pragma":["no-cache"],"Content-Type":["text/html; charset=UTF-8"]}

* I see "Add Client","h3"

 FAIL


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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




Time: 3.02 seconds, Memory: 19.75MB


There was 1 failure:


---------

1) Failed to ensure that clients create works in tests\codeception\frontend\acceptance\ClientCrudCest::testClientCreate (.\acceptance\ClientCrudCest.php)


 Step  I see "Add Client","h3"

 Fail  Element located either by name, CSS or XPath element with 'h3' was not found.


Scenario Steps:


 2. $I->see("Add Client","h3") at acceptance\ClientCrudCest.php:50

 1. $I->amOnPage("/frontend/web/index-test.php/clients/create") at tion\BasePage.php:77




FAILURES!

Tests: 1, Assertions: 1, Failures: 1.




You need to login:


$loginPage = LoginPage::openBy($I);

$I->see('Login');

$loginPage->login('username', 'userpassword');

if (method_exists($I, 'wait')) {

    $I->wait(1); // only for selenium

}

Hi thanks for your reply!

Where I can put this login code? in my newly created test class or somewhere else?

I put this in every Cept file.




use tests\codeception\_pages\DeliveryoptionPage;

use tests\codeception\_pages\LoginPage;

use yii\helpers\Url;


$I = new AcceptanceTester($scenario);


// page should not be visible if not logged in:

$I->amOnPage( Url::to(['/deliveryoption']));

$I->dontsee('deliveryoption', 'h1');


$loginPage = LoginPage::openBy($I);

$I->see('Login');

$loginPage->login('user', 'password');

if (method_exists($I, 'wait')) {

    $I->wait(1); // only for selenium

}

Hi thank your for your reply, now I have one more problem.

I have dropdown with 4 buttons, these are links not buttons, how I can click on those buttons?

When I run this code, I got error:


1) Failed to ensure that clients create works in tests\codeception\frontend\acceptance\ClientCrudCest::testClientCreate (.\acceptance\ClientCrudCest.php)


 Step  I click "schedule_pending"

 Fail  Link or Button by name or CSS or XPath element with 'schedule_pending' was not found.


Scenario Steps:


 13. $I->click("schedule_pending") at _pages\ClientCrudPage.php:26

 12. // I am going to submit client form with no data

 11. $I->see("Client Profile") at acceptance\ClientCrudCest.php:63

 10. $I->see("Add Client","h3") at acceptance\ClientCrudCest.php:62

 9. $I->amOnPage("/frontend/web/index-test.php/clients/create") at tion\BasePage.php:77

 8. $I->dontSeeLink("Sign In") at acceptance\ClientCrudCest.php:56




FAILURES!

Tests: 1, Assertions: 4, Failures: 1.



Here is my code!


<ul class="dropdown-menu" role="menu">

      <li><a href="#" id="schedule_pending" class="enroll-client-btn">SAVE & PENDING</a></li>

      <li><a href="#" id="schedule_enrollment" class="enroll-client-btn">SAVE & ENROLLMENT</a></li>

      <li><a href="#" id="schedule_appointment" class="enroll-client-btn">SAVE & APPOINTMENT</a></li>

      <li><a href="#" id="schedule_eval" class="enroll-client-btn">SAVE & EVALUATION</a></li>

</ul>

Class ClientCrudCet:

<?php

namespace tests\codeception\frontend\acceptance;

use tests\codeception\common\_pages\LoginPage;

use tests\codeception\frontend\_pages\ClientCrudPage;

//use common\models\User;

use frontend\models\Clients;


class ClientCrudCest

{

...


 public function testClientCreate($I, $scenario)

    {


        $I->wantTo('ensure login page works');

        $loginPage = LoginPage::openBy($I);


        $I->amGoingTo('try to login with correct credentials');

        $loginPage->login('erau', 'password_0');

        $I->expectTo('see that user is logged');

        $I->seeLink('Logout');

        $I->dontSeeLink('Sign In');




        $I->wantTo('ensure that clients create works');


        $clientCreatePage = ClientCrudPage::openBy($I);

        $I->see('Add Client', 'h3');

        $I->see('Client Profile');




        $I->amGoingTo('submit client form with no data');


        $clientCreatePage->submit([]);


        $I->expectTo('see validation errors');

        $I->see('First Name cannot be blank.', '.help-block');

        $I->see('Email cannot be blank.', '.help-block');

        $I->see('Middle Name cannot be blank.', '.help-block');

        $I->see("Kindly fill out the required fields");

    

        $I->amGoingTo('submit client form with not correct email');

        $clientCreatePage->submit([

            'first_name' => 'tester',

            'email' => 'tester.email',

            'middle_name' => 'tester_password',

        ]);


        $I->expectTo('see that email address is wrong');

        $I->dontSee('First Name cannot be blank..', '.help-block');

        $I->dontSee('Middle Name cannot be blank.', '.help-block');

        $I->see('Email is not a valid email address.', '.help-block');


        $I->amGoingTo('submit client form with correct email');

        $clientCreatePage->submit([

            'first_name' => 'tester',

            'email' => 'tester.email@example.com',

            'middle_name' => 'tester_password',

        ]);


        

        

    }

}

Class ClientCrudPage:


class ClientCrudPage extends BasePage

{


    public $route = 'clients/create';


    /**

     * @param array $signupData

     */

    public function submit(array $signupData)

    {

        foreach ($signupData as $field => $value) {

            $inputType = $field === 'body' ? 'textarea' : 'input';

            $this->actor->fillField($inputType . '[name="Clients[' . $field . ']"]', $value);

        }

        

        $this->actor->click('schedule_pending');

    }

}