[Question] Some xpath selector not found with seeElement() in functional testing


(Me) #1

Hi all,

I wanna test ajax response in one of my controller.

This is my cest method :




    public function testAjaxFormTambah(Admin $I)

    {

        $I->loginAsAdmin();

        $I->amOnPage([ '/backend/departemen/index' ]);

        $I->sendAjaxGetRequest(Url::to([ '/backend/departemen/tambah' ]), []);

        // Display output for debugging

        codecept_debug($I->getYii2()->client->getResponse()->getContent());

        $I->see('Tambah Departemen');

        $I->see('Departemen');

        // Four lines below for debugging purpose, testing xpath

        $I->seeElement('//div[@class="box"]');

        $I->seeElement('//h3[@class="box-title"]');

        $I->seeElement('//div[@class="box-body"]');

        $I->seeElement('//*[@class="box-footer"]');

        // I actually wanna test this line below

        $I->seeElement('//input[@id="departemen-namadepartemen"]');

        $I->see('Simpan');

    }



This is my Step Object which I use on above cest method




<?php


namespace Step\Functional;


use app\models\User;


class Admin extends \FunctionalTester

{


    public function loginAsAdmin()

    {

        $I = $this;

        $I->amOnPage([ '/user/default/login' ]);

        $I->submitForm('#login-form', [

            'LoginForm' => [

                'username' => 'r00t',

                'password' => '1234',

                'rememberMe' => true,

            ],

        ]);

        $I->seeCookie('_identity');

        $cookie = $I->grabCookie('_identity');

        $I->setCookie('_identity', $cookie);

        $I->seeCurrentUrlEquals('/index-test.php/arungistrator');

    }


}



