Issue #1681374 by ksbalajisundar: Modified scenario to be more short and readable
[sandbox/eliza411/1663360.git] / features / bootstrap / FeatureContext.php
1 <?php
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;
10
11 class FeatureContext extends MinkContext
12 {
13 /**
14 * Initializes context.
15 *
16 * Every scenario gets its own context object.
17 *
18 * @param array $parameters.
19 * Context parameters (set them up through behat.yml).
20 */
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));
25 }
26
27 /** @BeforeFeature */
28 public static function prepareForTheFeature() {
29 // clean database or do other preparation stuff
30 }
31
32 /**
33 * @Then /^I should see the heading "([^"]*)"$/
34 */
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) {
41 return;
42 }
43 }
44 }
45 throw new Exception("The text " . $headingname . " was not found in any
46 heading " . $this->getSession()->getCurrentUrl());
47 }
48
49 /**
50 * @Then /^I login to the site$/
51 */
52 public function iLoginToTheSite()
53 {
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');
58 if (empty($submit)) {
59 throw new Exception('No submit button at ' .
60 $this->getSubcontext('subcontext_alias')->getSession()->getCurrentUrl());
61 }
62 $submit->click();
63 }
64
65 /**
66 * @Then /^I should see the link "([^"]*)"$/
67 */
68 public function iShouldSeeTheLink($linkname) {
69 $element = $this->getSession()->getPage();
70 $result = $element->findLink($linkname);
71 if (empty($result)) {
72 throw new Exception("No link to " . $linkname . " on " . $this->getSession()->getCurrentUrl());
73 }
74 }
75
76 /**
77 * @Then /^I should not see the link "([^"]*)"$/
78 */
79 public function iShouldNotSeeTheLink($linkname) {
80 $element = $this->getSession()->getPage();
81 $result = $element->findLink($linkname);
82 if ($result) {
83 throw new Exception("The link " . $linkname . " was present on " . $session->getCurrentUrl() . " and was not supposed to be.");
84 }
85 }
86
87 /**
88 * @Then /^I should see "([^"]*)" links on the right sidebar$/
89 */
90 public function iShouldSeeLinksOnTheRightSidebar($count)
91 {
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');
98 }
99
100 /**
101 * @Then /^I should see the project$/
102 */
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());
108 }
109 }
110
111 /**
112 * @Given /^I fill in searchBox with "([^"]*)"$/
113 */
114 public function iFillInSearchboxWith(l$input)
115 {
116 $this->fillField("projects",$input);
117 }
118
119 /**
120 * @Given /^I select "([^"]*)" from the suggestion$/
121 */
122 public function iSelectFromTheSuggestion($value)
123 {
124 $element = $this->getSession()->getPage();
125 $element->fillField('Project', $value);
126 $this->project_value = $value;
127 }
128
129 /**
130 * @When /^I press search to filter$/
131 */
132 public function iPressSearchToFilter()
133 {
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());
141 }
142 $element->pressButton($button);
143
144 }
145 /**
146 * @When /^I press "([^"]*)" to filter$/
147 */
148 public function iPressToFilter($arg1)
149 {
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());
154 }*/
155 if(!($submit->click())) {
156 throw new Exception('No Click happened at ' . $this->getSession()->getCurrentUrl());
157 }
158 }
159
160 /**
161 * @Given /^I should see atleast "([^"]*)" records$/
162 */
163 public function iShouldSeeAtleastRecords($count)
164 {
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");
170 }
171 }
172
173 /**
174 * @When /^I click the table heading "([^"]*)"$/
175 */
176 public function iClickTheTableHeading($column)
177 {
178 $count = 0;
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) {
184 $count++;
185 $href = $text->getAttribute("href");
186 $this->getSession()->visit($href);
187 break;
188 }
189 }
190 if ($count == 0) {
191 throw new Exception("The page does not have a table with the heading '" . $column . "'");
192 }
193 }
194 else {
195 throw new Exception("The page has no table headings");
196 }
197 }
198
199 /**
200 * @Then /^I should see "([^"]*)" sorted in "([^"]*)" order$/
201 */
202 public function iShouldSeeSortedInOrder($column, $order)
203 {
204 $column_class = "";
205 $count = 0;
206 $date = FALSE;
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) {
211 $count = 1;
212 $class = $text->getAttribute("class");
213 $temp = explode(" ", $class);
214 $column_class = $temp[1];
215 break;
216 }
217 }
218 if ($count == 0) {
219 throw new Exception("The page does not have a table with column '" . $column . "'");
220 }
221 $count = 0;
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
226 $loop = 1;
227 date_default_timezone_set ("UTC");
228 foreach ($items as $item) {
229 $text = $item->getText();
230 if ($loop == 1) {
231 // check if the text is date field
232 if ($this->isStringDate($text)) {
233 $date = TRUE;
234 }
235 }
236 if ($date) {
237 $orig_arr[] = $this->isStringDate($text);
238 }
239 else {
240 $orig_arr[] = $text;
241 }
242 $loop = 2;
243 }
244 // create a temp array for sorting and comparing
245 $temp_arr = $orig_arr;
246 // sort
247 if ($order == "ascending") {
248 if ($date) {
249 sort($temp_arr, SORT_NUMERIC);
250 }
251 else {
252 sort($temp_arr);
253 }
254 }
255 elseif ($order == "descending") {
256 if ($date) {
257 rsort($temp_arr, SORT_NUMERIC);
258 }
259 else {
260 rsort($temp_arr);
261 }
262 }
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]) {
266 $count++;
267 }
268 }
269 // if all indexs match, then count will be same as array size
270 if ($count == sizeof($temp_arr)) {
271 return true;
272 }
273 else {
274 throw new Exception("The column '" . $column . "' is not sorted in " . $order . " order");
275 }
276 }
277 else {
278 throw new Exception("The column '" . $column . "' is not sorted in " . $order . " order");
279 }
280 }
281
282 /**
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
286 */
287 public function isStringDate($string) {
288 $return = "";;
289 $string = trim($string);
290 if ($string) {
291 $time = strtotime($string);
292 if ($time === FALSE) {
293 $return = FALSE;
294 }
295 elseif(is_numeric($time) && strlen($time) == 10) {
296 return $time;
297 }
298 else {
299 $return = FALSE;
300 }
301 }
302 else {
303 $return = FALSE;
304 }
305 return $return;
306 }
307
308 /**
309 * @Given /^I should not see the following <texts>$/
310 */
311 public function iShouldNotSeeTheFollowingTexts(TableNode $table)
312 {
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");
319 }
320 }
321 }
322
323 /**
324 * @Given /^I should see the following <texts>$/
325 */
326 public function iShouldSeeTheFollowingTexts(TableNode $table)
327 {
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");
334 }
335 }
336 }
337
338 /**
339 * @Given /^I should see the following <links>$/
340 */
341 public function iShouldSeeTheFollowingLinks(TableNode $table)
342 {
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);
348 if(empty($result)) {
349 throw new Exception("The link '" . $link . "' was not found");
350 }
351 }
352 }
353
354 /**
355 * @Given /^I should not see the following <links>$/
356 */
357 public function iShouldNotSeeTheFollowingLinks(TableNode $table)
358 {
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");
366 }
367 }
368 }
369
370 /**
371 * @When /^I click on page "([^"]*)"$/
372 */
373 public function iClickOnPage($pager)
374 {
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);
382 return;
383 }
384 }
385 if ($pager == "first") {
386 $class = '.pager .pager-first a';
387 }
388 elseif ($pager == "previous") {
389 $class = '.pager .pager-previous a';
390 }
391 elseif ($pager == "next") {
392 $class = '.pager .pager-next a';
393 }
394 elseif ($pager == "last") {
395 $class = '.pager .pager-last a';
396 }
397 else {
398 throw new Exception("The page '" . $pager . "' was not found");
399 }
400 $result = $page->find('css', $class);
401 $href = $result->getAttribute("href");
402 $this->getSession()->visit($href);
403 }
404 }
405 ?>