From 6b951c6745ec4f45f453540f445aad3ac6f9fcd9 Mon Sep 17 00:00:00 2001 From: Alexander Obuhovich Date: Fri, 24 Jun 2016 15:10:16 +0300 Subject: [PATCH 1/4] Make "$page_factory" argument required during proxy construction --- library/QATools/QATools/BEM/Proxy/AbstractPartProxy.php | 2 +- library/QATools/QATools/BEM/Proxy/ElementProxy.php | 2 +- library/QATools/QATools/PageObject/Proxy/AbstractProxy.php | 2 +- .../QATools/PageObject/Proxy/WebElementCollectionProxy.php | 2 +- library/QATools/QATools/PageObject/Proxy/WebElementProxy.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/library/QATools/QATools/BEM/Proxy/AbstractPartProxy.php b/library/QATools/QATools/BEM/Proxy/AbstractPartProxy.php index 15e245e7..5ed07049 100644 --- a/library/QATools/QATools/BEM/Proxy/AbstractPartProxy.php +++ b/library/QATools/QATools/BEM/Proxy/AbstractPartProxy.php @@ -40,7 +40,7 @@ abstract class AbstractPartProxy extends AbstractProxy implements IPart * @param BEMElementLocator $locator Locator. * @param IPageFactory $page_factory Page factory. */ - public function __construct($name, BEMElementLocator $locator, IPageFactory $page_factory = null) + public function __construct($name, BEMElementLocator $locator, IPageFactory $page_factory) { if ( !$this->elementClass ) { $this->elementClass = '\\QATools\\QATools\\BEM\\Element\\IPart'; diff --git a/library/QATools/QATools/BEM/Proxy/ElementProxy.php b/library/QATools/QATools/BEM/Proxy/ElementProxy.php index fec2c117..60d2b70c 100644 --- a/library/QATools/QATools/BEM/Proxy/ElementProxy.php +++ b/library/QATools/QATools/BEM/Proxy/ElementProxy.php @@ -33,7 +33,7 @@ class ElementProxy extends AbstractPartProxy implements IElement * @param BEMElementLocator $locator Locator. * @param IPageFactory $page_factory Page factory. */ - public function __construct($name, BEMElementLocator $locator, IPageFactory $page_factory = null) + public function __construct($name, BEMElementLocator $locator, IPageFactory $page_factory) { $this->className = '\\QATools\\QATools\\BEM\\Element\\Element'; $this->elementClass = '\\QATools\\QATools\\BEM\\Element\\IElement'; diff --git a/library/QATools/QATools/PageObject/Proxy/AbstractProxy.php b/library/QATools/QATools/PageObject/Proxy/AbstractProxy.php index 32840324..fc4bd36a 100644 --- a/library/QATools/QATools/PageObject/Proxy/AbstractProxy.php +++ b/library/QATools/QATools/PageObject/Proxy/AbstractProxy.php @@ -69,7 +69,7 @@ abstract class AbstractProxy extends AbstractElementCollection implements IProxy * @param IElementLocator $locator Element selector. * @param IPageFactory $page_factory Page factory. */ - public function __construct(IElementLocator $locator, IPageFactory $page_factory = null) + public function __construct(IElementLocator $locator, IPageFactory $page_factory) { $this->locator = $locator; $this->pageFactory = $page_factory; diff --git a/library/QATools/QATools/PageObject/Proxy/WebElementCollectionProxy.php b/library/QATools/QATools/PageObject/Proxy/WebElementCollectionProxy.php index 7a37fbc1..5604546a 100644 --- a/library/QATools/QATools/PageObject/Proxy/WebElementCollectionProxy.php +++ b/library/QATools/QATools/PageObject/Proxy/WebElementCollectionProxy.php @@ -32,7 +32,7 @@ class WebElementCollectionProxy extends WebElementProxy * @param IElementLocator $locator Element selector. * @param IPageFactory $page_factory Page factory. */ - public function __construct(IElementLocator $locator, IPageFactory $page_factory = null) + public function __construct(IElementLocator $locator, IPageFactory $page_factory) { if ( !$this->className ) { $this->className = '\\QATools\\QATools\\PageObject\\Element\\WebElementCollection'; diff --git a/library/QATools/QATools/PageObject/Proxy/WebElementProxy.php b/library/QATools/QATools/PageObject/Proxy/WebElementProxy.php index b48a7dcc..ac5c145a 100644 --- a/library/QATools/QATools/PageObject/Proxy/WebElementProxy.php +++ b/library/QATools/QATools/PageObject/Proxy/WebElementProxy.php @@ -31,7 +31,7 @@ class WebElementProxy extends AbstractProxy implements IWebElement * @param IElementLocator $locator Element selector. * @param IPageFactory $page_factory Page factory. */ - public function __construct(IElementLocator $locator, IPageFactory $page_factory = null) + public function __construct(IElementLocator $locator, IPageFactory $page_factory) { if ( !$this->className ) { $this->className = '\\QATools\\QATools\\PageObject\\Element\\WebElement'; From c0e8cede92e38309ef78a9754a13b4a573066209 Mon Sep 17 00:00:00 2001 From: Alexander Obuhovich Date: Fri, 24 Jun 2016 15:19:36 +0300 Subject: [PATCH 2/4] Make "$page_factory" argument required during element construction --- .../QATools/BEM/Proxy/ElementProxy.php | 2 +- .../Element/AbstractElementContainer.php | 36 +++---------------- .../Element/AbstractTypifiedElement.php | 31 ++++++++++++---- .../QATools/HtmlElements/Element/Form.php | 15 ++++---- .../QATools/HtmlElements/Element/Select.php | 2 +- .../Element/AbstractElementCollection.php | 2 +- .../Element/AbstractElementContainer.php | 33 +++-------------- .../PageObject/Element/INodeElementAware.php | 2 +- .../QATools/PageObject/Element/WebElement.php | 27 +++++++++++--- .../PageObject/Exception/ElementException.php | 2 -- library/QATools/QATools/PageObject/Page.php | 2 +- .../Element/AbstractElementContainerTest.php | 12 ------- .../Element/AbstractTypifiedElementTest.php | 2 +- .../HtmlElements/Element/SelectOptionTest.php | 8 ++--- .../Element/TypifiedElementTestCase.php | 2 +- .../Element/AbstractElementContainerTest.php | 12 ------- .../Element/ElementCollectionTest.php | 4 +-- .../PageObject/Element/WebElementTest.php | 2 +- 18 files changed, 79 insertions(+), 117 deletions(-) diff --git a/library/QATools/QATools/BEM/Proxy/ElementProxy.php b/library/QATools/QATools/BEM/Proxy/ElementProxy.php index 60d2b70c..25c446f6 100644 --- a/library/QATools/QATools/BEM/Proxy/ElementProxy.php +++ b/library/QATools/QATools/BEM/Proxy/ElementProxy.php @@ -52,7 +52,7 @@ protected function locateObject() return; } - $web_element = WebElement::fromNodeElement($this->locateElement()); + $web_element = WebElement::fromNodeElement($this->locateElement(), $this->pageFactory); $this->object = new $this->className($this->getName(), $web_element); } diff --git a/library/QATools/QATools/HtmlElements/Element/AbstractElementContainer.php b/library/QATools/QATools/HtmlElements/Element/AbstractElementContainer.php index d42c4f9e..9c0f6134 100644 --- a/library/QATools/QATools/HtmlElements/Element/AbstractElementContainer.php +++ b/library/QATools/QATools/HtmlElements/Element/AbstractElementContainer.php @@ -25,13 +25,6 @@ abstract class AbstractElementContainer extends AbstractTypifiedElement implements IElementContainer { - /** - * Stores instance of used page factory. - * - * @var IPageFactory - */ - private $_pageFactory; - /** * Specifies wrapped WebElement. * @@ -40,21 +33,10 @@ abstract class AbstractElementContainer extends AbstractTypifiedElement implemen */ public function __construct(WebElement $wrapped_element, IPageFactory $page_factory) { - parent::__construct($wrapped_element); + parent::__construct($wrapped_element, $page_factory); - $this->_pageFactory = $page_factory; - $this->_pageFactory->initElementContainer($this); - $this->_pageFactory->initElements($this, $page_factory->createDecorator($this)); - } - - /** - * Returns page factory, used during object creation. - * - * @return IPageFactory - */ - protected function getPageFactory() - { - return $this->_pageFactory; + $page_factory->initElementContainer($this); + $page_factory->initElements($this, $page_factory->createDecorator($this)); } /** @@ -64,18 +46,10 @@ protected function getPageFactory() * @param IPageFactory $page_factory Page factory. * * @return static - * @throws TypifiedElementException When page factory is missing. */ - public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory = null) + public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory) { - if ( !isset($page_factory) ) { - throw new TypifiedElementException( - 'Page factory is required to create this element', - TypifiedElementException::TYPE_PAGE_FACTORY_REQUIRED - ); - } - - $wrapped_element = WebElement::fromNodeElement($node_element); + $wrapped_element = WebElement::fromNodeElement($node_element, $page_factory); return new static($wrapped_element, $page_factory); } diff --git a/library/QATools/QATools/HtmlElements/Element/AbstractTypifiedElement.php b/library/QATools/QATools/HtmlElements/Element/AbstractTypifiedElement.php index ccdb227a..3a2ab177 100644 --- a/library/QATools/QATools/HtmlElements/Element/AbstractTypifiedElement.php +++ b/library/QATools/QATools/HtmlElements/Element/AbstractTypifiedElement.php @@ -45,6 +45,13 @@ abstract class AbstractTypifiedElement implements ITypifiedElement, INodeElement */ private $_wrappedElement; + /** + * Stores instance of used page factory. + * + * @var IPageFactory + */ + private $_pageFactory; + /** * List of acceptance criteria. * @@ -55,11 +62,13 @@ abstract class AbstractTypifiedElement implements ITypifiedElement, INodeElement /** * Specifies wrapped WebElement. * - * @param WebElement $wrapped_element Element to be wrapped. + * @param WebElement $wrapped_element Element to be wrapped. + * @param IPageFactory $page_factory Page factory. */ - public function __construct(WebElement $wrapped_element) + public function __construct(WebElement $wrapped_element, IPageFactory $page_factory) { $this->_wrappedElement = $wrapped_element; + $this->_pageFactory = $page_factory; $this->assertWrappedElement(); } @@ -72,11 +81,11 @@ public function __construct(WebElement $wrapped_element) * * @return static */ - public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory = null) + public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory) { - $wrapped_element = WebElement::fromNodeElement($node_element); + $wrapped_element = WebElement::fromNodeElement($node_element, $page_factory); - return new static($wrapped_element); + return new static($wrapped_element, $page_factory); } /** @@ -206,6 +215,16 @@ public function getSession() return $this->getWrappedElement()->getSession(); } + /** + * Returns page factory, used during object creation. + * + * @return IPageFactory + */ + protected function getPageFactory() + { + return $this->_pageFactory; + } + /** * Checks whether current node is visible on page. * @@ -287,7 +306,7 @@ public function getXpathEscaper() */ protected function isSeleniumDriver() { - return is_a($this->getSession()->getDriver(), '\\Behat\\Mink\\Driver\\Selenium2Driver'); + return is_a($this->_pageFactory->getSession()->getDriver(), '\\Behat\\Mink\\Driver\\Selenium2Driver'); } /** diff --git a/library/QATools/QATools/HtmlElements/Element/Form.php b/library/QATools/QATools/HtmlElements/Element/Form.php index 21a3ea9c..f4bcaef6 100644 --- a/library/QATools/QATools/HtmlElements/Element/Form.php +++ b/library/QATools/QATools/HtmlElements/Element/Form.php @@ -91,31 +91,32 @@ public function typify(array $node_elements) $input_type = $node_element->getAttribute('type'); if ( $input_type == self::CHECKBOX_INPUT ) { - return Checkbox::fromNodeElement($node_element); + return Checkbox::fromNodeElement($node_element, $this->getPageFactory()); } elseif ( $input_type == self::RADIO_INPUT ) { - return RadioGroup::fromNodeElements($node_elements); + return RadioGroup::fromNodeElements($node_elements, null, $this->getPageFactory()); } elseif ( $input_type == self::FILE_INPUT ) { - return FileInput::fromNodeElement($node_element); + return FileInput::fromNodeElement($node_element, $this->getPageFactory()); } else { /*if ( is_null($input_type) || ($input_type == self::TEXT_INPUT) || ($input_type == self::PASSWORD_INPUT) ) {*/ - return TextInput::fromNodeElement($node_element); + return TextInput::fromNodeElement($node_element, $this->getPageFactory()); } } elseif ( $tag_name == 'select' ) { - return Select::fromNodeElement($node_element); + return Select::fromNodeElement($node_element, $this->getPageFactory()); } elseif ( $tag_name == 'textarea' ) { - return TextInput::fromNodeElement($node_element); + return TextInput::fromNodeElement($node_element, $this->getPageFactory()); } + $web_element = WebElement::fromNodeElement($node_element, $this->getPageFactory()); throw new FormException( - 'Unable create typified element for ' . (string)WebElement::fromNodeElement($node_element), + 'Unable create typified element for ' . (string)$web_element, FormException::TYPE_UNKNOWN_FIELD ); } diff --git a/library/QATools/QATools/HtmlElements/Element/Select.php b/library/QATools/QATools/HtmlElements/Element/Select.php index f599c4da..e22937c4 100644 --- a/library/QATools/QATools/HtmlElements/Element/Select.php +++ b/library/QATools/QATools/HtmlElements/Element/Select.php @@ -294,7 +294,7 @@ protected function wrapOptions(array $nodes) foreach ( $nodes as $node_element ) { /** @var SelectOption $option */ - $option = SelectOption::fromNodeElement($node_element); + $option = SelectOption::fromNodeElement($node_element, $this->getPageFactory()); $ret[] = $option->setSelect($this); } diff --git a/library/QATools/QATools/PageObject/Element/AbstractElementCollection.php b/library/QATools/QATools/PageObject/Element/AbstractElementCollection.php index 018655b9..086bc4e0 100644 --- a/library/QATools/QATools/PageObject/Element/AbstractElementCollection.php +++ b/library/QATools/QATools/PageObject/Element/AbstractElementCollection.php @@ -99,7 +99,7 @@ public function offsetSet($index, $newval) public static function fromNodeElements( array $node_elements, $element_class = null, - IPageFactory $page_factory = null + IPageFactory $page_factory ) { $collection = new static(); diff --git a/library/QATools/QATools/PageObject/Element/AbstractElementContainer.php b/library/QATools/QATools/PageObject/Element/AbstractElementContainer.php index 7c849f61..51882748 100644 --- a/library/QATools/QATools/PageObject/Element/AbstractElementContainer.php +++ b/library/QATools/QATools/PageObject/Element/AbstractElementContainer.php @@ -24,13 +24,6 @@ abstract class AbstractElementContainer extends WebElement implements IElementContainer { - /** - * Stores instance of used page factory. - * - * @var IPageFactory - */ - private $_pageFactory; - /** * Initializes html element. * @@ -39,11 +32,10 @@ abstract class AbstractElementContainer extends WebElement implements IElementCo */ public function __construct(NodeElement $wrapped_element, IPageFactory $page_factory) { - parent::__construct($wrapped_element); + parent::__construct($wrapped_element, $page_factory); - $this->_pageFactory = $page_factory; - $this->_pageFactory->initElementContainer($this); - $this->_pageFactory->initElements($this, $this->_pageFactory->createDecorator($this)); + $page_factory->initElementContainer($this); + $page_factory->initElements($this, $page_factory->createDecorator($this)); } /** @@ -55,26 +47,9 @@ public function __construct(NodeElement $wrapped_element, IPageFactory $page_fac * @return static * @throws ElementException When page factory is missing. */ - public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory = null) + public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory) { - if ( !isset($page_factory) ) { - throw new ElementException( - 'Page factory is required to create this element', - ElementException::TYPE_PAGE_FACTORY_REQUIRED - ); - } - return new static($node_element, $page_factory); } - /** - * Returns page factory, used during object creation. - * - * @return IPageFactory - */ - protected function getPageFactory() - { - return $this->_pageFactory; - } - } diff --git a/library/QATools/QATools/PageObject/Element/INodeElementAware.php b/library/QATools/QATools/PageObject/Element/INodeElementAware.php index 782df125..2501cb3f 100644 --- a/library/QATools/QATools/PageObject/Element/INodeElementAware.php +++ b/library/QATools/QATools/PageObject/Element/INodeElementAware.php @@ -28,6 +28,6 @@ interface INodeElementAware * * @return static */ - public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory = null); + public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory); } diff --git a/library/QATools/QATools/PageObject/Element/WebElement.php b/library/QATools/QATools/PageObject/Element/WebElement.php index 70fcdbc2..517c2c4d 100644 --- a/library/QATools/QATools/PageObject/Element/WebElement.php +++ b/library/QATools/QATools/PageObject/Element/WebElement.php @@ -87,6 +87,13 @@ class WebElement implements IWebElement, INodeElementAware */ private $_wrappedElement; + /** + * Stores instance of used page factory. + * + * @var IPageFactory + */ + private $_pageFactory; + /** * The XPath escaper. * @@ -97,11 +104,13 @@ class WebElement implements IWebElement, INodeElementAware /** * Initializes web element. * - * @param NodeElement $wrapped_element Wrapped element. + * @param NodeElement $wrapped_element Wrapped element. + * @param IPageFactory $page_factory Page factory. */ - public function __construct(NodeElement $wrapped_element) + public function __construct(NodeElement $wrapped_element, IPageFactory $page_factory) { $this->_wrappedElement = $wrapped_element; + $this->_pageFactory = $page_factory; $this->_xpathEscaper = new Escaper(); } @@ -113,9 +122,9 @@ public function __construct(NodeElement $wrapped_element) * * @return static */ - public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory = null) + public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory) { - return new static($node_element); + return new static($node_element, $page_factory); } /** @@ -188,6 +197,16 @@ public function getXpathEscaper() return $this->_xpathEscaper; } + /** + * Returns page factory, used during object creation. + * + * @return IPageFactory + */ + protected function getPageFactory() + { + return $this->_pageFactory; + } + /** * Returns string representation of element. * diff --git a/library/QATools/QATools/PageObject/Exception/ElementException.php b/library/QATools/QATools/PageObject/Exception/ElementException.php index 18c74deb..796ef2d7 100644 --- a/library/QATools/QATools/PageObject/Exception/ElementException.php +++ b/library/QATools/QATools/PageObject/Exception/ElementException.php @@ -24,8 +24,6 @@ class ElementException extends PageFactoryException const TYPE_NOT_FOUND = 104; - const TYPE_PAGE_FACTORY_REQUIRED = 105; - const TYPE_MULTIPLE_ELEMENTS_FOUND = 106; const TYPE_UNKNOWN_PROPERTY = 107; diff --git a/library/QATools/QATools/PageObject/Page.php b/library/QATools/QATools/PageObject/Page.php index f7c33930..d6450b3e 100644 --- a/library/QATools/QATools/PageObject/Page.php +++ b/library/QATools/QATools/PageObject/Page.php @@ -87,7 +87,7 @@ public function open(array $params = array()) throw new PageException('Page url not specified', PageException::TYPE_EMPTY_URL); } - $this->getSession()->visit($url); + $this->pageFactory->getSession()->visit($url); return $this; } diff --git a/tests/QATools/QATools/HtmlElements/Element/AbstractElementContainerTest.php b/tests/QATools/QATools/HtmlElements/Element/AbstractElementContainerTest.php index 8e6fea77..1574bf00 100644 --- a/tests/QATools/QATools/HtmlElements/Element/AbstractElementContainerTest.php +++ b/tests/QATools/QATools/HtmlElements/Element/AbstractElementContainerTest.php @@ -67,18 +67,6 @@ public function testGetPageFactory() $this->assertSame($this->pageFactory, $method->invoke($element)); } - /** - * @expectedException \QATools\QATools\HtmlElements\Exception\TypifiedElementException - * @expectedExceptionCode \QATools\QATools\HtmlElements\Exception\TypifiedElementException::TYPE_PAGE_FACTORY_REQUIRED - * @expectedExceptionMessage Page factory is required to create this element - */ - public function testFromNodeElementWithoutPageFactory() - { - /* @var $element_class ElementContainerChild */ - $element_class = $this->elementClass; - $element_class::fromNodeElement($this->createNodeElement()); - } - /** * @medium */ diff --git a/tests/QATools/QATools/HtmlElements/Element/AbstractTypifiedElementTest.php b/tests/QATools/QATools/HtmlElements/Element/AbstractTypifiedElementTest.php index c8c1b482..68bbdace 100644 --- a/tests/QATools/QATools/HtmlElements/Element/AbstractTypifiedElementTest.php +++ b/tests/QATools/QATools/HtmlElements/Element/AbstractTypifiedElementTest.php @@ -190,7 +190,7 @@ public function testToString() */ protected function createElement() { - return new $this->elementClass($this->webElement); + return new $this->elementClass($this->webElement, $this->pageFactory); } /** diff --git a/tests/QATools/QATools/HtmlElements/Element/SelectOptionTest.php b/tests/QATools/QATools/HtmlElements/Element/SelectOptionTest.php index 9f2734ea..d38bd63f 100644 --- a/tests/QATools/QATools/HtmlElements/Element/SelectOptionTest.php +++ b/tests/QATools/QATools/HtmlElements/Element/SelectOptionTest.php @@ -67,14 +67,14 @@ public function testSelectException() $this->webElement->shouldReceive('isSelected')->once()->andReturn(false); /** @var SelectOption $option */ - $option = new $this->elementClass($this->webElement); + $option = new $this->elementClass($this->webElement, $this->pageFactory); $option->select(); } public function testSetSelect() { /** @var SelectOption $option */ - $option = new $this->elementClass($this->webElement); + $option = new $this->elementClass($this->webElement, $this->pageFactory); $this->assertSame($option, $option->setSelect($this->select)); } @@ -194,7 +194,7 @@ protected function getElement() protected function createElement() { /** @var SelectOption $option */ - $option = new $this->elementClass($this->webElement); + $option = new $this->elementClass($this->webElement, $this->pageFactory); $option->setSelect($this->select); return $option; @@ -211,7 +211,7 @@ protected function mockElement(array $methods = array()) { $method_string = $methods ? '[' . implode(',', $methods) . ']' : ''; - return m::mock($this->elementClass . $method_string, array($this->webElement, $this->select)); + return m::mock($this->elementClass . $method_string, array($this->webElement, $this->pageFactory)); } } diff --git a/tests/QATools/QATools/Live/HtmlElements/Element/TypifiedElementTestCase.php b/tests/QATools/QATools/Live/HtmlElements/Element/TypifiedElementTestCase.php index 85c7aab6..50b4834f 100644 --- a/tests/QATools/QATools/Live/HtmlElements/Element/TypifiedElementTestCase.php +++ b/tests/QATools/QATools/Live/HtmlElements/Element/TypifiedElementTestCase.php @@ -62,7 +62,7 @@ protected function createElement(array $selector) $selenium_selector = new SeleniumSelector(); $xpath = $selenium_selector->translateToXPath($selector); - $web_element = new WebElement(new NodeElement($xpath, $this->session)); + $web_element = new WebElement(new NodeElement($xpath, $this->session), $this->pageFactory); return new $this->elementClass($web_element, $this->pageFactory); } diff --git a/tests/QATools/QATools/PageObject/Element/AbstractElementContainerTest.php b/tests/QATools/QATools/PageObject/Element/AbstractElementContainerTest.php index 37f0ee06..b8a5c6f7 100644 --- a/tests/QATools/QATools/PageObject/Element/AbstractElementContainerTest.php +++ b/tests/QATools/QATools/PageObject/Element/AbstractElementContainerTest.php @@ -41,18 +41,6 @@ public function testGetPageFactory() $this->assertSame($this->pageFactory, $method->invoke($element)); } - /** - * @expectedException \QATools\QATools\PageObject\Exception\ElementException - * @expectedExceptionCode \QATools\QATools\PageObject\Exception\ElementException::TYPE_PAGE_FACTORY_REQUIRED - * @expectedExceptionMessage Page factory is required to create this element - */ - public function testFromNodeElementWithoutPageFactory() - { - /* @var $element_class ElementContainerChild */ - $element_class = $this->elementClass; - $element_class::fromNodeElement($this->createNodeElement()); - } - /** * Create element. * diff --git a/tests/QATools/QATools/PageObject/Element/ElementCollectionTest.php b/tests/QATools/QATools/PageObject/Element/ElementCollectionTest.php index 75044fe6..c6a47203 100644 --- a/tests/QATools/QATools/PageObject/Element/ElementCollectionTest.php +++ b/tests/QATools/QATools/PageObject/Element/ElementCollectionTest.php @@ -112,7 +112,7 @@ public function testCreatingCollectionWithNoMatchByElementInterface($from_constr public function testFromNodeElementsDefaultsToCollectionClass() { $element = $this->createNodeElement(); - $collection = NodeElementAwareCollection::fromNodeElements(array($element)); + $collection = NodeElementAwareCollection::fromNodeElements(array($element), null, $this->pageFactory); $this->assertCount(1, $collection); } @@ -124,7 +124,7 @@ public function testFromNodeElementsDefaultsToCollectionClass() public function testFromNodeElementElementCantWorkWithInterfaces() { $element = $this->createNodeElement(); - CollectionWithExistingElementInterface::fromNodeElements(array($element)); + CollectionWithExistingElementInterface::fromNodeElements(array($element), null, $this->pageFactory); } /** diff --git a/tests/QATools/QATools/PageObject/Element/WebElementTest.php b/tests/QATools/QATools/PageObject/Element/WebElementTest.php index 1c7f569a..aa497aac 100644 --- a/tests/QATools/QATools/PageObject/Element/WebElementTest.php +++ b/tests/QATools/QATools/PageObject/Element/WebElementTest.php @@ -85,7 +85,7 @@ public function testGetXpathEscaper() */ protected function createElement() { - return new $this->elementClass(new NodeElement('XPATH', $this->session)); + return new $this->elementClass(new NodeElement('XPATH', $this->session), $this->pageFactory); } } From 5fc3193413bb4d5b3e67517ba3c610e9bf26b849 Mon Sep 17 00:00:00 2001 From: Alexander Obuhovich Date: Fri, 24 Jun 2016 15:21:06 +0300 Subject: [PATCH 3/4] Use session from PageFactory and not Element and mark element "getSession" method as deprecated --- CHANGELOG.md | 1 + composer.json | 3 +- composer.lock | 59 ++++++++++++++++++- .../Element/AbstractTypifiedElement.php | 12 +++- .../QATools/PageObject/Element/WebElement.php | 17 +++++- .../Element/AbstractTypifiedElementTest.php | 3 + .../PageObject/Element/WebElementTest.php | 9 ++- tests/QATools/QATools/PageObject/PageTest.php | 5 -- 8 files changed, 96 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd57aa23..a8858898 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Changed - When "TypifiedElementProxy" class was used manually (not through annotations) the element class was "TextBlock" instead of "AbstractTypifiedElement". - When "TypifiedElementCollectionProxy" class was used manually (not through annotations) the element class was "TextBlock" instead of "AbstractTypifiedElementCollection". +- All elements created manually (not through annotations) now require `IPageFactory` instance as 2nd argument (before only container type elements were needing this). ### Fixed - When "WebElementCollectionProxy" class was used manually (not through annotations) the element class was "WebElement" instead of "WebElementCollection". diff --git a/composer.json b/composer.json index 780d403b..6579d9b4 100755 --- a/composer.json +++ b/composer.json @@ -28,7 +28,8 @@ "require-dev": { "aik099/coding-standard": "dev-master", "behat/mink-selenium2-driver": "~1.2", - "mockery/mockery": "~0.9" + "mockery/mockery": "~0.9", + "symfony/phpunit-bridge": "^3.1" }, "suggest": { diff --git a/composer.lock b/composer.lock index 916d71d5..362be12e 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "e72960a1600e74bb723dda3988444df7", - "content-hash": "2eab9d98f4e335b734b5f7cfcc1e08a6", + "hash": "830c2f41d39d617d5b4acc245a1697be", + "content-hash": "0148b32661f2c42ef82164251fe0fff8", "packages": [ { "name": "behat/mink", @@ -438,6 +438,61 @@ "testing" ], "time": "2016-05-22 21:52:33" + }, + { + "name": "symfony/phpunit-bridge", + "version": "v3.1.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/phpunit-bridge.git", + "reference": "f5e7cdc9b5cd3f7d9b0228692cd6ec5736c3b074" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/phpunit-bridge/zipball/f5e7cdc9b5cd3f7d9b0228692cd6ec5736c3b074", + "reference": "f5e7cdc9b5cd3f7d9b0228692cd6ec5736c3b074", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "symfony/debug": "For tracking deprecated interfaces usages at runtime with DebugClassLoader" + }, + "type": "symfony-bridge", + "extra": { + "branch-alias": { + "dev-master": "3.1-dev" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Bridge\\PhpUnit\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony PHPUnit Bridge", + "homepage": "https://symfony.com", + "time": "2016-04-12 18:27:47" } ], "aliases": [], diff --git a/library/QATools/QATools/HtmlElements/Element/AbstractTypifiedElement.php b/library/QATools/QATools/HtmlElements/Element/AbstractTypifiedElement.php index 3a2ab177..767ceb54 100644 --- a/library/QATools/QATools/HtmlElements/Element/AbstractTypifiedElement.php +++ b/library/QATools/QATools/HtmlElements/Element/AbstractTypifiedElement.php @@ -206,13 +206,19 @@ public function getName() } /** - * Returns wrapped element session. + * Returns element session. * - * @return Session + * @return Session + * @deprecated Accessing the session from the element is deprecated as of 1.2 and will be impossible in 2.0. */ public function getSession() { - return $this->getWrappedElement()->getSession(); + @trigger_error( + sprintf('The method %s is deprecated as of 1.2 and will be removed in 2.0', __METHOD__), + E_USER_DEPRECATED + ); + + return $this->_pageFactory->getSession(); } /** diff --git a/library/QATools/QATools/PageObject/Element/WebElement.php b/library/QATools/QATools/PageObject/Element/WebElement.php index 517c2c4d..c7a61857 100644 --- a/library/QATools/QATools/PageObject/Element/WebElement.php +++ b/library/QATools/QATools/PageObject/Element/WebElement.php @@ -70,7 +70,6 @@ * @method boolean hasTable($locator) Checks whether element has a table with specified locator. * @method void attachFileToField($locator, $path) Attach file to file field with specified locator. * - * @method Session getSession() Returns element session. * @method boolean has($selector, $locator) Checks whether element with specified selector exists inside the current element. * @method boolean isValid() Checks if an element still exists in the DOM. * @method string getText() Returns element text (inside tag). @@ -197,6 +196,22 @@ public function getXpathEscaper() return $this->_xpathEscaper; } + /** + * Returns element session. + * + * @return Session + * @deprecated Accessing the session from the element is deprecated as of 1.2 and will be impossible in 2.0. + */ + public function getSession() + { + @trigger_error( + sprintf('The method %s is deprecated as of 1.2 and will be removed in 2.0', __METHOD__), + E_USER_DEPRECATED + ); + + return $this->_pageFactory->getSession(); + } + /** * Returns page factory, used during object creation. * diff --git a/tests/QATools/QATools/HtmlElements/Element/AbstractTypifiedElementTest.php b/tests/QATools/QATools/HtmlElements/Element/AbstractTypifiedElementTest.php index 68bbdace..218cd527 100644 --- a/tests/QATools/QATools/HtmlElements/Element/AbstractTypifiedElementTest.php +++ b/tests/QATools/QATools/HtmlElements/Element/AbstractTypifiedElementTest.php @@ -135,6 +135,9 @@ public function testSetName() $this->assertEquals($expected, $this->typifiedElement->getName()); } + /** + * @group legacy + */ public function testGetSession() { $this->assertSame($this->session, $this->typifiedElement->getSession()); diff --git a/tests/QATools/QATools/PageObject/Element/WebElementTest.php b/tests/QATools/QATools/PageObject/Element/WebElementTest.php index aa497aac..753f26a4 100644 --- a/tests/QATools/QATools/PageObject/Element/WebElementTest.php +++ b/tests/QATools/QATools/PageObject/Element/WebElementTest.php @@ -31,7 +31,6 @@ public function testConstructor() $element = $this->createElement(); $this->assertEquals('XPATH', $element->getXpath()); - $this->assertSame($this->session, $element->getSession()); } public function testFromNodeElement() @@ -78,6 +77,14 @@ public function testGetXpathEscaper() $this->assertInstanceOf('\\Behat\\Mink\\Selector\\Xpath\\Escaper', $element->getXpathEscaper()); } + /** + * @group legacy + */ + public function testGetSession() + { + $this->assertSame($this->session, $this->createElement()->getSession()); + } + /** * Create element. * diff --git a/tests/QATools/QATools/PageObject/PageTest.php b/tests/QATools/QATools/PageObject/PageTest.php index b77457e6..cc3d8536 100644 --- a/tests/QATools/QATools/PageObject/PageTest.php +++ b/tests/QATools/QATools/PageObject/PageTest.php @@ -65,11 +65,6 @@ protected function setUp() $this->page = new $this->pageClass($this->pageFactory); } - public function testConstructor() - { - $this->assertSame($this->session, $this->page->getSession()); - } - public function testGetAbsoluteUrl() { $expected = 'RL'; From 2a567b35a70517519daf9048e191287d34c65a15 Mon Sep 17 00:00:00 2001 From: Alexander Obuhovich Date: Fri, 24 Jun 2016 15:30:43 +0300 Subject: [PATCH 4/4] CS fix --- .../QATools/PageObject/Element/AbstractElementContainer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/library/QATools/QATools/PageObject/Element/AbstractElementContainer.php b/library/QATools/QATools/PageObject/Element/AbstractElementContainer.php index 51882748..2b86d988 100644 --- a/library/QATools/QATools/PageObject/Element/AbstractElementContainer.php +++ b/library/QATools/QATools/PageObject/Element/AbstractElementContainer.php @@ -45,7 +45,6 @@ public function __construct(NodeElement $wrapped_element, IPageFactory $page_fac * @param IPageFactory $page_factory Page factory. * * @return static - * @throws ElementException When page factory is missing. */ public static function fromNodeElement(NodeElement $node_element, IPageFactory $page_factory) {