Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Grid Class Reference
Inheritance diagram for Grid:
Ordered StoreGrid BackupGrid ProductGrid Chooser Grid Catalog AgreementGrid Chooser Filter IntegrationGrid Grid Grid Grid Grid AccountsGrid Grid Grid Grid Grid ProductGrid Grid FilterGrid Grid SearchTermsGrid Grid Grid Grid Grid Grid Grid StatusGrid Grid Grid Grid Grid Items SitemapGrid Grid Grid DataGrid Grid Grid Grid LockedUsersGrid RoleGrid Roles Grid UserGrid Grid Grid Grid Grid Grid

Public Member Functions

 search (array $filter)
 
 searchAndOpen (array $filter)
 
 searchAndSelect (array $filter)
 
 resetFilter ()
 
 massaction (array $items, $action, $acceptAlert=false, $massActionSelection='')
 
 getRowsData (array $columns)
 
 isRowVisible (array $filter, $isSearchable=true, $isStrict=true)
 
 sortGridByField ($field, $sort="desc")
 
 isFirstRowVisible ()
 
 openFirstRow ()
 

Protected Member Functions

 getTemplateBlock ()
 
 prepareForSearch (array $filters)
 
 waitLoader ()
 
 getRow (array $filter, $isStrict=true)
 
 nextPage ()
 
 xpathEscape ($query, $defaultDelim='"')
 

Protected Attributes

 $filters = []
 
 $searchButton = '[data-action="grid-filter-apply"]'
 
 $sortLink = "[name='%s'][title='%s']"
 
 $resetButton = '[data-action="grid-filter-reset"]'
 
 $rowItem = 'tbody tr'
 
 $lastRowItem = 'tbody tr:last-child'
 
 $editLink = 'td[class*=col-action] a'
 
 $selectItem = 'tbody tr [type="checkbox"]'
 
 $selectAll = '.massaction a[onclick*=".selectAll()"]'
 
 $massactionSelect = '[id*=massaction-select]'
 
 $massactionAction = '[data-menu="grid-mass-select"]'
 
 $massactionSubmit = '[id*=massaction-form] button'
 
 $templateBlock = './ancestor::body'
 
 $waitForSelectorType = Locator::SELECTOR_CSS
 
 $waitForSelectorVisible = true
 
 $option = '[name="status"]'
 
 $active = '[class=*_active]'
 
 $rowTemplate = 'td[contains(.,normalize-space("%s"))]'
 
 $rowTemplateStrict = 'td[text()[normalize-space()="%s"]]'
 
 $loader = '[data-role="spinner"]'
 
 $actionNextPage = '[class*=data-grid-pager] .action-next'
 
 $actionNextPageDisabled = '[class*=data-grid-pager] .action-next.disabled'
 
 $firstRowSelector = ''
 
 $noRecords = '.empty-text'
 
 $rowPattern = '//tbody/tr[%s]'
 
 $confirmModal = '.confirm._show[data-role=modal]'
 

Detailed Description

Abstract class Grid Basic grid actions

@SuppressWarnings(PHPMD.NumberOfChildren) @SuppressWarnings(PHPMD.TooManyFields)

Definition at line 21 of file Grid.php.

Member Function Documentation

◆ getRow()

getRow ( array  $filter,
  $isStrict = true 
)
protected

Obtain specific row in grid

Parameters
array$filter
bool$isStrict
Returns
SimpleElement

Definition at line 369 of file Grid.php.

370  {
371  $rowTemplate = ($isStrict) ? $this->rowTemplateStrict : $this->rowTemplate;
372  $rows = [];
373  foreach ($filter as $value) {
374  if (strpos($value, '"') !== false) {
375  $rowTemplate = str_replace('"', '', $rowTemplate);
376  $value = $this->xpathEscape($value);
377  }
378  $rows[] = sprintf($rowTemplate, $value);
379  }
380  $location = sprintf($this->rowPattern, implode(' and ', $rows));
381  return $this->_rootElement->find($location, Locator::SELECTOR_XPATH);
382  }
xpathEscape($query, $defaultDelim='"')
Definition: Grid.php:484
$value
Definition: gender.phtml:16

