Test di creazione Anagrafica Cliente

This commit is contained in:
Thomas Zilio 2018-08-03 19:11:45 +02:00
parent 32ad29da4b
commit b66014c107
5 changed files with 301 additions and 26 deletions

View File

@ -1,10 +1,57 @@
<?php
namespace Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
// all public methods declared in helper class will be available in $t
class Acceptance extends \Codeception\Module
{
public function login($username, $password)
{
$t = $this->getAcceptanceModule();
$t->amOnPage('/');
$t->fillField('username', $username);
$t->fillField('password', $password);
$this->clickAndWait('Accedi');
}
public function clickAndWait($link, $context = null)
{
$t = $this->getAcceptanceModule();
$t->click($link, $context);
$t->waitForElementNotVisible('#main_loading');
}
public function clickAndWaitModal($link, $context = null)
{
$t = $this->getAcceptanceModule();
$this->clickAndWait($link, $context);
$t->waitForElementVisible('.modal');
}
public function select2($selector, $option, $timeout = 5)
{
$select2 = $this->getModule('\Helper\Select2');
$select2->openSelect2($selector);
$select2->selectOptionForSelect2($selector, $option, $timeout);
$select2->closeSelect2($selector);
}
protected function getAcceptanceModule()
{
if (!$this->hasModule('WebDriver')) {
throw new \Exception('You must enable the WebDriver module', 1);
}
return $this->getModule('WebDriver');
}
}

View File

