Skip to content

Commit d098325

Browse files
committed
#54: Set a global value for timeouts that's used across all wait related actions
- Reverted MagentoWebDriver class - Moved getDefaultWaitTimeout method to ActionObject class - Changed generateStepsPhp method in TestGenerator class to use timeout from getDefaultWaitTimeout method if not set - Moved and fixed unit test for the implementation
1 parent 6381a4b commit d098325

File tree

6 files changed

+28
-177
lines changed

6 files changed

+28
-177
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ MODULE_WHITELIST=Magento_Framework,Magento_ConfigurableProductWishlist,Magento_C
3636
#MFTF_DEBUG=
3737

3838
#*** Default timeout for wait actions
39-
WAIT_TIMEOUT=20
39+
WAIT_TIMEOUT=10
4040
#*** End of .env ***#

dev/tests/unit/Magento/FunctionalTestFramework/Module/MagentoWebDriverTest.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

dev/tests/unit/Magento/FunctionalTestFramework/Test/Objects/ActionObjectTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,19 @@ public function testTooManyArgumentException()
291291
$actionObject->resolveReferences();
292292
}
293293

294+
/**
295+
* Method should return either .env file value or constant value
296+
*/
297+
public function testGetDefaultWaitTimeout()
298+
{
299+
$this->assertEquals(ActionObject::getDefaultWaitTimeout(), ActionObject::DEFAULT_WAIT_TIMEOUT);
300+
301+
$envFile = new \Dotenv\Dotenv(__DIR__ . '/../../../../../../../', '.env.example');
302+
$envFile->load();
303+
304+
$this->assertEquals(ActionObject::getDefaultWaitTimeout(), getenv('WAIT_TIMEOUT'));
305+
}
306+
294307
private function mockSectionHandlerWithElement($elementObject)
295308
{
296309
$sectionObject = new SectionObject('SectionObject', ['elementObject' => $elementObject]);

src/Magento/FunctionalTestingFramework/Module/MagentoWebDriver.php

Lines changed: 2 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313
use Facebook\WebDriver\WebDriverSelect;
1414
use Facebook\WebDriver\WebDriverBy;
1515
use Facebook\WebDriver\Exception\NoSuchElementException;
16-
use Facebook\WebDriver\WebDriverExpectedCondition;
1716
use Codeception\Exception\ElementNotFound;
1817
use Codeception\Exception\ModuleConfigException;
1918
use Codeception\Exception\ModuleException;
2019
use Codeception\Util\Uri;
2120
use Codeception\Util\ActionSequence;
22-
use Codeception\Util\Locator;
2321
use Magento\FunctionalTestingFramework\Util\Protocol\CurlTransport;
2422
use Magento\FunctionalTestingFramework\Util\Protocol\CurlInterface;
2523
use Magento\Setup\Exception;
@@ -57,8 +55,6 @@ class MagentoWebDriver extends WebDriver
5755
'//div[@data-role="spinner"]'
5856
];
5957

