2 use Behat\Symfony2Extension\Context\KernelAwareInterface
;
3 use Behat\MinkExtension\Context\MinkContext
;
4 use Behat\Behat\Context\ClosuredContextInterface
,
5 Behat\Behat\Context\TranslatedContextInterface
,
6 Behat\Behat\Context\BehatContext
,
7 Behat\Behat\Exception\PendingException
;
8 use Behat\Gherkin\Node\PyStringNode
,
9 Behat\Gherkin\Node\TableNode
;
11 class FeatureContext
extends MinkContext
14 * Initializes context.
16 * Every scenario gets its own context object.
18 * @param array $parameters.
19 * Context parameters (set them up through behat.yml).
21 public
function __construct(array $parameters) {
22 $this->right_sidebar
= $parameters['right_sidebar'];
23 $this->basic_auth
= $parameters['basic_auth'];
24 $this->useContext('subcontext_alias', new
SubContext($parameters));
28 public static
function prepareForTheFeature() {
29 // clean database or do other preparation stuff
33 * @Then /^I should see the heading "([^"]*)"$/
35 public
function iShouldSeeTheHeading($headingname) {
36 $element = $this->getSession()->getPage();
37 foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as
$heading) {
38 $results = $element->findAll('css', $heading);
39 foreach ($results as
$result) {
40 if ($result->getText() == $headingname) {
45 throw new
Exception("The text " .
$headingname .
" was not found in any
46 heading " .
$this->getSession()->getCurrentUrl());
50 * @Then /^I login to the site$/
52 public
function iLoginToTheSite()
54 $element = $this->getSession()->getPage();
55 $element->fillField('Username', $this->basic_auth
['username']);
56 $element->fillField('Password', $this->basic_auth
['password']);
57 $submit = $element->findButton('Log in');
59 throw new
Exception('No submit button at ' .
60 $this->getSubcontext('subcontext_alias')->getSession()->getCurrentUrl());
66 * @Then /^I should see the link "([^"]*)"$/
68 public
function iShouldSeeTheLink($linkname) {
69 $element = $this->getSession()->getPage();
70 $result = $element->findLink($linkname);
72 throw new
Exception("No link to " .
$linkname .
" on " .
$this->getSession()->getCurrentUrl());
77 * @Then /^I should not see the link "([^"]*)"$/
79 public
function iShouldNotSeeTheLink($linkname) {
80 $element = $this->getSession()->getPage();
81 $result = $element->findLink($linkname);
83 throw new
Exception("The link " .
$linkname .
" was present on " .
$session->getCurrentUrl() .
" and was not supposed to be.");
88 * @Then /^I should see "([^"]*)" links on the right sidebar$/
90 public
function iShouldSeeLinksOnTheRightSidebar($count)
92 $mainContext = $this->getMainContext();
93 $page = $mainContext->getSession()->getPage();
94 $nodes = $page->findAll("css", $mainContext->right_sidebar.
" .item-list a");
95 if (sizeof($nodes) == $count) return true
;
96 throw new
Exception('Found ' .
sizeof($nodes) .
' links instead of ' .
97 $count .
' links on the right sidebar');
101 * @Then /^I should see the project$/
103 public
function iShouldSeeTheProject() {
104 $element = $this->getSession()->getPage();
105 $result = $element->hasContent($this->project
);
106 if ($result === FALSE
) {
107 throw new
Exception("The text " .
$this->project .
" was not found " .
$session->getCurrentUrl());
112 * @Given /^I fill in searchBox with "([^"]*)"$/
114 public
function iFillInSearchboxWith(l
$input)
116 $this->fillField("projects",$input);
120 * @Given /^I select "([^"]*)" from the suggestion$/
122 public
function iSelectFromTheSuggestion($value)
124 $element = $this->getSession()->getPage();
125 $element->fillField('Project', $value);
126 $this->project_value
= $value;
130 * @When /^I press search to filter$/
132 public
function iPressSearchToFilter()
134 $button = 'edit-submit-project-issue-all-projects';
135 $element = $this->getSession()->getPage();
136 $element->fillField('Project', $this->project_value
);
137 //$submit = $element->findById('edit-submit-project-issue-all-projects');
138 $submit = $element->findButton($button);
139 if (empty($submit)) {
140 throw new
Exception('No submit button at ' .
$this->getSession()->getCurrentUrl());
142 $element->pressButton($button);
146 * @When /^I press "([^"]*)" to filter$/
148 public
function iPressToFilter($arg1)
150 $element = $this->getSession()->getPage();
151 $submit = $element->findById('edit-submit-project-issue-all-projects');
152 /* if (empty($submit)) {
153 throw new Exception('No submit button at ' . $session->getCurrentUrl());
155 if(!($submit->click())) {
156 throw new
Exception('No Click happened at ' .
$this->getSession()->getCurrentUrl());
161 * @Given /^I should see atleast "([^"]*)" records$/
163 public
function iShouldSeeAtleastRecords($count)
165 // counts the number of rows in the view table
166 $element = $this->getSession()->getPage();
167 $records = $element->findAll('css', '.view table.views-table tr');
168 if (sizeof($records) < $count) {
169 throw new
Exception("The page has less than " .
$count .
" records");
174 * @When /^I click the table heading "([^"]*)"$/
176 public
function iClickTheTableHeading($column)
179 $page = $this->getSession()->getPage();
180 $heading = $page->findAll('css', '.view table.views-table th a');
181 if (sizeof($heading)) {
182 foreach ($heading as
$text) {
183 if ($text->getText() == $column) {
185 $href = $text->getAttribute("href");
186 $this->getSession()->visit($href);
191 throw new
Exception("The page does not have a table with the heading '" .
$column .
"'");
195 throw new
Exception("The page has no table headings");
200 * @Then /^I should see "([^"]*)" sorted in "([^"]*)" order$/
202 public
function iShouldSeeSortedInOrder($column, $order)
207 $page = $this->getSession()->getPage();
208 $heading = $page->findAll('css', '.view table.views-table th');
209 foreach ($heading as
$text) {
210 if ($text->getText() == $column) {
212 $class = $text->getAttribute("class");
213 $temp = explode(" ", $class);
214 $column_class = $temp[1];
219 throw new
Exception("The page does not have a table with column '" .
$column .
"'");
222 $items = $page->findAll('css', '.view table.views-table tr td.'.
$column_class);
223 // make sure we have the data
224 if (sizeof($items)) {
225 // put all items in an array
227 date_default_timezone_set ("UTC");
228 foreach ($items as
$item) {
229 $text = $item->getText();
231 // check if the text is date field
232 if ($this->isStringDate($text)) {
237 $orig_arr[] = $this->isStringDate($text);
244 // create a temp array for sorting and comparing
245 $temp_arr = $orig_arr;
247 if ($order == "ascending") {
249 sort($temp_arr, SORT_NUMERIC
);
255 elseif ($order == "descending") {
257 rsort($temp_arr, SORT_NUMERIC
);
263 // after sorting, compare each index value of temp array & original array
264 for ($i = 0; $i < sizeof($temp_arr); $i++) {
265 if ($temp_arr[$i] == $orig_arr[$i]) {
269 // if all indexs match, then count will be same as array size
270 if ($count == sizeof($temp_arr)) {
274 throw new
Exception("The column '" .
$column .
"' is not sorted in " .
$order .
" order");
278 throw new
Exception("The column '" .
$column .
"' is not sorted in " .
$order .
" order");
283 * Function to check whether the given string is a date or not
284 * @param $string String The string to be checked for
285 * @return $return String/Bool - Return timestamp if it is date, false otherwise
287 public
function isStringDate($string) {
289 $string = trim($string);
291 $time = strtotime($string);
292 if ($time === FALSE
) {
295 elseif(is_numeric($time) && strlen($time) == 10) {
309 * @Given /^I should not see the following <texts>$/
311 public
function iShouldNotSeeTheFollowingTexts(TableNode
$table)
313 $page = $this->getSession()->getPage();
314 $table = $table->getHash();
315 foreach ($table as
$key => $value) {
316 $text = $table[$key]['texts'];
317 if(!$page->hasContent($text) === FALSE
) {
318 throw new
Exception("The text '" .
$text .
"' was found");
324 * @Given /^I should see the following <texts>$/
326 public
function iShouldSeeTheFollowingTexts(TableNode
$table)
328 $page = $this->getSession()->getPage();
329 $table = $table->getHash();
330 foreach ($table as
$key => $value) {
331 $text = $table[$key]['texts'];
332 if($page->hasContent($text) === FALSE
) {
333 throw new
Exception("The text '" .
$text .
"' was not found");
339 * @Given /^I should see the following <links>$/
341 public
function iShouldSeeTheFollowingLinks(TableNode
$table)
343 $page = $this->getSession()->getPage();
344 $table = $table->getHash();
345 foreach ($table as
$key => $value) {
346 $link = $table[$key]['links'];
347 $result = $page->findLink($link);
349 throw new
Exception("The link '" .
$link .
"' was not found");
355 * @Given /^I should not see the following <links>$/
357 public
function iShouldNotSeeTheFollowingLinks(TableNode
$table)
359 $page = $this->getSession()->getPage();
360 $table = $table->getHash();
361 foreach ($table as
$key => $value) {
362 $link = $table[$key]['links'];
363 $result = $page->findLink($link);
364 if(!empty($result)) {
365 throw new
Exception("The link '" .
$link .
"' was found");
371 * @When /^I click on page "([^"]*)"$/
373 public
function iClickOnPage($pager)
375 $page = $this->getSession()->getPage();
376 $result = $page->findAll('css', '.pager .pager-item a');
377 foreach ($result as
$temp) {
378 print $temp->getText() .
"---";
379 if (trim($temp->getText()) == trim($pager)) {
380 $href = $temp->getAttribute("href");
381 $this->getSession()->visit($href);
385 if ($pager == "first") {
386 $class = '.pager .pager-first a';
388 elseif ($pager == "previous") {
389 $class = '.pager .pager-previous a';
391 elseif ($pager == "next") {
392 $class = '.pager .pager-next a';
394 elseif ($pager == "last") {
395 $class = '.pager .pager-last a';
398 throw new
Exception("The page '" .
$pager .
"' was not found");
400 $result = $page->find('css', $class);
401 $href = $result->getAttribute("href");
402 $this->getSession()->visit($href);