@ -0,0 +1,232 @@
<?php
// @codingStandardsIgnoreFile
namespace Helper;
// Select2 version 4.0 or greater helpers for the jQuery based replacement for select boxes.
// See: http://select2.github.io/select2
// Author: Tortue Torche <tortuetorche@spam.me>
// Author: Florian Krämer
// Author: Tom Walsh
// License: MIT
//
// Installation:
// * Put this file in your 'tests/_support/Helper' directory
// * Add it in your 'tests/acceptance.suite.yml' file, like this:
// class_name: AcceptanceTester
// modules:
// enabled:
// - WebDriver:
// url: 'http://localhost:8000'
// # ...
// - \Helper\Select2
//
// * Then run ./vendor/bin/codecept build
class Select2 extends \Codeception\Module
{
/**
* @param $selector
* @param $optionText
* @param bool $expectedReturn Default to true
*
* @return string JavaScript
*/
protected function _optionIsSelectedForSelect2($selector, $optionText, $expectedReturn = true)
{
$returnFlag = $expectedReturn === true ? '' : '!';
return $script = <<<EOT
return (function (\$) {
var isSelected = false;
var values = \$("$selector").val();
values = \$.isArray(values ) ? values : [values];
if (values && values.length > 0) {
isSelected = values.some(function (data) {
if (data && data.text && data.text === "$optionText") {
return data;
}
});
}
return ${returnFlag}isSelected;
}(jQuery));
EOT;
}
/**
* Wait until the select2 component is loaded.
*
* @param $selector
* @param int $timeout seconds. Default to 5
*/
public function waitForSelect2($selector, $timeout = 5)
{
$t = $this->getAcceptanceModule();
$selector = $this->getSelect2Selector($selector);
$t->waitForJS('return !!jQuery("'.$selector.'").data("select2");', $timeout);
}
/**
* Checks that the given option is not selected.
*
* @param $selector
* @param $optionText
* @param int $timeout seconds. Default to 5
*/
public function dontSeeOptionIsSelectedForSelect2($selector, $optionText, $timeout = 5)
{
$t = $this->getAcceptanceModule();
$selector = $this->getSelect2Selector($selector);
$this->waitForSelect2($selector, $timeout);
$script = $this->_optionIsSelectedForSelect2($selector, $optionText, false);
$t->waitForJS($script, $timeout);
}
/**
* Checks that the given option is selected.
*
* @param $selector
* @param $optionText
* @param int $timeout seconds. Default to 5
*/
public function seeOptionIsSelectedForSelect2($selector, $optionText, $timeout = 5)
{
$t = $this->getAcceptanceModule();
$selector = $this->getSelect2Selector($selector);
$this->waitForSelect2($selector, $timeout);
$script = $this->_optionIsSelectedForSelect2($selector, $optionText);
$t->waitForJS($script, $timeout);
}
/**
* Selects an option in a select2 component.
*
* $t->selectOptionForSelect2('#my_select2', 'Option value');
* $t->selectOptionForSelect2('#my_select2', ['Option value 1', 'Option value 2']);
* $t->selectOptionForSelect2('#my_select2', ['text' => 'Option text']);
* $t->selectOptionForSelect2('#my_select2', ['id' => 'Option value', 'text' => 'Option text']);
*
* @param $selector
* @param $option
* @param int $timeout seconds. Default to 1
*/
public function selectOptionForSelect2($selector, $option, $timeout = 5)
{
$t = $this->getAcceptanceModule();
$selector = $this->getSelect2Selector($selector);
$this->waitForSelect2($selector, $timeout);
if (is_int($option)) {
$option = (string) $option;
}
if (is_string($option) || (is_array($option) && array_values($option) === $option)) {
$t->executeJS('jQuery("'.$selector.'").select2("val", '.json_encode($option).');', [$timeout]);
$t->executeJS('jQuery("'.$selector.'").trigger("select2:select").trigger("change");', [$timeout]);
} elseif (is_array($option)) {
$optionId = 'null';
if (isset($option['text']) && empty($option['id'])) {
$optionText = $option['text'];
$optionId = <<<EOT
function() {
if (!\$.expr[':'].textEquals) {
// Source: http://stackoverflow.com/a/26431267
\$.expr[':'].textEquals = function(el, i, m) {
var searchText = m[3];
return $(el).text().trim() === searchText;
}
}
// Find select option by text
return \$("$selector").find("option:textEquals('$optionText'):first").val();
}();
EOT;
}
$jsonOption = json_encode($option);
$script = <<<EOT
(function (\$) {
var option = $jsonOption;
if (!option.id) {
option.id = $optionId;
}
\$("$selector").val(option.id).trigger('select2:select').trigger('change');
}(jQuery));
EOT;
$t->executeJS($script, [$timeout]);
} else {
$t->fail();
}
}
/**
* Unselect an option in the given select2 component.
*
* @param $selector
* @param $option
* @param int $timeout seconds. Default to 1
*/
public function unselectOptionForSelect2($selector, $option = null, $timeout = 1)
{
$t = $this->getAcceptanceModule();
$selector = $this->getSelect2Selector($selector);
$this->waitForSelect2($selector, $timeout);
if ($option && is_string($option)) {
$script = <<<EOT
(function (\$) {
var values = \$("$selector").select2("val");
var index = values.indexOf("$option");
if (index > -1) {
values.splice(index, 1);
}
\$("$selector").select2("val", values);
\$(\$("$selector").trigger("select2:select").trigger("change");
}(jQuery));
EOT;
$t->executeJS($script, [$timeout]);
} else {
$t->executeJS('jQuery("'.$selector.'").select2("val", "");', [$timeout]);
$t->executeJS('jQuery("'.$selector.'").trigger("select2:select").trigger("change");', [$timeout]);
}
}
/**
* Open the Select2 component.
*
* @param string $selector
*/
public function openSelect2($selector)
{
$t = $this->getAcceptanceModule();
$selector = $this->getSelect2Selector($selector);
$this->waitForSelect2($selector);
$t->executeJS('jQuery("'.$selector.'").select2("open");');
}
/**
* Close the Select2 component.
*
* @param string $selector
*/
public function closeSelect2($selector)
{
$t = $this->getAcceptanceModule();
$selector = $this->getSelect2Selector($selector);
$this->waitForSelect2($selector);
$t->executeJS('jQuery("'.$selector.'").select2("close");');
}
protected function getSelect2Selector($selector)
{
return $selector;
//return preg_replace("/^\#((?!s2id_).+)$/", '#s2id_$1', $selector);
}
protected function getAcceptanceModule()
{
if (!$this->hasModule('WebDriver')) {
throw new \Exception('You must enable the WebDriver module', 1);
}
return $this->getModule('WebDriver');
}
}

View File

@ -9,3 +9,4 @@ modules:
enabled:
- WebDriver
- \Helper\Acceptance
- \Helper\Select2

View File

@ -0,0 +1,20 @@
<?php
class AnagraficheCest
{
public function addWorks(AcceptanceTester $t)
{
$t->login('admin', 'admin');
$t->clickAndWait('Anagrafiche', '.sidebar');
$t->clickAndWaitModal('.btn-primary', '#tabs');
$t->fillField('Ragione sociale', 'TEST');
$t->select2('#idtipoanagrafica', '1');
$t->clickAndWait('Aggiungi', '#add-form');
$t->see('Dati anagrafici');
}
}

View File

@ -1,25 +0,0 @@
<?php
class LoginCest
{
public function _before(AcceptanceTester $I)
{
}
public function _after(AcceptanceTester $I)
{
}
public function frontpageWorks(AcceptanceTester $I)
{
$I->amOnPage('/');
$I->fillField('username', 'admin');
$I->fillField('password', 'admin');
$I->click('Accedi');
$I->see('OpenSTAManager');
}
}