Skip to content

Commit

Permalink
Merge pull request #185 from aik099/184-take-session-from-page-factor…
Browse files Browse the repository at this point in the history
…y-not-element

Use session from factory, not the element
  • Loading branch information
Alexander Obuhovich committed Jun 24, 2016
2 parents a65fbbf + 2a567b3 commit 8fdb9b0
Show file tree
Hide file tree
Showing 26 changed files with 180 additions and 136 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
59 changes: 57 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion library/QATools/QATools/BEM/Proxy/AbstractPartProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions library/QATools/QATools/BEM/Proxy/ElementProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
abstract class AbstractElementContainer extends AbstractTypifiedElement implements IElementContainer
{

/**
* Stores instance of used page factory.
*
* @var IPageFactory
*/
private $_pageFactory;

/**
* Specifies wrapped WebElement.
*
Expand All @@ -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));
}

/**
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
}
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -197,13 +206,29 @@ 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();
}

/**
* Returns page factory, used during object creation.
*
* @return IPageFactory
*/
protected function getPageFactory()
{
return $this->_pageFactory;
}

/**
Expand Down Expand Up @@ -287,7 +312,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');
}

/**
Expand Down
15 changes: 8 additions & 7 deletions library/QATools/QATools/HtmlElements/Element/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
2 changes: 1 addition & 1 deletion library/QATools/QATools/HtmlElements/Element/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
abstract class AbstractElementContainer extends WebElement implements IElementContainer
{

/**
* Stores instance of used page factory.
*
* @var IPageFactory
*/
private $_pageFactory;

/**
* Initializes html element.
*
Expand All @@ -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));
}

/**
Expand All @@ -53,28 +45,10 @@ 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 = 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Loading

0 comments on commit 8fdb9b0

Please sign in to comment.