Skip to content

Commit 977fa9e

Browse files
committed
Merge branch 'gimler-patch-1'
2 parents e4085a9 + d009330 commit 977fa9e

File tree

5 files changed

+46
-21
lines changed

5 files changed

+46
-21
lines changed

i18n/en.xliff.dist

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
<source>(I )follow the :index :link link</source>
2020
<target></target>
2121
</trans-unit>
22+
<trans-unit id="i-press-the-nth-button">
23+
<source>(I )press the :index :button button</source>
24+
<target></target>
25+
</trans-unit>
2226
<trans-unit id="i-fill-in-with-the-current-date">
2327
<source>(I )fill in :field with the current date</source>
2428
<target></target>

src/Context/BaseContext.php

+14
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ protected function countElements($element, $index, $parent)
131131
}
132132

133133
$elements = $parents[$index - 1]->findAll('css', $element);
134+
134135
return count($elements);
135136
}
137+
138+
protected function findElement($selector, $locator, $index)
139+
{
140+
$page = $this->getSession()->getPage();
141+
142+
$nodes = $page->findAll($selector, $locator);
143+
144+
if (!isset($nodes[$index - 1])) {
145+
throw new \Exception("The $index $selector '$locator' was not found anywhere in the page");
146+
}
147+
148+
return $nodes[$index - 1];
149+
}
136150
}

src/Context/BrowserContext.php

+16-18
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,8 @@ public function iAmOnUrlComposedBy(TableNode $tableNode)
5858
*/
5959
public function iClickOnTheNthElement($index, $element)
6060
{
61-
$nodes = $this->getSession()->getPage()->findAll('css', $element);
62-
63-
if (isset($nodes[$index - 1])) {
64-
$nodes[$index - 1]->click();
65-
}
66-
else {
67-
throw new \Exception("The element '$element' number $index was not found anywhere in the page");
68-
}
61+
$node = $this->findElement('css', $element, $index);
62+
$node->click();
6963
}
7064

7165
/**
@@ -75,17 +69,21 @@ public function iClickOnTheNthElement($index, $element)
7569
*/
7670
public function iFollowTheNthLink($index, $link)
7771
{
78-
$page = $this->getSession()->getPage();
79-
80-
$links = $page->findAll('named', [
81-
'link', $this->getSession()->getSelectorsHandler()->xpathLiteral($link)
82-
]);
83-
84-
if (!isset($links[$index - 1])) {
85-
throw new \Exception("The $index element '$link' was not found anywhere in the page");
86-
}
72+
$element = ['link', $this->getSession()->getSelectorsHandler()->xpathLiteral($link)];
73+
$node = $this->findElement('named', $element, $index);
74+
$node->click();
75+
}
8776

88-
$links[$index - 1]->click();
77+
/**
78+
* Presses the nth specified button
79+
*
80+
* @When (I )press the :index :button button
81+
*/
82+
public function pressTheNthButton($index, $button)
83+
{
84+
$element = ['button', $this->getSession()->getSelectorsHandler()->xpathLiteral($button)];
85+
$node = $this->findElement('named', $element, $index);
86+
$node->click();
8987
}
9088

9189
/**

tests/features/browser.feature

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ Feature: Browser Feature
4242
And the "months_selector" select box should not contain "december"
4343
And the "months_selector" select box should contain "january"
4444
When I click on the 1st "ul li" element
45-
Then I should see "You clicked First"
45+
Then I should see "You clicked First LI"
46+
When I press the 2nd "Submit" button
47+
Then I should see "You clicked Second BUTTON"
48+
When I follow the 1st "Second" link
49+
Then I should see "You clicked Second A"
4650

4751
@javascript
4852
Scenario: Frames testing

tests/fixtures/www/browser/elements.html

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616
</ul>
1717
<span id="element"></span>
1818
</div>
19+
<a href="#">First</a>
20+
<a href="#">Second</a>
1921
<form>
2022
<input type="text" name="today" />
23+
24+
<button type="button" title="Submit">First</button>
25+
<button type="button" title="Submit">Second</button>
2126
</form>
2227
<script>
23-
[].forEach.call(document.querySelectorAll('li'), function(el) {
28+
[].forEach.call(document.querySelectorAll('li, a, button'), function(el) {
2429
el.addEventListener('click', function() {
25-
document.getElementById('element').innerHTML = 'You clicked ' + el.innerHTML;
30+
document.getElementById('element').innerHTML = 'You clicked ' + el.innerHTML + ' ' + el.tagName;
2631
})
2732
});
2833
</script>

0 commit comments

Comments
 (0)