◆ getRowsData()

getRowsData ( array  $columns)

Get rows data

Parameters
array$columns
Returns
array

Definition at line 390 of file Grid.php.

391  {
392  $data = [];
393  do {
394  $rows = $this->_rootElement->getElements($this->rowItem);
395  foreach ($rows as $row) {
396  $rowData = [];
397  foreach ($columns as $columnName) {
398  $rowData[$columnName] = trim($row->find('.col-' . $columnName)->getText());
399  }
400 
401  $data[] = $rowData;
402  }
403  } while ($this->nextPage());
404 
405  return $data;
406  }
$columns
Definition: default.phtml:15

◆ getTemplateBlock()

getTemplateBlock ( )
protected

Get backend abstract block

Returns
\Magento\Backend\Test\Block\Template

Definition at line 210 of file Grid.php.

211  {
212  return Factory::getBlockFactory()->getMagentoBackendTemplate(
213  $this->_rootElement->find($this->templateBlock, Locator::SELECTOR_XPATH)
214  );
215  }

◆ isFirstRowVisible()

isFirstRowVisible ( )

Check whether first row is visible

Returns
bool

Definition at line 461 of file Grid.php.

462  {
463  return $this->_rootElement->find($this->firstRowSelector, Locator::SELECTOR_XPATH)->isVisible();
464  }

◆ isRowVisible()

isRowVisible ( array  $filter,
  $isSearchable = true,
  $isStrict = true 
)

Check if specific row exists in grid

Parameters
array$filter
bool$isSearchable
bool$isStrict
Returns
bool

Definition at line 416 of file Grid.php.

417  {
418  $this->waitLoader();
419  if ($isSearchable) {
420  $this->search($filter);
421  }
422 
423  return $this->getRow($filter, $isStrict)->isVisible();
424  }
getRow(array $filter, $isStrict=true)
Definition: Grid.php:369

◆ massaction()

massaction ( array  $items,
  $action,
  $acceptAlert = false,
  $massActionSelection = '' 
)

Perform selected massaction over checked items.

Parameters
array$items
array | string$action[array -> key = value from first select; value => value from subselect]
bool$acceptAlert[optional]
string$massActionSelection[optional]
Returns
void

Definition at line 319 of file Grid.php.

320  {
321  if ($this->_rootElement->find($this->noRecords)->isVisible()) {
322  return;
323  }
324  if (!is_array($action)) {
325  $action = [$action => '-'];
326  }
327  foreach ($items as $item) {
328  $this->searchAndSelect($item);
329  }
330  if ($massActionSelection) {
331  $this->_rootElement->find($this->massactionAction, Locator::SELECTOR_CSS, 'select')
332  ->setValue($massActionSelection);
333  }
334  $actionType = key($action);
335  $this->_rootElement->find($this->massactionSelect, Locator::SELECTOR_CSS, 'select')->setValue($actionType);
336  if (isset($action[$actionType]) && $action[$actionType] != '-') {
337  $this->_rootElement->find($this->option, Locator::SELECTOR_CSS, 'select')->setValue($action[$actionType]);
338  }
339  $this->massActionSubmit($acceptAlert);
340  }
$items

◆ nextPage()

nextPage ( )
protected

Click to next page action link

Returns
bool

Definition at line 446 of file Grid.php.

447  {
448  if ($this->_rootElement->find($this->actionNextPageDisabled)->isVisible()) {
449  return false;
450  }
451  $this->_rootElement->find($this->actionNextPage)->click();
452  $this->waitLoader();
453  return true;
454  }

◆ openFirstRow()

openFirstRow ( )

Open first item in grid

Returns
void

Definition at line 471 of file Grid.php.

