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