From 7e565f6adcecc2aa41e7639f67529a6a852ffaa3 Mon Sep 17 00:00:00 2001 From: Carlo Tasca Date: Tue, 17 May 2016 13:42:39 +0100 Subject: [PATCH 1/2] Allow elements to be passed to Spinner --- magefix/src/Magefix/Plugin/Spinner.php | 30 +++++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/magefix/src/Magefix/Plugin/Spinner.php b/magefix/src/Magefix/Plugin/Spinner.php index 79912ef..b6a9bb7 100644 --- a/magefix/src/Magefix/Plugin/Spinner.php +++ b/magefix/src/Magefix/Plugin/Spinner.php @@ -38,12 +38,15 @@ public function spin($lambda, $wait = 60) /** * Spin until element is visible. Default timeout is 60 seconds. * - * @param string $element + * @param string|Object $element * @param int $wait */ public function spinUntilVisible($element, $wait = 60) { - $this->spin(function($context) use ($element) { + $this->spin(function ($context) use ($element) { + if (is_object($element)) { + return $element->isVisible(); + } return $context->getElement($element)->isVisible(); }, $wait); } @@ -51,12 +54,15 @@ public function spinUntilVisible($element, $wait = 60) /** * Spin until element is not visible. Default timeout is 60 seconds. * - * @param string $element + * @param string|Object $element * @param int $wait */ public function spinUntilInvisible($element, $wait = 60) { - $this->spin(function($context) use ($element) { + $this->spin(function ($context) use ($element) { + if (is_object($element)) { + return $element->isVisible() == false; + } return ($context->getElement($element)->isVisible() == false); }, $wait); } @@ -64,12 +70,16 @@ public function spinUntilInvisible($element, $wait = 60) /** * Spin and click element. Default timeout is 60 seconds. * - * @param string $element + * @param string|Object $element * @param int $wait */ public function spinAndClick($element, $wait = 60) { - $this->spin(function($context) use ($element) { + $this->spin(function ($context) use ($element) { + if (is_object($element)) { + $element->click(); + return true; + } $context->getElement($element)->click(); return true; }, $wait); @@ -78,12 +88,16 @@ public function spinAndClick($element, $wait = 60) /** * Spin and press element. Default timeout is 60 seconds. * - * @param string $element + * @param string|Object $element * @param int $wait */ public function spinAndPress($element, $wait = 60) { - $this->spin(function($context) use ($element) { + $this->spin(function ($context) use ($element) { + if (is_object($element)) { + $element->press(); + return true; + } $context->getElement($element)->press(); return true; }, $wait); From 9f7b7f0ea168f0d094dc7ba7f52f27232e8cb681 Mon Sep 17 00:00:00 2001 From: Carlo Tasca Date: Tue, 17 May 2016 14:04:55 +0100 Subject: [PATCH 2/2] Abstract spinner operation --- magefix/src/Magefix/Plugin/Spinner.php | 48 +++++++++++++++----------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/magefix/src/Magefix/Plugin/Spinner.php b/magefix/src/Magefix/Plugin/Spinner.php index b6a9bb7..8775d12 100644 --- a/magefix/src/Magefix/Plugin/Spinner.php +++ b/magefix/src/Magefix/Plugin/Spinner.php @@ -44,10 +44,7 @@ public function spin($lambda, $wait = 60) public function spinUntilVisible($element, $wait = 60) { $this->spin(function ($context) use ($element) { - if (is_object($element)) { - return $element->isVisible(); - } - return $context->getElement($element)->isVisible(); + return $this->_spinnerAction($context, $element, 'isVisible', true); }, $wait); } @@ -60,10 +57,7 @@ public function spinUntilVisible($element, $wait = 60) public function spinUntilInvisible($element, $wait = 60) { $this->spin(function ($context) use ($element) { - if (is_object($element)) { - return $element->isVisible() == false; - } - return ($context->getElement($element)->isVisible() == false); + return $this->_spinnerAction($context, $element, 'isVisible', false); }, $wait); } @@ -76,12 +70,7 @@ public function spinUntilInvisible($element, $wait = 60) public function spinAndClick($element, $wait = 60) { $this->spin(function ($context) use ($element) { - if (is_object($element)) { - $element->click(); - return true; - } - $context->getElement($element)->click(); - return true; + return $this->_spinnerAction($context, $element, 'click'); }, $wait); } @@ -94,12 +83,7 @@ public function spinAndClick($element, $wait = 60) public function spinAndPress($element, $wait = 60) { $this->spin(function ($context) use ($element) { - if (is_object($element)) { - $element->press(); - return true; - } - $context->getElement($element)->press(); - return true; + return $this->_spinnerAction($context, $element, 'press'); }, $wait); } @@ -115,4 +99,28 @@ protected function _throwBacktraceException() $backtrace[1]['file'] . ", line " . $backtrace[1]['line'] ); } + + /** + * @param $context + * @param $element + * @param $action + * @param null $condition + * @return bool + */ + private function _spinnerAction($context, $element, $action, $condition = null) + { + if (is_object($element)) { + if (!is_null($condition)) { + return $element->$action() === $condition; + } + $element->$action(); + } else { + if (!is_null($condition)) { + return $context->getElement($element)->$action() === $condition; + } + $context->getElement($element)->$action(); + } + + return true; + } }