472  {
473  $this->waitLoader();
474  $this->_rootElement->find($this->firstRowSelector, Locator::SELECTOR_XPATH)->click();
475  }

◆ prepareForSearch()

prepareForSearch ( array  $filters)
protected

Prepare data to perform search, fill in search filter

Parameters
array$filters
Exceptions

Definition at line 223 of file Grid.php.

224  {
225  foreach ($filters as $key => $value) {
226  if (isset($this->filters[$key])) {
227  $selector = $this->filters[$key]['selector'];
228  $strategy = isset($this->filters[$key]['strategy'])
229  ? $this->filters[$key]['strategy']
230  : Locator::SELECTOR_CSS;
231  $typifiedElement = isset($this->filters[$key]['input'])
232  ? $this->filters[$key]['input']
233  : null;
234  $this->_rootElement->find($selector, $strategy, $typifiedElement)->setValue($value);
235  } else {
236  throw new \Exception("Column $key is absent in the grid or not described yet.");
237  }
238  }
239  }
$value
Definition: gender.phtml:16

◆ resetFilter()

resetFilter ( )

Press 'Reset' button

Definition at line 303 of file Grid.php.

304  {
305  $this->waitLoader();
306  $this->_rootElement->find($this->resetButton)->click();
307  $this->waitLoader();
308  }

◆ search()

search ( array  $filter)

Search item via grid filter

Parameters
array$filter

Definition at line 246 of file Grid.php.

247  {
248  $this->resetFilter();
249  $this->prepareForSearch($filter);
250  $this->_rootElement->find($this->searchButton, Locator::SELECTOR_CSS)->click();
251  $this->waitLoader();
252  }

◆ searchAndOpen()

searchAndOpen ( array  $filter)

Search item and open it

Parameters
array$filter
Exceptions

Definition at line 260 of file Grid.php.

261  {
262  $this->search($filter);
263  $rowItem = $this->getRow($filter);
264  if ($rowItem->isVisible()) {
265  $rowItem->find($this->editLink, Locator::SELECTOR_CSS)->click();
266  } else {
267  throw new \Exception("Searched item was not found by filter\n" . print_r($filter, true));
268  }
269  $this->waitLoader();
270  }
getRow(array $filter, $isStrict=true)
Definition: Grid.php:369

◆ searchAndSelect()

searchAndSelect ( array  $filter)

Search for item and select it

Parameters
array$filter
Exceptions

Definition at line 289 of file Grid.php.

290  {
291  $this->search($filter);
292  $selectItem = $this->getRow($filter)->find($this->selectItem);
293  if ($selectItem->isVisible()) {
294  $selectItem->click();
295  } else {
296  throw new \Exception("Searched item was not found by filter\n" . print_r($filter, true));
297  }
298  }
getRow(array $filter, $isStrict=true)
Definition: Grid.php:369

◆ sortGridByField()

sortGridByField (   $field,
  $sort = "desc" 
)

Sort grid by field

Parameters
$field
string$sort

Definition at line 432 of file Grid.php.

433  {
434  $sortBlock = $this->_rootElement->find(sprintf($this->sortLink, $field, $sort));
435  if ($sortBlock->isVisible()) {
436  $sortBlock->click();
437  $this->waitLoader();
438  }
439  }

◆ waitLoader()

waitLoader ( )
protected

Wait loader

Returns
void

Definition at line 277 of file Grid.php.

278  {
279  $this->waitForElementNotVisible($this->loader);
280  $this->getTemplateBlock()->waitLoader();
281  }

◆ xpathEscape()

xpathEscape (   $query,
  $defaultDelim = '"' 
)
protected

Escape single and/or double quotes in XPath selector by concat()

Parameters
string$query
string$defaultDelim[optional]
Returns
string

Definition at line 484 of file Grid.php.

