Skip to content

Commit 07d9000

Browse files
committed
MAGETWO-61877: UI Upgrade from 2.0.x to 2.0.11 Test Failure with Sample Data
- refactoring code to better readability
1 parent 67e81e4 commit 07d9000

File tree

1 file changed

+133
-41
lines changed

1 file changed

+133
-41
lines changed

dev/tests/functional/tests/app/Magento/Upgrade/Test/Block/SelectVersion.php

Lines changed: 133 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,56 +66,148 @@ public function chooseUpgradeOtherComponents()
6666

6767
/**
6868
* Set maximum compatible sample data for each row
69+
* Iterates through each page of the grid and sets the compatible version from fixture
6970
*
7071
* @param string $sampleDataVersion
7172
* @return void
72-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
7373
*/
7474
public function chooseVersionUpgradeOtherComponents($sampleDataVersion)
7575
{
76-
$perPageSelect= $this->_rootElement->find(
77-
"select#perPage",
78-
Locator::SELECTOR_CSS,
76+
do {
77+
$this->iterateAndSetComponentsRows($this->convertVersionFixtureToRegexp($sampleDataVersion));
78+
if ($this->canClickOnNextPage()) {
79+
$this->clickOnNextPage();
80+
}
81+
} while ($this->canClickOnNextPage());
82+
}
83+
84+
/**
85+
* Gets components rows as ElementInterface
86+
*
87+
* @return \Magento\Mtf\Client\ElementInterface[]
88+
*/
89+
private function getComponentsTableRows()
90+
{
91+
return $this->_rootElement->getElements("table.data-grid tbody tr");
92+
}
93+
94+
/**
95+
* Iterate through components in table and set compatible version for selected magento version
96+
*
97+
* @param $sampleDataVersion
98+
* @return void
99+
*/
100+
private function iterateAndSetComponentsRows($sampleDataVersion)
101+
{
102+
$rows = $this->getComponentsTableRows();
103+
for ($rowIndex = 1; $rowIndex <= count($rows); $rowIndex++) {
104+
$textElement = $this->getRowComponentTitle($rowIndex);
105+
if ($this->titleContainsSampleData($textElement)) {
106+
$this->setSampleDataVersionToRowSelect($rowIndex, $sampleDataVersion);
107+
}
108+
}
109+
}
110+
111+
/**
112+
* Clicks on Next Page of the grid
113+
*
114+
* @return void
115+
*/
116+
private function clickOnNextPage()
117+
{
118+
$this->_rootElement->find(".admin__data-grid-pager .action-next", Locator::SELECTOR_CSS)->click();
119+
}
120+
121+
/**
122+
* Can click on next page
123+
*
124+
* @return bool
125+
*/
126+
private function canClickOnNextPage()
127+
{
128+
return !$this->_rootElement->find(
129+
".admin__data-grid-pager .action-next",
130+
Locator::SELECTOR_CSS
131+
)->isDisabled();
132+
}
133+
134+
/**
135+
* Gets rows component title
136+
*
137+
* @param int $rowIndex
138+
* @return \Magento\Mtf\Client\ElementInterface
139+
*/
140+
private function getRowComponentTitle($rowIndex)
141+
{
142+
return $this->_rootElement->find(
143+
"//table//tbody//tr[" . $rowIndex . "]//td//*[contains(text(),'sample')]",
144+
Locator::SELECTOR_XPATH
145+
);
146+
}
147+
148+
/**
149+
* Gets the select element from row
150+
*
151+
* @param int $rowIndex
152+
* @return \Magento\Mtf\Client\ElementInterface
153+
*/
154+
private function getSelectFromRow($rowIndex)
155+
{
156+
return $this->_rootElement->find(
157+
'//table//tbody//tr[' . $rowIndex . ']//td//select',
158+
Locator::SELECTOR_XPATH,
79159
'select'
80160
);
81-
$fixtureVersion = $sampleDataVersion;
82-
$perPageSelect->setValue(200);
83-
sleep(1);
84-
85-
$elementsArray= $this->_rootElement->getElements("table.data-grid tbody tr");
86-
87-
foreach ($elementsArray as $key => $rowElement) {
88-
$textElement = $this->_rootElement->find(
89-
"//table//tbody//tr[" . ($key + 1) . "]//td//*[contains(text(),'sample')]",
90-
Locator::SELECTOR_XPATH
91-
);
92-
if (preg_match('/magento.+sample.+data/', $textElement->getText())) {
93-
$selectElement = $this->_rootElement->find(
94-
'//table//tbody//tr[' . ($key +1) . ']//td//select',
95-
Locator::SELECTOR_XPATH,
96-
'select'
97-
);
98-
99-
$fixtureVersion = str_replace('*', '[0-9]+', $fixtureVersion);
100-
$toSelectValue = $selectElement->getValue();
101-
$toSelectValueOriginal = $toSelectValue;
102-
$toSelectVersion = '0';
103-
foreach ($selectElement->getElements('option') as $option) {
104-
$optionText = $option->getText();
105-
if (preg_match('/' . $fixtureVersion .'/', $optionText)) {
106-
if (preg_match('/([0-9\.\-a-zA-Z]+)/', $optionText, $match)) {
107-
if (!empty($match) > 0) {
108-
if (version_compare($match[0], $toSelectVersion, '>')) {
109-
$toSelectVersion = $match[0];
110-
$toSelectValue = $optionText;
111-
}
112-
}
113-
}
114-
}
115-
}
161+
}
162+
163+
/**
164+
* Convert sample data version fixture to regexp format
165+
* Example 100.1.* to 100\.1\.[0-9]+
166+
*
167+
* @param string $sampleDataVersion
168+
* @return string
169+
* @throws \Exception
170+
*/
171+
private function convertVersionFixtureToRegexp($sampleDataVersion)
172+
{
173+
if (!preg_match('/\d+(?:\.*\d*)*/', $sampleDataVersion)) {
174+
throw new \Exception('Wrong format for sample data version fixture. Example: 100.1.* needed.');
175+
}
176+
return str_replace('*', '[0-9]+', $sampleDataVersion);
177+
}
178+
179+
/**
180+
* Asserts if element's text contains sample data
181+
*
182+
* @param \Magento\Mtf\Client\ElementInterface $element
183+
* @return bool
184+
*/
185+
private function titleContainsSampleData($element)
186+
{
187+
return preg_match('/magento.+sample.+data/', $element->getText());
188+
}
116189

117-
if ($toSelectValue !== $toSelectValueOriginal) {
118-
$selectElement->setValue($toSelectValue);
190+
191+
/**
192+
* Sets sample data version matching the maximum compatible version from fixture
193+
*
194+
* @param int $rowIndex
195+
* @param string $sampleDataVersionForRegex
196+
* @return void
197+
*/
198+
private function setSampleDataVersionToRowSelect($rowIndex, $sampleDataVersionForRegex)
199+
{
200+
$selectElement = $this->getSelectFromRow($rowIndex);
201+
$toSelectVersion = '0';
202+
foreach ($selectElement->getElements('option') as $option) {
203+
$optionText = $option->getText();
204+
if (preg_match('/' . $sampleDataVersionForRegex . '/', $optionText)
205+
&& preg_match('/([0-9\.\-a-zA-Z]+)/', $optionText, $match)
206+
) {
207+
$extractedVersion = reset($match);
208+
if (version_compare($extractedVersion, $toSelectVersion, '>')) {
209+
$toSelectVersion = $extractedVersion;
210+
$selectElement->setValue($optionText);
119211
}
120212
}
121213
}

0 commit comments

Comments
 (0)