/
Grid.php
498 lines (449 loc) · 12.6 KB
/
Grid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Test\Block\Widget;
use Magento\Mtf\Block\Block;
use Magento\Mtf\Client\Locator;
use Magento\Mtf\Client\Element\SimpleElement;
use Magento\Mtf\Factory\Factory;
/**
* Abstract class Grid
* Basic grid actions
*
* @SuppressWarnings(PHPMD.NumberOfChildren)
* @SuppressWarnings(PHPMD.TooManyFields)
*/
abstract class Grid extends Block
{
/**
* Filters array mapping
*
* @var array
*/
protected $filters = [];
/**
* Locator value for 'Search' button
*
* @var string
*/
protected $searchButton = '[data-action="grid-filter-apply"]';
/**
* Locator for 'Sort' link
*
* @var string
*/
protected $sortLink = "[name='%s'][title='%s']";
/**
* Locator value for 'Reset' button
*
* @var string
*/
protected $resetButton = '[data-action="grid-filter-reset"]';
/**
* The first row in grid. For this moment we suggest that we should strictly define what we are going to search
*
* @var string
*/
protected $rowItem = 'tbody tr';
/**
* Locator value for link in action column
*
* @var string
*/
protected $editLink = 'td[class*=col-action] a';
/**
* An element locator which allows to select entities in grid
*
* @var string
*/
protected $selectItem = 'tbody tr [type="checkbox"]';
/**
* 'Select All' link
*
* @var string
*/
protected $selectAll = '.massaction a[onclick*=".selectAll()"]';
/**
* Massaction dropdown
*
* @var string
*/
protected $massactionSelect = '[id*=massaction-select]';
/**
* Massaction dropdown
*
* @var string
*/
protected $massactionAction = '[data-menu="grid-mass-select"]';
/**
* Massaction 'Submit' button
*
* @var string
*/
protected $massactionSubmit = '[id*=massaction-form] button';
/**
* Backend abstract block
*
* @var string
*/
protected $templateBlock = './ancestor::body';
/**
* Locator type of waitForSelector
*
* @var Locator
*/
protected $waitForSelectorType = Locator::SELECTOR_CSS;
/**
* Wait for should be for visibility or not?
*
* @var boolean
*/
protected $waitForSelectorVisible = true;
/**
* Selector for action option select
*
* @var string
*/
protected $option = '[name="status"]';
/**
* Active class
*
* @var string
*/
protected $active = '[class=*_active]';
/**
* Secondary part of row locator template for getRow() method
*
* @var string
*/
protected $rowTemplate = 'td[contains(.,normalize-space("%s"))]';
/**
* Secondary part of row locator template for getRow() method with strict option
*
* @var string
*/
protected $rowTemplateStrict = 'td[text()[normalize-space()="%s"]]';
/**
* Magento grid loader
*
* @var string
*/
protected $loader = '[data-role="spinner"]';
/**
* Locator for next page action
*
* @var string
*/
protected $actionNextPage = '[class*=data-grid-pager] .action-next';
/**
* Locator for disabled next page action
*
* @var string
*/
protected $actionNextPageDisabled = '[class*=data-grid-pager] .action-next.disabled';
/**
* First row selector
*
* @var string
*/
protected $firstRowSelector = '';
/**
* Selector for no records row.
*
* @var string
*/
protected $noRecords = '.empty-text';
/**
* Base part of row locator template for getRow() method.
*
* @var string
*/
protected $rowPattern = '//tbody/tr[%s]';
/**
* Selector for confirm.
*
* @var string
*/
protected $confirmModal = '.confirm._show[data-role=modal]';
/**
* Get backend abstract block
*
* @return \Magento\Backend\Test\Block\Template
*/
protected function getTemplateBlock()
{
return Factory::getBlockFactory()->getMagentoBackendTemplate(
$this->_rootElement->find($this->templateBlock, Locator::SELECTOR_XPATH)
);
}
/**
* Prepare data to perform search, fill in search filter
*
* @param array $filters
* @throws \Exception
*/
protected function prepareForSearch(array $filters)
{
foreach ($filters as $key => $value) {
if (isset($this->filters[$key])) {
$selector = $this->filters[$key]['selector'];
$strategy = isset($this->filters[$key]['strategy'])
? $this->filters[$key]['strategy']
: Locator::SELECTOR_CSS;
$typifiedElement = isset($this->filters[$key]['input'])
? $this->filters[$key]['input']
: null;
$this->_rootElement->find($selector, $strategy, $typifiedElement)->setValue($value);
} else {
throw new \Exception("Column $key is absent in the grid or not described yet.");
}
}
}
/**
* Search item via grid filter
*
* @param array $filter
*/
public function search(array $filter)
{
$this->resetFilter();
$this->prepareForSearch($filter);
$this->_rootElement->find($this->searchButton, Locator::SELECTOR_CSS)->click();
$this->waitLoader();
}
/**
* Search item and open it
*
* @param array $filter
* @throws \Exception
*/
public function searchAndOpen(array $filter)
{
$this->search($filter);
$rowItem = $this->getRow($filter);
if ($rowItem->isVisible()) {
$rowItem->find($this->editLink, Locator::SELECTOR_CSS)->click();
} else {
throw new \Exception("Searched item was not found by filter\n" . print_r($filter, true));
}
$this->waitLoader();
}
/**
* Wait loader
*
* @return void
*/
protected function waitLoader()
{
$this->browser->waitUntil(
function () {
$element = $this->browser->find($this->loader);
return $element->isVisible() == false ? true : null;
}
);
$this->getTemplateBlock()->waitLoader();
}
/**
* Search for item and select it
*
* @param array $filter
* @throws \Exception
*/
public function searchAndSelect(array $filter)
{
$this->search($filter);
$selectItem = $this->getRow($filter)->find($this->selectItem);
if ($selectItem->isVisible()) {
$selectItem->click();
} else {
throw new \Exception('Searched item was not found.');
}
}
/**
* Press 'Reset' button
*/
public function resetFilter()
{
$this->waitLoader();
$this->_rootElement->find($this->resetButton)->click();
$this->waitLoader();
}
/**
* Perform selected massaction over checked items.
*
* @param array $items
* @param array|string $action [array -> key = value from first select; value => value from subselect]
* @param bool $acceptAlert [optional]
* @param string $massActionSelection [optional]
* @return void
*/
public function massaction(array $items, $action, $acceptAlert = false, $massActionSelection = '')
{
if ($this->_rootElement->find($this->noRecords)->isVisible()) {
return;
}
if (!is_array($action)) {
$action = [$action => '-'];
}
foreach ($items as $item) {
$this->searchAndSelect($item);
}
if ($massActionSelection) {
$this->_rootElement->find($this->massactionAction, Locator::SELECTOR_CSS, 'select')
->setValue($massActionSelection);
}
$actionType = key($action);
$this->_rootElement->find($this->massactionSelect, Locator::SELECTOR_CSS, 'select')->setValue($actionType);
if (isset($action[$actionType]) && $action[$actionType] != '-') {
$this->_rootElement->find($this->option, Locator::SELECTOR_CSS, 'select')->setValue($action[$actionType]);
}
$this->massActionSubmit($acceptAlert);
}
/**
* Submit mass actions
*
* @param bool $acceptAlert
* @return void
*/
protected function massActionSubmit($acceptAlert)
{
$this->_rootElement->find($this->massactionSubmit, Locator::SELECTOR_CSS)->click();
if ($acceptAlert) {
$element = $this->browser->find($this->confirmModal);
/** @var \Magento\Ui\Test\Block\Adminhtml\Modal $modal */
$modal = $this->blockFactory->create('Magento\Ui\Test\Block\Adminhtml\Modal', ['element' => $element]);
$modal->acceptAlert();
}
}
/**
* Obtain specific row in grid
*
* @param array $filter
* @param bool $isStrict
* @return SimpleElement
*/
protected function getRow(array $filter, $isStrict = true)
{
$rowTemplate = ($isStrict) ? $this->rowTemplateStrict : $this->rowTemplate;
$rows = [];
foreach ($filter as $value) {
if (strpos($value, '"') !== false) {
$rowTemplate = str_replace('"', '', $rowTemplate);
$value = $this->xpathEscape($value);
}
$rows[] = sprintf($rowTemplate, $value);
}
$location = sprintf($this->rowPattern, implode(' and ', $rows));
return $this->_rootElement->find($location, Locator::SELECTOR_XPATH);
}
/**
* Get rows data
*
* @param array $columns
* @return array
*/
public function getRowsData(array $columns)
{
$data = [];
do {
$rows = $this->_rootElement->getElements($this->rowItem);
foreach ($rows as $row) {
$rowData = [];
foreach ($columns as $columnName) {
$rowData[$columnName] = trim($row->find('.col-' . $columnName)->getText());
}
$data[] = $rowData;
}
} while ($this->nextPage());
return $data;
}
/**
* Check if specific row exists in grid
*
* @param array $filter
* @param bool $isSearchable
* @param bool $isStrict
* @return bool
*/
public function isRowVisible(array $filter, $isSearchable = true, $isStrict = true)
{
$this->waitLoader();
if ($isSearchable) {
$this->search($filter);
}
return $this->getRow($filter, $isStrict)->isVisible();
}
/**
* Sort grid by field
*
* @param $field
* @param string $sort
*/
public function sortGridByField($field, $sort = "desc")
{
$sortBlock = $this->_rootElement->find(sprintf($this->sortLink, $field, $sort));
if ($sortBlock->isVisible()) {
$sortBlock->click();
$this->waitLoader();
}
}
/**
* Click to next page action link
*
* @return bool
*/
protected function nextPage()
{
if ($this->_rootElement->find($this->actionNextPageDisabled)->isVisible()) {
return false;
}
$this->_rootElement->find($this->actionNextPage)->click();
$this->waitLoader();
return true;
}
/**
* Check whether first row is visible
*
* @return bool
*/
public function isFirstRowVisible()
{
return $this->_rootElement->find($this->firstRowSelector, Locator::SELECTOR_XPATH)->isVisible();
}
/**
* Open first item in grid
*
* @return void
*/
public function openFirstRow()
{
$this->waitLoader();
$this->_rootElement->find($this->firstRowSelector, Locator::SELECTOR_XPATH)->click();
}
/**
* Escape single and/or double quotes in XPath selector by concat()
*
* @param string $query
* @param string $defaultDelim [optional]
* @return string
*/
protected function xpathEscape($query, $defaultDelim = '"')
{
if (strpos($query, $defaultDelim) === false) {
return $defaultDelim . $query . $defaultDelim;
}
preg_match_all("#(?:('+)|[^']+)#", $query, $matches);
list($parts, $apos) = $matches;
$delim = '';
foreach ($parts as $i => &$part) {
$delim = $apos[$i] ? '"' : "'";
$part = $delim . $part . $delim;
}
if (count($parts) == 1) {
$parts[] = $delim . $delim;
}
return 'concat(' . implode(',', $parts) . ')';
}
}