-1

I'm a newer in Yii2 and programming tests. I'm using Codeception for testing.

Summary:
In one of my tests I have to click on OK button of a confirm dialog. In order to do this, I have tried:
$I->click('OK');
$I->acceptPopup();
Neither of them work.

Details:
I'm using ActiveRecord to deal with a table of products ('productos'), and I used Gii to generate scripts.

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "productos".
 *
 * @property int $id
 * @property string $codigo
 * @property string $descripcion
 * @property double $cantidad
 * @property double $precio
 * @property string $fefecto
 *
 * @property DetallesPedido[] $detallesPedidos
 */
class Productos extends \yii\db\ActiveRecord
{
/**
 * {@inheritdoc}
 */
public static function tableName()
{
    return 'productos';
}

/**
 * {@inheritdoc}
 */
public function rules()
{
    return [
        [['codigo', 'descripcion', 'precio', 'fefecto'], 'required'],
        [['cantidad', 'precio'], 'number'],
        [['fefecto'], 'date', 'format'=>'yyyy-M-d'],
        [['fefecto'], 'safe'],
        [['codigo'], 'string', 'max' => 10],
        [['descripcion'], 'string', 'max' => 60],
    ];
}

/**
 * {@inheritdoc}
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'codigo' => 'Codigo',
        'descripcion' => 'Descripcion',
        'cantidad' => 'Cantidad',
        'precio' => 'Precio',
        'fefecto' => 'Fecha Alta',
    ];
}
}

The script for the view asociated to Productos.php is:

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model app\models\Productos */

    $this->title = $model->id . ' - ' . $model->codigo;
    $this->params['breadcrumbs'][] = ['label' => 'Productos', 'url' => ['index']];
    $this->params['breadcrumbs'][] = $this->title;
?>
<div class="productos-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'id' => $model->id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'codigo',
            'descripcion',
            'cantidad',
            'precio',
            'fefecto',
        ],
    ]) ?>

</div>

The script for functional test:

<?php

use app\models\Productos;

class ProductosCest
{
public function _before(\FunctionalTester $I)
{
    //Deleting all products from database
    Productos::deleteAll();

    $I->amLoggedInAs(100);//Logarse
    $I->amOnRoute('productos/index');

    // Loading product on database
    $I->haveRecord(Productos::className(), [
        'id' => 1,
        'codigo' => 'PA01',
        'descripcion' => 'Paleta de acero triangular de 20 cm',
        'cantidad' => 1,
        'precio' => 10.53,
        'fefecto' => '2017-03-12',
    ]);
}

public function _after(\FunctionalTester $I)
{
    $I->click(['class' => 'btn-link']);//Logout

    //Deleting all products from database
    Productos::deleteAll();

}

public function deleteProducto(\FunctionalTester $I)
{
    $I->amGoingTo('delete a product');
    $I->amOnRoute('productos/delete', ['id' => 1]); //Click delete button from id=1

    //Pulsar el botón Aceptar
    /*
    $I->performOn('.confirm', \Codeception\Util\ActionSequence::build()
    ->see('Are you sure you want to delete this item?')
    ->click('Aceptar')
    );
    */
    $I->acceptPopup();

    $I->expect('be in the index product view');
    $I->see('Productos', 'h1');

    $I->expect('the product is not in the index product view');
    $I->dontSee('Paleta de acero triangular de 20 cm');

}

}

When I run tests, I get:

There was 1 failure:


1) ProductosCest: Delete producto Test tests/functional/ProductosCest.php:deleteProducto Step Click {"class":"btn-link"} Fail Link or Button by name or CSS or XPath element with class 'btn-link' was not found.

Scenario Steps:

  1. $I->click({"class":"btn-link"}) at tests/functional/ProductosCest.php:28
  2. $I->amOnRoute("productos/delete",{"id":1}) at tests/functional/ProductosCest.php:132
  3. // I am going to delete a product
  4. $I->haveRecord("app\models\Productos",{"id":1,"codigo":"PA01","descripc...}) at tests/functional/ProductosCest.php:17
  5. $I->amOnRoute("productos/index") at tests/functional/ProductosCest.php:13
  6. $I->amLoggedInAs(100) at tests/functional/ProductosCest.php:12

As you can see, there is a problem in line:
$I->acceptPopup();
because message 'be in the index product view' does not appear in the test log.

Screenshots:
View products

Confirm Dialog

jose luis
  • 1
  • 3

1 Answers1

2

You use Yii2 module in functional tests, right? This module does not execute javascript code, so you won't get confirmation dialog.

In order to test confirmation dialog, you must use WebDriver module, which is used in acceptance suite usually.

acceptPopup method only works with native popup windows, as created by window.alert, window.confirm or window.prompt. If you use a modal window (it looks like you do), you must use click method to click it.

Naktibalda
  • 13,705
  • 5
  • 35
  • 51