diff --git a/codeception.dist.yml b/codeception.dist.yml index 1a3116511..e268f61b0 100644 --- a/codeception.dist.yml +++ b/codeception.dist.yml @@ -11,4 +11,4 @@ actor_suffix: Tester extensions: enabled: - Codeception\Extension\RunFailed - - Codeception\Extension\Recorder + #- Codeception\Extension\Recorder diff --git a/tests/_support/Helper/Acceptance.php b/tests/_support/Helper/Acceptance.php index 679cab996..9a9920fc7 100644 --- a/tests/_support/Helper/Acceptance.php +++ b/tests/_support/Helper/Acceptance.php @@ -33,8 +33,12 @@ class Acceptance extends \Codeception\Module public function select2ajax($selector, $option, $timeout = null) { $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) diff --git a/tests/_support/Helper/Select2Ajax.php b/tests/_support/Helper/Select2Ajax.php index 702ccf419..93c12f72a 100644 --- a/tests/_support/Helper/Select2Ajax.php +++ b/tests/_support/Helper/Select2Ajax.php @@ -1,24 +1,58 @@ -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) + 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(); $selector = $this->getSelect2Selector($selector); @@ -28,39 +62,26 @@ class Select2Ajax extends Select2 $option = (string) $option; } - if (is_string($option) || (is_array($option) && array_values($option) === $option)) { - $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 = <<executeJS($script, [$timeout]); - } else { - $t->fail(); + $results_selector = str_replace('#', '', $selector); + + $script = << ({key,value})); + + if(output[0]) { + options.push(output[0].value.data); } + }) + + $code +}); +EOT; + + $t->executeJS($script, [$timeout]); } }