This is ajax response content from my action




  <div class="box box-primary box-solid">

      <div class="box-header with-border">

          <h3 class="box-title">Tambah Departemen</h3>

          <div class="box-tools pull-right">

              <button type="button" id="df_cancel" class="btn btn-box-tool"><i class="fa fa-times"></i><span class="sr-only">Tutup</span></button>        </div>

      </div>

  

      <form id="dept_form" action="/index-test.php/arungistrator/departemen/tambah" method="post">

      <div class="box-body">

                  <div class="form-group field-departemen-namadepartemen required">

  <label class="control-label" for="departemen-namadepartemen">Departemen</label>

  <input type="text" id="departemen-namadepartemen" class="form-control" name="Departemen[NamaDepartemen]">

  

  <div class="help-block"></div>

  </div>

    </div>

  

      <div class="box-footer">

          <button type="submit" id="df_submit" class="btn btn-primary"><i class="icons ion-checkmark-round"></i>&nbsp;&nbsp;Simpan</button>    </div>

  

      </form>

  </div>

  <script type="text/javascript">jQuery('#dept_form').yiiActiveForm([{"id":"departemen-namadepartemen","name":"NamaDepartemen","container":".field-departemen-namadepartemen","input":"#departemen-namadepartemen","validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages, {"message":"Anda harus mengisi Nama Departemen"});}},{"id":"departemen-namadepartemen","name":"NamaDepartemen","container":".field-departemen-namadepartemen","input":"#departemen-namadepartemen","validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages, {"message":"Anda harus mengisi Nama Departemen"});}}], []);</script>



Finally I execute cest method with --debug option, this is the output




Codeception PHP Testing Framework v2.2.4

Powered by PHPUnit 4.8.27 by Sebastian Bergmann and contributors.


Functional Tests (1) -------------------------------------------------------------------------------------------------------------------------------------------------

Modules: Asserts, Filesystem, Yii2, \Helper\Functional, \Helper\SettingInitHelper, \Helper\RbacInitHelper

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

DepartemenCest: Test ajax form tambah

Signature: tests\functional\backend\DepartemenCest:testAjaxFormTambah

Test: tests/functional/backend/DepartemenCest.php:testAjaxFormTambah

Scenario --

 I bootstrap setting 

  [yii\web\Session::open] Session started 

 I init rbac 

  [yii\web\Session::open] Session started 

 I have fixtures {"departemen":"tests\\unit\\fixtures\\DepartemenFixture"}

 I login as admin 

   I am on page ["\/user\/default\/login"]

  [Request Headers] []

  [Page] /index-test.php/login

  [Response] 200

  [Request Cookies] []

  [Response Headers] {"content-type":["text/html; charset=UTF-8"]}

   I submit form "#login-form",{"LoginForm":{"username":"r00t","password":"1234","rememberMe":true}}

  [Uri] localhost/index-test.php/login

  [Method] POST

  [Parameters] {"LoginForm[username]":"","LoginForm[password]":"","LoginForm":{"username":"r00t","password":"1234","rememberMe":true}}

  [Request Headers] []

  [yii\web\Session::init] Session is already started 

  [yii\db\Connection::open] Opening DB connection: mysql:host=localhost;dbname=hrs_panam_test 

  [yii\web\Session::open] Session started 

  [yii\web\User::login] User '1' logged in from  with duration 2592000. 

  [Headers] {"location":["localhost/index-test.php/arungistrator"],"content-type":["text/html; charset=UTF-8"]}

  [Page] localhost/index-test.php/login

  [Response] 302

  [Request Cookies] []

  [Response Headers] {"location":["localhost/index-test.php/arungistrator"],"content-type":["text/html; charset=UTF-8"]}

  [Redirecting to] localhost/index-test.php/arungistrator

  [yii\web\Session::init] Session is already started 

  [yii\db\Connection::open] Opening DB connection: mysql:host=localhost;dbname=hrs_panam_test 

  [yii\web\Session::open] Session started 

  [yii\web\User::loginByCookie] User '1' logged in from  via cookie. 

  [Page] localhost/index-test.php/arungistrator

  [Response] 200

  [Request Cookies] {"_identity":"e0054c7c40dc011d574bd6f09067f0dd0d7c8332f87731fdc4ef8187ac85c869a:2:{i:0;s:9:"_identity";i:1;s:30:"[1,"8qg7bP6KToB8KR8L",2592000]";}"}

  [Response Headers] {"content-type":["text/html; charset=UTF-8"]}

   I see cookie "_identity"

  [Cookie Jar] ["_identity=e0054c7c40dc011d574bd6f09067f0dd0d7c8332f87731fdc4ef8187ac85c869a%3A2%3A%7Bi%3A0%3Bs%3A9%3A%22_identity%22%3Bi%3A1%3Bs%3A30%3A%22%5B1%2C%228qg7bP6KToB8KR8L%22%2C2592000%5D%22%3B%7D; expires=Sat, 12 Nov 2016 14:51:10 GMT; path=/; httponly"]

   I grab cookie "_identity"

  [Cookie Jar] ["_identity=e0054c7c40dc011d574bd6f09067f0dd0d7c8332f87731fdc4ef8187ac85c869a%3A2%3A%7Bi%3A0%3Bs%3A9%3A%22_identity%22%3Bi%3A1%3Bs%3A30%3A%22%5B1%2C%228qg7bP6KToB8KR8L%22%2C2592000%5D%22%3B%7D; expires=Sat, 12 Nov 2016 14:51:10 GMT; path=/; httponly"]

   I set cookie "_identity","e0054c7c40dc011d574bd6f09067f0dd0d7c8332f87731fdc4ef8187ac85c869a:2:{i:0;s:9:"_identity";i:1;s:30:"[1,"8qg7bP6KToB8KR8L",2592000]";}"

  [Cookie Jar] ["_identity=e0054c7c40dc011d574bd6f09067f0dd0d7c8332f87731fdc4ef8187ac85c869a%3A2%3A%7Bi%3A0%3Bs%3A9%3A%22_identity%22%3Bi%3A1%3Bs%3A30%3A%22%5B1%2C%228qg7bP6KToB8KR8L%22%2C2592000%5D%22%3B%7D; path=/; httponly"]

   I see current url equals "/index-test.php/arungistrator"

 I am on page ["\/backend\/departemen\/index"]

  [Request Headers] []

  [yii\web\Session::init] Session is already started 

  [yii\db\Connection::open] Opening DB connection: mysql:host=localhost;dbname=hrs_panam_test 

  [yii\web\Session::open] Session started 

  [yii\web\User::loginByCookie] User '1' logged in from  via cookie. 

  [Page] /index-test.php/arungistrator/departemen

  [Response] 200

  [Request Cookies] {"_identity":"e0054c7c40dc011d574bd6f09067f0dd0d7c8332f87731fdc4ef8187ac85c869a:2:{i:0;s:9:"_identity";i:1;s:30:"[1,"8qg7bP6KToB8KR8L",2592000]";}"}

  [Response Headers] {"content-type":["text/html; charset=UTF-8"]}

 I send ajax get request "/index-test.php/arungistrator/departemen/tambah",[]

  [Request Headers] []

  [yii\web\Session::init] Session is already started 

  [yii\db\Connection::open] Opening DB connection: mysql:host=localhost;dbname=hrs_panam_test 

  [yii\web\Session::open] Session started 

  [yii\web\User::loginByCookie] User '1' logged in from  via cookie. 

  [Page] /index-test.php/arungistrator/departemen/tambah

  [Response] 200

  [Request Cookies] {"_identity":"e0054c7c40dc011d574bd6f09067f0dd0d7c8332f87731fdc4ef8187ac85c869a:2:{i:0;s:9:"_identity";i:1;s:30:"[1,"8qg7bP6KToB8KR8L",2592000]";}"}

  [Response Headers] {"content-type":["text/html; charset=UTF-8"]}

 I get yii2 

  <div class="box box-primary box-solid">

      <div class="box-header with-border">

          <h3 class="box-title">Tambah Departemen</h3>

          <div class="box-tools pull-right">

              <button type="button" id="df_cancel" class="btn btn-box-tool"><i class="fa fa-times"></i><span class="sr-only">Tutup</span></button>        </div>

      </div>

  

      <form id="dept_form" action="/index-test.php/arungistrator/departemen/tambah" method="post">

      <div class="box-body">

                  <div class="form-group field-departemen-namadepartemen required">

  <label class="control-label" for="departemen-namadepartemen">Departemen</label>

  <input type="text" id="departemen-namadepartemen" class="form-control" name="Departemen[NamaDepartemen]">

  

  <div class="help-block"></div>

  </div>        <div class="form-group field-departemen-namadepartemen required">

  <label class="control-label" for="departemen-namadepartemen">Departemen</label>

  <input type="text" id="departemen-namadepartemen" class="form-control" name="Departemen[NamaDepartemen]">

  

  <div class="help-block"></div>

  </div>    </div>

  

      <div class="box-footer">

          <button type="submit" id="df_submit" class="btn btn-primary"><i class="icons ion-checkmark-round"></i>&nbsp;&nbsp;Simpan</button>    </div>

  

      </form>

  </div>

  <script type="text/javascript">jQuery('#dept_form').yiiActiveForm([{"id":"departemen-namadepartemen","name":"NamaDepartemen","container":".field-departemen-namadepartemen","input":"#departemen-namadepartemen","validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages, {"message":"Anda harus mengisi Nama Departemen"});}},{"id":"departemen-namadepartemen","name":"NamaDepartemen","container":".field-departemen-namadepartemen","input":"#departemen-namadepartemen","validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages, {"message":"Anda harus mengisi Nama Departemen"});}}], []);</script>

 I see "Tambah Departemen"

 I see "Departemen"

 I see element "//div[@class="box"]"

 I see element "//h3[@class="box-title"]"

 I see element "//div[@class="box-body"]"

 I see element "//*[@class="box-footer"]"

 FAIL 


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




Time: 6.22 seconds, Memory: 31.75MB


There was 1 failure:


---------

1) DepartemenCest: Test ajax form tambah

 Test  tests/functional/backend/DepartemenCest.php:testAjaxFormTambah

 Step  See element "//*[@class="box-footer"]"

 Fail  Element located either by name, CSS or XPath element with '//*[@class="box-footer"]' was not found.


Scenario Steps:


 18. $I->seeElement("//*[@class="box-footer"]") at tests/functional/backend/DepartemenCest.php:90

 17. $I->seeElement("//div[@class="box-body"]") at tests/functional/backend/DepartemenCest.php:89

 16. $I->seeElement("//h3[@class="box-title"]") at tests/functional/backend/DepartemenCest.php:88

 15. $I->seeElement("//div[@class="box"]") at tests/functional/backend/DepartemenCest.php:87

 14. $I->see("Departemen") at tests/functional/backend/DepartemenCest.php:85

 13. $I->see("Tambah Departemen") at tests/functional/backend/DepartemenCest.php:84




FAILURES!

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



The question is why codeception could not find class box-footer, but found other classes that I wrote?

I’m not sure what’s wrong, my xpath selector or something else.

Thanks in advance :D