60-
const DEFAULT_WAIT_TIMEOUT = 10;
61-
6258
/**
6359
* The module required fields, to be set in the suite .yml configuration file.
6460
*
@@ -119,16 +115,6 @@ public function _resetConfig()
119115
$this->config = ConfigSanitizerUtil::sanitizeWebDriverConfig($this->config);
120116
}
121117

122-
/**
123-
* Retrieve default timeout in seconds for 'wait*' actions
124-
*
125-
* @return int
126-
*/
127-
public static function getDefaultWaitTimeout()
128-
{
129-
return getenv('WAIT_TIMEOUT') ?: self::DEFAULT_WAIT_TIMEOUT;
130-
}
131-
132118
/**
133119
* Returns URL of a host.
134120
*
@@ -315,148 +301,14 @@ public function selectMultipleOptions($selectSearchTextField, $selectSearchResul
315301
}
316302
}
317303

318-
/**
319-
* Wait for $timeout seconds.
320-
*
321-
* @param int|float $timeout secs
322-
* @throws \Codeception\Exception\TestRuntimeException
323-
*/
324-
public function wait($timeout = null)
325-
{
326-
$timeout = $timeout ?? self::getDefaultWaitTimeout();
327-
328-
if ($timeout >= 1000) {
329-
throw new TestRuntimeException(
330-
"
331-
Waiting for more then 1000 seconds: 16.6667 mins\n
332-
Please note that wait method accepts number of seconds as parameter."
333-
);
334-
}
335-
usleep($timeout * 1000000);
336-
}
337-
338-
/**
339-
* Waits up to $timeout seconds for the given element to change.
340-
*
341-
* @param $element
342-
* @param \Closure $callback
343-
* @param int $timeout seconds
344-
* @throws \Codeception\Exception\ElementNotFound
345-
* @throws \Exception
346-
*/
347-
public function waitForElementChange($element, \Closure $callback, $timeout = null)
348-
{
349-
$timeout = $timeout ?? self::getDefaultWaitTimeout();
350-
351-
$el = $this->matchFirstOrFail($this->baseElement, $element);
352-
$checker = function () use ($el, $callback) {
353-
return $callback($el);
354-
};
355-
$this->webDriver->wait($timeout)->until($checker);
356-
}
357-
358-
/**
359-
* Waits up to $timeout seconds for an element to appear on the page.
360-
*
361-
* @param $element
362-
* @param int $timeout seconds
363-
* @throws \Exception
364-
*/
365-
public function waitForElement($element, $timeout = null)
366-
{
367-
$timeout = $timeout ?? self::getDefaultWaitTimeout();
368-
369-
$condition = WebDriverExpectedCondition::presenceOfElementLocated($this->getLocator($element));
370-
$this->webDriver->wait($timeout)->until($condition);
371-
}
372-
373-
/**
374-
* Waits up to $timeout seconds for the given element to be visible on the page.
375-
*
376-
* @param $element
377-
* @param int $timeout seconds
378-
* @throws \Exception
379-
*/
380-
public function waitForElementVisible($element, $timeout = null)
381-
{
382-
$timeout = $timeout ?? self::getDefaultWaitTimeout();
383-
384-
$condition = WebDriverExpectedCondition::visibilityOfElementLocated($this->getLocator($element));
385-
$this->webDriver->wait($timeout)->until($condition);
386-
}
387-
388-
/**
389-
* Waits up to $timeout seconds for the given element to become invisible.
390-
*
391-
* @param $element
392-
* @param int $timeout seconds
393-
* @throws \Exception
394-
*/
395-
public function waitForElementNotVisible($element, $timeout = null)
396-
{
397-
$timeout = $timeout ?? self::getDefaultWaitTimeout();
398-
399-
$condition = WebDriverExpectedCondition::invisibilityOfElementLocated($this->getLocator($element));
400-
$this->webDriver->wait($timeout)->until($condition);
401-
}
402-
403-
/**
404-
* Waits up to $timeout seconds for the given string to appear on the page.
405-
*
406-
* @param string $text
407-
* @param int $timeout seconds
408-
* @param string $selector optional
409-
* @throws \Exception
410-
*/
411-
public function waitForText($text, $timeout = null, $selector = null)
412-
{
413-
$timeout = $timeout ?? self::getDefaultWaitTimeout();
414-
415-
$message = sprintf(
416-
'Waited for %d secs but text %s still not found',
417-
$timeout,
418-
Locator::humanReadableString($text)
419-
);
420-
if (!$selector) {
421-
$condition = WebDriverExpectedCondition::elementTextContains(WebDriverBy::xpath('//body'), $text);
422-
$this->webDriver->wait($timeout)->until($condition, $message);
423-
return;
424-
}
425-
426-
$condition = WebDriverExpectedCondition::elementTextContains($this->getLocator($selector), $text);
427-
$this->webDriver->wait($timeout)->until($condition, $message);
428-
}
429-
430-
/**
431-
* Executes JavaScript and waits up to $timeout seconds for it to return true.
432-
*
433-
* @param string $script
434-
* @param int $timeout seconds
435-
* @throws \Exception
436-
*/
437-
public function waitForJS($script, $timeout = null)
438-
{
439-
$timeout = $timeout ?? self::getDefaultWaitTimeout();
440-
441-
$condition = function ($wd) use ($script) {
442-
return $wd->executeScript($script);
443-
};
444-
$message = sprintf(
445-
'Waited for %d secs but script %s still not executed',
446-
$timeout,
447-
Locator::humanReadableString($script)
448-
);
449-
$this->webDriver->wait($timeout)->until($condition, $message);
450-
}
451-
452304
/**
453305
* Wait for all Ajax calls to finish.
454306
*
455307
* @param int $timeout
456308
*/
457309
public function waitForAjaxLoad($timeout = null)
458310
{
459-
$timeout = $timeout ?? self::getDefaultWaitTimeout();
311+
$timeout = $timeout ?? $this->_getConfig()['pageload_timeout'];
460312

461313
try {
462314
$this->waitForJS('return !!window.jQuery && window.jQuery.active == 0;', $timeout);
@@ -475,7 +327,7 @@ public function waitForAjaxLoad($timeout = null)
475327
*/
476328
public function waitForPageLoad($timeout = null)
477329
{
478-
$timeout = $timeout ?? self::getDefaultWaitTimeout();
330+
$timeout = $timeout ?? $this->_getConfig()['pageload_timeout'];
479331

480332
$this->waitForJS('return document.readyState == "complete"', $timeout);
481333
$this->waitForAjaxLoad($timeout);

src/Magento/FunctionalTestingFramework/Test/Objects/ActionObject.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ActionObject
4242
const ACTION_ATTRIBUTE_VARIABLE_REGEX_PARAMETER = '/\(.+\)/';
4343
const ACTION_ATTRIBUTE_VARIABLE_REGEX_PATTERN = '/{{[\w]+\.[\w\[\]]+}}/';
4444
const ACTION_ATTRIBUTE_VARIABLE_REGEX_PATTERN_WITH_PARAMS= '/{{[\w]+\.[\w]+\(.+\)}}/';
45+
const DEFAULT_WAIT_TIMEOUT = 10;
4546

4647
/**
4748
* The unique identifier for the action
@@ -128,6 +129,16 @@ public function __construct(
128129
}
129130
}
130131

132+
/**
133+
* Retrieve default timeout in seconds for 'wait*' actions
134+
*
135+
* @return int
136+
*/
137+
public static function getDefaultWaitTimeout()
138+
{
139+
return getenv('WAIT_TIMEOUT') ?: self::DEFAULT_WAIT_TIMEOUT;
140+
}
141+
131142
/**
132143
* This function returns the string property stepKey.
133144
*

src/Magento/FunctionalTestingFramework/Util/TestGenerator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ private function generateStepsPhp($actionObjects, $hookObject = false)
492492
if (isset($customActionAttributes['timeout'])) {
493493
$time = $customActionAttributes['timeout'];
494494
}
495+
$time = $time ?? ActionObject::getDefaultWaitTimeout();
495496

496497
if (isset($customActionAttributes['parameterArray']) && $actionObject->getType() != 'pressKey') {
497498
// validate the param array is in the correct format

0 commit comments

Comments
 (0)