Skip to content

Commit

Permalink
Merge pull request #7 from orangedigital/issue6
Browse files Browse the repository at this point in the history
Rewrote the 'waitForComponentShould' step in reponse to #6
  • Loading branch information
philliphicks committed Oct 11, 2012
2 parents f46380c + 33a8b7c commit 6b61009
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
example/behat.me.yml
.DS_Store
*.swp
nbproject/
Expand Down
13 changes: 11 additions & 2 deletions example/features/example.feature
Expand Up @@ -17,13 +17,22 @@ And I check the "terms" checkbox
And I uncheck the "adverts" checkbox
And I attach "cat.jpeg" to "file upload input"
And I should not see "Wait Test" component

When I press the "Add" button
And I wait for the "Wait Test" component to appear
Then I should see "Wait Test" component
And I press the "Remove" button

When I press the "Hide" button
And I wait for the "Wait Test" component to disappear
Then I should not see "Wait Test" component

When I press the "Unhide" button
And I wait for the "Wait Test" component to appear
Then I should see "Wait Test" component

When I press the "Remove" button
And I wait for the "Wait Test" component to disappear
Then I should not see "Wait Test" component
And I click the "Add"

@javascript
Scenario: As a tester I want to assert the presence of elements on the page using business terms
Expand Down
4 changes: 3 additions & 1 deletion example/selectors.yml
Expand Up @@ -19,4 +19,6 @@ IDIndex1: 1
Hidden Div: "#secret-container"
Wait Test: "#wait_test"
Add: "#add"
Remove: "#remove"
Remove: "#remove"
Hide: "#hide"
Unhide: "#unhide"
26 changes: 22 additions & 4 deletions example/www/index.html
Expand Up @@ -4,10 +4,26 @@
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function addElement() {
$('#container').append('<p id="wait_test">why hello there, here I am!</p>');
window.setTimeout(function(){
$('#container').append('<p id="wait_test">why hello there, here I am!</p>');
}, 6000);

}
function removeElement(id) {
$("#" + id).hide();
window.setTimeout(function(id){
$("#" + id).remove();
}, 6000, id);
}
function hideElement(id) {
window.setTimeout(function(id){
$("#" + id).hide();
}, 6000, id);
}
function unHideElement(id) {
window.setTimeout(function(id){
$("#" + id).show();
}, 6000, id)

}
</script>
</head>
Expand Down Expand Up @@ -46,7 +62,9 @@ <h1>Test</h1>
</form>
<div id="container">
</div>
<input class="Add" type="submit" id="add" value="Add" onClick="addElement();">
<input class="Remove" type="submit" id="remove" value="Remove" onClick="removeElement('wait_test');">
<input class="Add" type="button" id="add" value="Add" onClick="addElement();">
<input class="Hide" type="button" id="hide" value="Hide" onClick="hideElement('wait_test');">
<input class="Unhide" type="button" id="unhide" value="Unhide" onClick="unHideElement('wait_test');">
<input class="Remove" type="button" id="remove" value="Remove" onClick="removeElement('wait_test');">
</body>
</html>
Expand Up @@ -252,27 +252,48 @@ public function iShouldNotSeeComponent($elementName) {
/**
* @When /^I wait for the "([^"]*)" component to (dis|)appear$/
*/
public function waitForComponent($elementName, $visibility = null)
public function waitForComponent($elementName, $expVisibility = null)
{
// Visibility is null when the 'dis' section of the string is not present
$expVisibility = (empty($expVisibility)) ? 'visible' : 'hidden';

$selector = $this->getSelectorFromString($elementName);

$session = $this->getSession();
$timeout = $this->getTimeout();

$condition = "window && window.jQuery && jQuery('$selector').is(':" .(
$visibility
? 'hidden'
: 'visible'
). "');";

$start = 1000 * microtime(true);
$end = $start + ((int)$timeout);

if($expVisibility == 'hidden') {
// If we are expecting the element to disappear it could either have its visibility changed or removed from the DOM
$condition = "window && window.jQuery && (jQuery('$selector').is(':" . $expVisibility . "') || jQuery.find('$selector').length == 0);";
} else {
$condition = "window && window.jQuery && jQuery('$selector').is(':" . $expVisibility . "');";
}

// Will block for $timeout or until the the condition return true
// Always returns true never throws an exception.
$session->wait($timeout, $condition);

if ((1000 * microtime(true)) >= $end) {
throw new \RuntimeException("Component ".$elementName." did not ".$visibility."appear on the page");

// Search for element. Element if found, null if element not found.
$element = $session->getPage()->find('css', $selector);

if (!is_null($element)) {

// Element can be on the page but may not be visible
$visibility = $element->isVisible();

if($expVisibility == 'hidden' && $visibility) {
throw new \RuntimeException("Component " . $elementName . " is visible");
} elseif ($expVisibility == 'visible' && !$visibility) {
throw new \RuntimeException("Component " . $elementName . " is on page but not visible");
}

} else {
// No Element on the page
if($expVisibility == 'visible') {
throw new \RuntimeException("Component " . $elementName . " does not appear on the page");
}
}

}

/**
Expand Down
96 changes: 89 additions & 7 deletions tests/Context/UIBusinessSelectorContextTest.php
Expand Up @@ -69,6 +69,21 @@ protected function setFindExpectationWithReturnElement($selector, $element) {
->method("getPage")
->will($this->returnValue($page));
}

protected function setFindExpectationWithNoReturnElement($selector) {
$page = $this->getMock('Behat\Mink\Element\Element', array(), array(), '', false, false);

$page
->expects($this->once())
->method('find')
->with('css', $selector)
->will($this->returnValue(null));

$this->session
->expects($this->once())
->method("getPage")
->will($this->returnValue($page));
}

protected function setFindExpectationWithNoElementFoundException($selector) {
$page = $this->getMock('Behat\Mink\Element\Element', array(), array(), '', false, false);
Expand Down Expand Up @@ -1014,25 +1029,46 @@ public function testIRefocusOnThePrimaryPage() {

public function testWaitForComponentShouldCorrectlySubstituteSelector() {

// Expected to appear
// Does appear && is visible

$this->setSessionExpectation(true);

$input = $this->getMock('Behat\Mink\Element\NodeElement', array(), array(), '', false, false);

$input->expects($this->once())
->method('isVisible')
->will($this->returnValue(true));

$this->setFindExpectationWithReturnElement('input[name=picture]', $input);

$this->context->waitForComponent('User Picture');
}

public function testWaitForComponentShouldThrowExceptionOnNonExistentSelector() {
public function testWaitForComponentShouldThrowExceptionIfElementAppearsWhenExpectedButIsNotVisible() {

$this->setSessionExpectation(false);
// Expected to appear
// Present on page but not visible

$this->setSessionExpectation(true);

$this->setExpectedException('\RuntimeException');
$input = $this->getMock('Behat\Mink\Element\NodeElement', array(), array(), '', false, false);

$this->context->waitForComponent('Stuffed Dog');
$input->expects($this->once())
->method('isVisible')
->will($this->returnValue(false));

$this->setFindExpectationWithReturnElement('input[name=picture]', $input);

$this->setExpectedException('\RuntimeException');

$this->context->waitForComponent('User Picture');
}

public function testWaitForComponentShouldThrowExceptionIfElementDoesNotAppearWhenExpected() {
public function testWaitForComponentShouldThrowExceptionIfElementDoesNotAppearOnPageWhenExpected() {

// Expected to appeaer
// is not on page

$this->setSessionExpectation(true);

Expand All @@ -1042,21 +1078,67 @@ public function testWaitForComponentShouldThrowExceptionIfElementDoesNotAppearWh

$this->context->waitForComponent('User Picture');
}


public function testWaitForComponentShouldThrowExceptionIfElementDoesNotDisappearWhenExpected()
{
// Expected to disappear
// Still present and visible

$this->setSessionExpectation(true);

$input = $this->getMock('Behat\Mink\Element\NodeElement', array(), array(), '', false, false);


$input->expects($this->once())
->method('isVisible')
->will($this->returnValue(true));

$this->setFindExpectationWithReturnElement('input[name=picture]', $input);

$this->setExpectedException('\RuntimeException');

$this->context->waitForComponent('User Picture', 'dis');
}

public function testWaitForComponentShouldNotThrowExceptionIfElementDisappearsWhenExpectedOffPage()
{
// Expected to disappear

$this->setSessionExpectation(true);

$input = $this->getMock('Behat\Mink\Element\NodeElement', array(), array(), '', false, false);

$this->setFindExpectationWithNoReturnElement('input[name=picture]');

$this->context->waitForComponent('User Picture', 'dis');
}

public function testWaitForComponentShouldNotThrowExceptionIfElementDisappearsWhenExpectedCecomesInvisible()
{
// Expected to disappear
// present on page but invisible

$this->setSessionExpectation(true);

$input = $this->getMock('Behat\Mink\Element\NodeElement', array(), array(), '', false, false);

$input->expects($this->once())
->method('isVisible')
->will($this->returnValue(false));

$this->setFindExpectationWithReturnElement('input[name=picture]', $input);

$this->context->waitForComponent('User Picture', 'dis');
}

public function testWaitForComponentShouldThrowExceptionOnNonExistentSelector() {

$this->setSessionExpectation(false);

$this->setExpectedException('\RuntimeException');

$this->context->waitForComponent('Stuffed Dog');
}




Expand Down

0 comments on commit 6b61009

Please sign in to comment.