Completed
Push — master ( 4bfae8...306a61 )
by Dmitry
07:38 queued 04:52
created

Select2::chooseOptionLike()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace hipanel\tests\_support\Page\Widget;
3
4
use Codeception\Util\Locator;
5
use hipanel\tests\_support\AcceptanceTester;
6
7
class Select2
8
{
9
    /**
10
     * @var \AcceptanceTester
11
     */
12
    protected $tester;
13
14
    function __construct(AcceptanceTester $I)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
    {
16
        $this->tester = $I;
17
    }
18
19
    /**
20
     * @param string $name
21
     * @throws \Exception
22
     */
23
    public function fillSearchField(string $name): void
24
    {
25
        $this->tester->fillField('.select2-search__field', $name);
26
        $this->tester->waitForElementNotVisible('.loading-results', 120);
27
    }
28
29
    /**
30
     * @param string $selector
31
     * @return Select2
32
     */
33
    public function open(string $selector): Select2
34
    {
35
        $this->tester->click($this->getSelect2Selector($selector));
36
        $this->seeIsOpened();
37
38
        return $this;
39
    }
40
41
    /**
42
     * @return Select2
43
     */
44
    public function seeIsOpened(): Select2
45
    {
46
        $this->tester->seeElement('.select2-container--open');
47
48
        return $this;
49
    }
50
51
    /**
52
     * @return Select2
53
     */
54
    public function seeIsClosed(): Select2
55
    {
56
        $this->tester->cantSeeElement('.select2-container--open');
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param $optionName
63
     * @return $this
64
     */
65
    public function chooseOption($optionName): Select2
66
    {
67
        $this->tester->executeJS(<<<JS
68
        $("li:contains('{$optionName}')").each(function() {
69
            if (this.firstChild.data === '{$optionName}') {
70
                $(this).trigger('mouseup');
71
            }
72
        });
73
JS
74
);
75
        return $this;
76
    }
77
78
    /**
79
     * @param $optionName
80
     * @return $this
81
     */
82
    public function chooseOptionLike($optionName): Select2
83
    {
84
        $this->tester->executeJS(<<<JS
85
        $("li:contains('{$optionName}')").each(function() {
86
            if (this.firstChild.data.indexOf('{$optionName}') !== -1) {
87
                $(this).trigger('mouseup');
88
            }
89
        });
90
JS
91
);
92
        return $this;
93
    }
94
95
    /**
96
     * @param string $selector
97
     * @return string
98
     */
99
    protected function getSelect2Selector(string $selector): string
100
    {
101
        return $selector . ' + span [role=combobox]';
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    protected function getSelect2OptionSelector(): string
108
    {
109
        return '.select2-container--open .select2-results__options li';
110
    }
111
}
112