Completed
Push — master ( b4748f...26cf8c )
by Dmitry
15s
created

Select2::fillSearchField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
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
    public function fillSearchField(string $name): void
20
    {
21
        $this->tester->fillField('.select2-search__field', $name);
22
        $this->tester->waitForElementNotVisible('.loading-results', 120);
23
    }
24
25
    public function open($selector)
26
    {
27
        $this->tester->click($this->getSelect2Selector($selector));
28
        $this->seeIsOpened();
29
30
        return $this;
31
    }
32
33
    public function seeIsOpened()
34
    {
35
        $this->tester->seeElement('.select2-container--open');
36
37
        return $this;
38
    }
39
40
    public function seeIsClosed()
41
    {
42
        $this->tester->cantSeeElement('.select2-container--open');
43
44
        return $this;
45
    }
46
47
    /**
48
     * @param $optionName
49
     * @return $this
50
     */
51
    public function chooseOption($optionName)
52
    {
53
        $this->tester->executeJS(<<<JS
54
        $("li:contains('{$optionName}')").each(function() {
55
            if (this.firstChild.data === '{$optionName}') {
56
                $(this).trigger('mouseup');
57
            }
58
        });
59
JS
60
);
61
        return $this;
62
    }
63
64
    protected function getSelect2Selector($selector)
65
    {
66
        return $selector . ' + span [role=combobox]';
67
    }
68
69
    protected function getSelect2OptionSelector()
70
    {
71
        return '.select2-container--open .select2-results__options li';
72
    }
73
}
74