Supporto SelectAjax tramite tests

This commit is contained in:
Thomas Zilio 2019-02-23 16:18:34 +01:00
parent 1816dcfe86
commit 4711b50fd2
3 changed files with 69 additions and 44 deletions

View File

@ -11,4 +11,4 @@ actor_suffix: Tester
extensions: extensions:
enabled: enabled:
- Codeception\Extension\RunFailed - Codeception\Extension\RunFailed
- Codeception\Extension\Recorder #- Codeception\Extension\Recorder

View File

@ -33,8 +33,12 @@ class Acceptance extends \Codeception\Module
public function select2ajax($selector, $option, $timeout = null) public function select2ajax($selector, $option, $timeout = null)
{ {
$select2 = $this->getModule('\Helper\Select2Ajax'); $select2 = $this->getModule('\Helper\Select2Ajax');
$t = $this->getAcceptanceModule();
$select2->selectOptionForSelect2($selector, $option, $timeout); $select2->openSelect2($selector);
$t->wait(1);
$select2->selectByPosition($selector, $option, $timeout);
$select2->closeSelect2($selector);
} }
public function seePageHasElement($element) public function seePageHasElement($element)

View File

@ -1,24 +1,58 @@
<?php <?php // @codingStandardsIgnoreFile
// @codingStandardsIgnoreFile
namespace Helper; namespace Helper;
/**
* Select2 version 4.0 or greater helpers for the jQuery based replacement for select boxes (Ajax version).
*
* 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:
* # ...
* - \Helper\Select2Ajax
* - Run ./vendor/bin/codecept build
*
* @see http://select2.github.io/select2
* @author Thomas Zilio
*
* @license MIT
*
*/
class Select2Ajax extends Select2 class Select2Ajax extends Select2
{ {
/** /**
* Selects an option in a select2 component. * 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 $selector
* @param $option * @param $option
* @param int $timeout seconds. Default to 1 * @param int $timeout seconds. Default to 1
*/ */
public function selectOptionForSelect2($selector, $option, $timeout = 5) public function selectByTextOrId($selector, $option, $timeout = 5)
{
$code = '
$(options).each(function () {
if($(this).text == "'.$option.'" || $(this).id == "'.$option.'") {
$("'.$selector.'").selectSetNew(this.id, this.text);
}
});';
$this->execute($selector, $timeout, $code);
}
public function selectByPosition($selector, $position, $timeout = 5)
{
$code = '
var result = options['.$position.'];
$("'.$selector.'").selectSetNew(result.id, result.text);';
$this->execute($selector, $timeout, $code);
}
protected function execute($selector, $timeout, $code)
{ {
$t = $this->getAcceptanceModule(); $t = $this->getAcceptanceModule();
$selector = $this->getSelect2Selector($selector); $selector = $this->getSelect2Selector($selector);
@ -28,39 +62,26 @@ class Select2Ajax extends Select2
$option = (string) $option; $option = (string) $option;
} }
if (is_string($option) || (is_array($option) && array_values($option) === $option)) { $results_selector = str_replace('#', '', $selector);
$t->executeJS('jQuery("'.$selector.'").selectSetNew('.json_encode($option).', "ID: " + '.json_encode($option).');', [$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 $script = <<<EOT
(function (\$) { $(document).ready(function() {
var option = $jsonOption; var children = $("#select2-$results_selector-results").children();
if (!option.id) {
option.id = $optionId; var options = [];
children.each(function () {
var data = $(this)[0];
var output = Object.entries(data).map(([key, value]) => ({key,value}));
if(output[0]) {
options.push(output[0].value.data);
} }
\$("$selector").val(option.id).trigger('select2:select').trigger('change'); })
}(jQuery));
$code
});
EOT; EOT;
$t->executeJS($script, [$timeout]); $t->executeJS($script, [$timeout]);
} else {
$t->fail();
}
} }
} }