From ff86bde03651648c2f4bca1f9613dab16180f717 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 13 Jun 2014 15:05:32 +1200 Subject: [PATCH] Step definitions for table interactions Moved from framework module which had GridField specific steps under the same naming. Made them more versatile, working for both GridField and other nodes. --- README.md | 12 +-- .../BehatExtension/Context/BasicContext.php | 74 +++++++++++++++++++ 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e8c0442..0d1f40b 100644 --- a/README.md +++ b/README.md @@ -470,6 +470,12 @@ It's based on the `vendor/bin/behat -di @cms` output. Given /^I wait (?:for )?([\d\.]+) second(?:s?)$/ + Then /^the "([^"]*)" table should contain "([^"]*)"$/ + + Then /^the "([^"]*)" table should not contain "([^"]*)"$/ + + Given /^I click on "([^"]*)" in the "([^"]*)" table$/ + ### Navigation Given /^(?:|I )am on homepage$/ @@ -610,12 +616,6 @@ It's based on the `vendor/bin/behat -di @cms` output. When /^I click the "([^"]*)" CMS tab$/ - Then /^the "([^"]*)" table should contain "([^"]*)"$/ - - Then /^the "([^"]*)" table should not contain "([^"]*)"$/ - - Given /^I click on "([^"]*)" in the "([^"]*)" table$/ - Then /^I can see the preview panel$/ Given /^the preview contains "([^"]*)"$/ diff --git a/src/SilverStripe/BehatExtension/Context/BasicContext.php b/src/SilverStripe/BehatExtension/Context/BasicContext.php index 8e8ebf3..4ae2866 100644 --- a/src/SilverStripe/BehatExtension/Context/BasicContext.php +++ b/src/SilverStripe/BehatExtension/Context/BasicContext.php @@ -676,4 +676,78 @@ JS; $session->getDriver()->click($radioButton->getXPath()); } + /** + * @Then /^the "([^"]*)" table should contain "([^"]*)"$/ + */ + public function theTableShouldContain($selector, $text) { + $table = $this->getTable($selector); + + $element = $table->find('named', array('content', "'$text'")); + assertNotNull($element, sprintf('Element containing `%s` not found in `%s` table', $text, $selector)); + } + + /** + * @Then /^the "([^"]*)" table should not contain "([^"]*)"$/ + */ + public function theTableShouldNotContain($selector, $text) { + $table = $this->getTable($selector); + + $element = $table->find('named', array('content', "'$text'")); + assertNull($element, sprintf('Element containing `%s` not found in `%s` table', $text, $selector)); + } + + /** + * @Given /^I click on "([^"]*)" in the "([^"]*)" table$/ + */ + public function iClickOnInTheTable($text, $selector) { + $table = $this->getTable($selector); + + $element = $table->find('xpath', sprintf('//*[count(*)=0 and contains(.,"%s")]', $text)); + assertNotNull($element, sprintf('Element containing `%s` not found', $text)); + $element->click(); + } + + /** + * Finds the first visible table by various factors: + * - table[id] + * - table[title] + * - table *[class=title] + * - fieldset[data-name] table + * - table caption + * + * @return Behat\Mink\Element\NodeElement + */ + protected function getTable($selector) { + $selector = $this->getSession()->getSelectorsHandler()->xpathLiteral($selector); + $page = $this->getSession()->getPage(); + $candidates = $page->findAll( + 'xpath', + $this->getSession()->getSelectorsHandler()->selectorToXpath( + "xpath", ".//table[(./@id = $selector or contains(./@title, $selector))]" + ) + ); + + // Find tables by a
field + $candidates += $page->findAll('xpath', "//table//caption[contains(normalize-space(string(.)), $selector)]/ancestor-or-self::table[1]"); + + // Find tables by a .title node + $candidates += $page->findAll('xpath', "//table//*[@class='title' and contains(normalize-space(string(.)), $selector)]/ancestor-or-self::table[1]"); + + // Some tables don't have a visible title, so look for a fieldset with data-name instead + $candidates += $page->findAll('xpath', "//fieldset[@data-name=$selector]//table"); + + assertTrue((bool)$candidates, 'Could not find any table elements'); + + $table = null; + foreach($candidates as $candidate) { + if(!$table && $candidate->isVisible()) { + $table = $candidate; + } + } + + assertTrue((bool)$table, 'Found table elements, but none are visible'); + + return $table; + } + }