485  {
486  if (strpos($query, $defaultDelim) === false) {
487  return $defaultDelim . $query . $defaultDelim;
488  }
489  preg_match_all("#(?:('+)|[^']+)#", $query, $matches);
490  list($parts, $apos) = $matches;
491  $delim = '';
492  foreach ($parts as $i => &$part) {
493  $delim = $apos[$i] ? '"' : "'";
494  $part = $delim . $part . $delim;
495  }
496  if (count($parts) == 1) {
497  $parts[] = $delim . $delim;
498  }
499 
500  return 'concat(' . implode(',', $parts) . ')';
501  }
$i
Definition: gallery.phtml:31

Field Documentation

◆ $actionNextPage

$actionNextPage = '[class*=data-grid-pager] .action-next'
protected

Definition at line 168 of file Grid.php.

◆ $actionNextPageDisabled

$actionNextPageDisabled = '[class*=data-grid-pager] .action-next.disabled'
protected

Definition at line 175 of file Grid.php.

◆ $active

$active = '[class=*_active]'
protected

Definition at line 140 of file Grid.php.

◆ $confirmModal

$confirmModal = '.confirm._show[data-role=modal]'
protected

Definition at line 203 of file Grid.php.

◆ $editLink

$editLink = 'td[class*=col-action] a'
protected

Definition at line 70 of file Grid.php.

◆ $filters

$filters = []
protected

Definition at line 28 of file Grid.php.

◆ $firstRowSelector

$firstRowSelector = ''
protected

Definition at line 182 of file Grid.php.

◆ $lastRowItem

$lastRowItem = 'tbody tr:last-child'
protected

Definition at line 63 of file Grid.php.

◆ $loader

$loader = '[data-role="spinner"]'
protected

Definition at line 161 of file Grid.php.

◆ $massactionAction

$massactionAction = '[data-menu="grid-mass-select"]'
protected

Definition at line 98 of file Grid.php.

◆ $massactionSelect

$massactionSelect = '[id*=massaction-select]'
protected

Definition at line 91 of file Grid.php.

◆ $massactionSubmit

$massactionSubmit = '[id*=massaction-form] button'
protected

Definition at line 105 of file Grid.php.

◆ $noRecords

$noRecords = '.empty-text'
protected

Definition at line 189 of file Grid.php.

◆ $option

$option = '[name="status"]'
protected

Definition at line 133 of file Grid.php.

◆ $resetButton

$resetButton = '[data-action="grid-filter-reset"]'
protected

Definition at line 49 of file Grid.php.

◆ $rowItem

$rowItem = 'tbody tr'
protected

Definition at line 56 of file Grid.php.

◆ $rowPattern

$rowPattern = '//tbody/tr[%s]'
protected

Definition at line 196 of file Grid.php.

◆ $rowTemplate

$rowTemplate = 'td[contains(.,normalize-space("%s"))]'
protected

Definition at line 147 of file Grid.php.

◆ $rowTemplateStrict

$rowTemplateStrict = 'td[text()[normalize-space()="%s"]]'
protected

Definition at line 154 of file Grid.php.

◆ $searchButton

$searchButton = '[data-action="grid-filter-apply"]'
protected

Definition at line 35 of file Grid.php.

◆ $selectAll

$selectAll = '.massaction a[onclick*=".selectAll()"]'
protected

Definition at line 84 of file Grid.php.

◆ $selectItem

$selectItem = 'tbody tr [type="checkbox"]'
protected

Definition at line 77 of file Grid.php.

◆ $sortLink

$sortLink = "[name='%s'][title='%s']"
protected

Definition at line 42 of file Grid.php.

◆ $templateBlock

$templateBlock = './ancestor::body'
protected

Definition at line 112 of file Grid.php.

◆ $waitForSelectorType

$waitForSelectorType = Locator::SELECTOR_CSS
protected

Definition at line 119 of file Grid.php.

◆ $waitForSelectorVisible

$waitForSelectorVisible = true
protected

Definition at line 126 of file Grid.php.


The documentation for this class was generated from the following file: