Completed
Push — master ( 68f02d...6954ab )
by Dmitry
14:39 queued 06:03
created

IndexPage::containsLegend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace hipanel\tests\_support\Page;
4
5
use hipanel\tests\_support\Page\Widget\Input\Input;
6
use hipanel\tests\_support\Page\Widget\Input\TestableInput;
7
use hipanel\tests\_support\Page\Widget\Input\Dropdown;
8
9
use WebDriverKeys;
10
11
class IndexPage extends Authenticated
12
{
13
    /**
14
     * @param TestableInput[] $inputs example:
15
     * ```php
16
     *  [
17
     *      Input::asAdvancedSearch(Tester, 'Name'),
18
     *      Select2::asAdvancedSearch(Tester, 'Status')
19
     *  ]
20
     *```
21
     */
22
    public function containsFilters(array $inputs): void
23
    {
24
        $I = $this->tester;
25
26
        $I->see('Advanced search', 'h3');
27
28
        foreach ($inputs as $input) {
29
            $input->isVisible();
30
        }
31
        $I->see('Search', TestableInput::AS_BASE . "button[type='submit']");
32
        $I->see('Clear', TestableInput::AS_BASE  . "a");
33
    }
34
35
    /**
36
     * @param string[] $list array of legend list
37
     */
38
    public function containsLegend(array $list): void
39
    {
40
        $I = $this->tester;
41
42
        $I->see('Legend', 'h3');
43
44
        foreach ($list as $text) {
45
            $I->see($text, "//h3[text()='Legend']/../../div/ul/li");
46
        }
47
    }
48
49
    /**
50
     * @param string[] $buttons array of buttons
51
     */
52
    public function containsBulkButtons(array $buttons): void
53
    {
54
        $I = $this->tester;
55
56
        foreach ($buttons as $text) {
57
            $I->see($text, "//button[@type='submit' or @type='button']");
58
        }
59
    }
60
61
    /**
62
     * @param string[] $columnNames array of column names
63
     * @param string|null $representation the representation name
64
     * @throws \Codeception\Exception\ModuleException
65
     */
66
    public function containsColumns(array $columnNames, $representation = null): void
67
    {
68
        $I = $this->tester;
69
        $formId = $I->grabAttributeFrom("//form[contains(@id, 'bulk') " .
70
                                        "and contains(@id, 'search')]", 'id');
71
72
        if ($representation !== null) {
73
            $I->click("//button[contains(text(), 'View:')]");
74
            $I->click("//ul/li/a[contains(text(), '$representation')]");
75
            $I->waitForPageUpdate(120);
76
        }
77
78
        foreach ($columnNames as $column) {
79
            $I->see($column, "//form[@id='$formId']//table/thead/tr/th");
80
        }
81
    }
82
83
    /**
84
     * Filters index page table
85
     *
86
     * @param TestableInput $inputElement
87
     * @param $value
88
     * @throws \Codeception\Exception\ModuleException
89
     */
90
    public function filterBy(TestableInput $inputElement, string $value): void
91
    {
92
        $inputElement->setValue($value);
93
        if ($inputElement instanceof Input) {
94
            $this->tester->pressKey($inputElement->getSelector(),WebDriverKeys::ENTER);
95
        }
96
        $this->tester->waitForPageUpdate();
97
    }
98
99
    /**
100
     * Selects table row by its number
101
     *
102
     * @param int $n - number of the row that should be selected
103
     */
104
    public function selectTableRowByNumber(int $n): void
105
    {
106
        $I = $this->tester;
107
108
        $selector = "form tbody tr:nth-child($n) input[type=checkbox]";
109
        $I->click($selector);
110
    }
111
112
    /**
113
     * Opens table row menu by its number
114
     *
115
     * @param int $n - number of the row which menu should be opened
116
     */
117
    public function openRowMenuByNumber(int $n): void
118
    {
119
        $this->tester->click("form tbody tr:nth-child($n) button");
120
    }
121
122
    /**
123
     * Opens table row menu by item id
124
     *
125
     * @param string $id - id of item which menu should be opened
126
     */
127
    public function openRowMenuById(string $id): void
128
    {
129
        $this->tester->click("tr[data-key='$id'] button");
130
    }
131
132
    /**
133
     * Clicks to row menu option
134
     *
135
     * @param $option - the name of option that should be clicked
136
     * @throws \Codeception\Exception\ModuleException
137
     */
138
    public function chooseRowMenuOption(string $option): void
139
    {
140
        $this->tester->click("//ul[@class='nav']//a[contains(text(), '{$option}')]");
141
        $this->tester->waitForPageUpdate();
142
    }
143
144
    /**
145
     * Parse tbody, count td and return result
146
     *
147
     * @return int
148
     */
149
    public function countRowsInTableBody(): int
150
    {
151
        return count($this->tester->grabMultiple('//tbody/tr'));
152
    }
153
154
    /**
155
     * Checked filtering correct works
156
     *
157
     * @param string $filterBy
158
     * @param string $name
159
     * @throws \Codeception\Exception\ModuleException
160
     */
161
    public function checkFilterBy(string $filterBy, string $name): void
162
    {
163
        $this->filterBy(new Dropdown($this->tester, "tr.filters select[name*=$filterBy]"), $name);
164
        $count = $this->countRowsInTableBody();
165
        for ($i = 0 ; $i < $count; ++$i) {
166
            $this->tester->see($name, '//tbody/tr');
167
        }
168
    }
169
170
    /**
171
     * Checked sort correct works
172
     *
173
     * Method find column by $sortBy, parse data, call default sort by $sortBy
174
     * and compare data in table with sort(copy_data_from_table)
175
     *
176
     * @param string $sortBy
177
     * @throws \Codeception\Exception\ModuleException
178
     */
179
    public function checkSortingBy(string $sortBy): void
180
    {
181
        $this->tester->click("//button[contains(text(),'Sort')]");
182
        $this->tester->click("//ul//a[contains(text(),'$sortBy')]");
183
        $this->tester->waitForPageUpdate();
184
        $tableWithNeedle = $this->tester->grabMultiple('//th/a');
185
        $whereNeedle = 0;
186
        $count = $this->countRowsInTableBody();
187
        while ($whereNeedle < count($tableWithNeedle)) {
188
            if ($tableWithNeedle[$whereNeedle] === $sortBy) {
189
                break ;
190
            }
191
            $whereNeedle++;
192
        }
193
        $whereNeedle += 2;
194
        /**
195
         *  $whereNeedle += 2 && $i = 1 because xpath elements starting from 1
196
         */
197
        $arrayForSort = array();
198 View Code Duplication
        for ($i = 1 ; $i <= $count; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
            $arrayForSort[$i] = $this->tester->grabTextFrom("//tbody/tr[$i]/td[$whereNeedle]");
200
        }
201
        /**
202
         *  After sort() function arrayForSort start index = 0, but xpath elements starting from 1
203
         */
204
        sort($arrayForSort);
205 View Code Duplication
        for ($i = 1 ; $i <= $count; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
            $this->tester->see($arrayForSort[$i - 1], "//tbody/tr[$i]/td[$whereNeedle]");
207
        }
208
    }
209
210
}
211