Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More methods for NodeElement children manipulations #586

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/Behat/Mink/Element/NodeElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,86 @@ public function getParent()
return $this->find('xpath', '..');
}

/**
* Returns All child elements (including descendant) to the current one.
*
* @return static[]|null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findAll can never return null.

and it should be NodeElement[] or self[], not static[]. If you extend the NodeElement class, the elements returned here will not use the child class (findAll() always returns a list of NodeElement instances)

*/
public function getAllChildren()
{
return $this->findAll('xpath', 'descendant::*');
}

/**
* Returns All direct child elements (NOT including deeper descendant) to the current one.
*
* @return static[]|null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

*/
public function getDirectChildren()
{
return $this->findAll('xpath', 'child::*');
}

/**
* Returns the first child element to the current one.
*
* @return static|null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be NodeElement|null or self|null here as well

*/
public function getFirstChild()
{
return $this->find('xpath', 'child::*[1]');
}

/**
* Returns the last direct child element to the current one.
*
* @return static|null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

*/
public function getLastChild()
{
return $this->find('xpath', 'child::*[last()]');
}

/**
* Returns all preceding sibling elements to the current one.
*
* @return static[]|null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

*/
public function getPreviousSiblings()
{
return $this->findAll('xpath', 'preceding-sibling::*');
}

/**
* Returns the preceding sibling element close to the current one.
*
* @return static|null
*/
public function getPreviousSibling()
{
return $this->find('xpath', 'preceding-sibling::*[1]');
}

/**
* Returns all following sibling elements to the current one.
*
* @return static[]|null
*/
public function getNextSiblings()
{
return $this->findAll('xpath', 'following-sibling::*');
}

/**
* Returns the following sibling element close to the current one.
*
* @return static|null
*/
public function getNextSibling()
{
return $this->find('xpath', 'following-sibling::*[1]');
}

/**
* Returns current node tag name.
*
Expand Down
23 changes: 23 additions & 0 deletions src/Behat/Mink/Element/TraversableElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ public function findById($id)
return $this->find('named', array('id', $id));
}

/**
* Finds element by its name.
*
* @param string name element name
* @return static|null
*/
public function findByName($name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this method much. The name is not a unique identifier at all, so I'm not sure returning a single element makes sense

{
$name = $this->getSelectorsHandler()->xpathLiteral($name);
return $this->find('named', array('name', $name));
}

/**
* Finds elements by its tag.
*
* @param string tag element tag
* @return static[]|null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong. It always returns a list of NodeElement, even when being called on a DocumentElement instance. It should be NodeElement[] (it cannot be null either)

*/
public function findByTag($tag)
{
return $this->findAll('css', $tag);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation is wrong. This is not finding by tag only

}

/**
* Checks whether element has a link with specified locator.
*
Expand Down
1 change: 1 addition & 0 deletions src/Behat/Mink/Selector/ExactNamedSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct()
$this->registerReplacement('%altMatch%', './@alt = %locator%');
$this->registerReplacement('%relMatch%', './@rel = %locator%');
$this->registerReplacement('%labelAttributeMatch%', './@label = %locator%');
$this->registerReplacement('%nameMatch%', './@name = %locator%');

parent::__construct();
}
Expand Down
5 changes: 5 additions & 0 deletions src/Behat/Mink/Selector/NamedSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class NamedSelector implements SelectorInterface
'%altMatch%' => 'contains(./@alt, %locator%)',
'%relMatch%' => 'contains(./@rel, %locator%)',
'%labelAttributeMatch%' => 'contains(./@label, %locator%)',
'%nameMatch%' => 'contains(./@name, %locator%)',

// complex replacements
'%inputTypeWithoutPlaceholderFilter%' => "%lowercaseType% = 'radio' or %lowercaseType% = 'checkbox' or %lowercaseType% = 'file'",
Expand Down Expand Up @@ -152,6 +153,10 @@ class NamedSelector implements SelectorInterface
XPATH
,'id' => <<<XPATH
.//*[%idMatch%]
XPATH

,'name' => <<<XPATH
.//*[%nameMatch%]
XPATH
);

Expand Down
1 change: 1 addition & 0 deletions src/Behat/Mink/Selector/PartialNamedSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct()
$this->registerReplacement('%altMatch%', 'contains(./@alt, %locator%)');
$this->registerReplacement('%relMatch%', 'contains(./@rel, %locator%)');
$this->registerReplacement('%labelAttributeMatch%', 'contains(./@label, %locator%)');
$this->registerReplacement('%nameMatch%', 'contains(./@name, %locator%)');

parent::__construct();
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Selector/NamedSelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public function getSelectorTests()
'table' => array('test.html', 'table', 'the-table', 2, 3),

'id' => array('test.html', 'id', 'bad-link-text', 1),

'name' => array('test.html', 'name', 'the-field', 11),
);
}

Expand Down