Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rewrote the 'waitForComponentShould' step in reponse to #6

The component now takes a number of additional edge cases into
account.

disappear - element off the page / element hidden
appear - element on the page and visible

Added a range of additional unit and integration tests.

Ammended the examples to include a pause before manipulating
the dom. The examples now reflect the functionality.
  • Loading branch information...
commit 33a8b7cdcd7d7583a843478edf02133d19faf68b 1 parent f46380c
Ben Waine authored
View
1  .gitignore
@@ -1,3 +1,4 @@
+example/behat.me.yml
.DS_Store
*.swp
nbproject/
View
13 example/features/example.feature
@@ -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
View
4 example/selectors.yml
@@ -19,4 +19,6 @@ IDIndex1: 1
Hidden Div: "#secret-container"
Wait Test: "#wait_test"
Add: "#add"
-Remove: "#remove"
+Remove: "#remove"
+Hide: "#hide"
+Unhide: "#unhide"
View
26 example/www/index.html
@@ -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>
@@ -46,7 +62,9 @@
</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>
View
47 src/OrangeDigital/BusinessSelectorExtension/Context/UIBusinessSelectorContext.php
@@ -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");
+ }
}
+
}
/**
View
96 tests/Context/UIBusinessSelectorContextTest.php
@@ -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);
@@ -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);
@@ -1042,14 +1078,20 @@ 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');
@@ -1057,6 +1099,46 @@ public function testWaitForComponentShouldThrowExceptionIfElementDoesNotDisappea
$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');
+ }
+
Please sign in to comment.
Something went wrong with that request. Please try again.