Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Adds "Element::isValid" method for checking element presence in DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Obuhovich committed Feb 19, 2014
1 parent c1dd197 commit 1568f84
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Behat/Mink/Element/Element.php
Expand Up @@ -54,6 +54,16 @@ public function has($selector, $locator)
return null !== $this->find($selector, $locator);
}

/**
* Checks if an element is still valid.
*
* @return boolean
*/
public function isValid()
{
return 1 === count($this->getSession()->getDriver()->find($this->getXpath()));
}

/**
* Finds first element with specified selector.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Behat/Mink/Element/ElementInterface.php
Expand Up @@ -43,6 +43,13 @@ public function getSession();
*/
public function has($selector, $locator);

/**
* Checks if an element is still valid.
*
* @return boolean
*/
public function isValid();

/**
* Finds first element with specified selector.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/Behat/Mink/Element/NodeElementTest.php
Expand Up @@ -34,6 +34,34 @@ public function testGetText()
$this->assertEquals($expected, $node->getText());
}

public function testElementIsValid()
{
$elementXpath = 'some xpath';
$node = new NodeElement($elementXpath, $this->session);

$this->session->getDriver()
->expects($this->once())
->method('find')
->with($elementXpath)
->will($this->returnValue(array($elementXpath)));

$this->assertTrue($node->isValid());
}

public function testElementIsNotValid()
{
$node = new NodeElement('some xpath', $this->session);

$this->session->getDriver()
->expects($this->exactly(2))
->method('find')
->with('some xpath')
->will($this->onConsecutiveCalls(array(), array('xpath1', 'xpath2')));

$this->assertFalse($node->isValid(), 'no elements found is invalid element');
$this->assertFalse($node->isValid(), 'more then 1 element found is invalid element');
}

public function testHasAttribute()
{
$node = new NodeElement('input_tag', $this->session);
Expand Down

0 comments on commit 1568f84

Please sign in to comment.