Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

Commit

Permalink
Merge branch 'gimler-patch-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
sanpii committed Jun 22, 2016
2 parents e4085a9 + d009330 commit 977fa9e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
4 changes: 4 additions & 0 deletions i18n/en.xliff.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<source>(I )follow the :index :link link</source>
<target></target>
</trans-unit>
<trans-unit id="i-press-the-nth-button">
<source>(I )press the :index :button button</source>
<target></target>
</trans-unit>
<trans-unit id="i-fill-in-with-the-current-date">
<source>(I )fill in :field with the current date</source>
<target></target>
Expand Down
14 changes: 14 additions & 0 deletions src/Context/BaseContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ protected function countElements($element, $index, $parent)
}

$elements = $parents[$index - 1]->findAll('css', $element);

return count($elements);
}

protected function findElement($selector, $locator, $index)
{
$page = $this->getSession()->getPage();

$nodes = $page->findAll($selector, $locator);

if (!isset($nodes[$index - 1])) {
throw new \Exception("The $index $selector '$locator' was not found anywhere in the page");
}

return $nodes[$index - 1];
}
}
34 changes: 16 additions & 18 deletions src/Context/BrowserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,8 @@ public function iAmOnUrlComposedBy(TableNode $tableNode)
*/
public function iClickOnTheNthElement($index, $element)
{
$nodes = $this->getSession()->getPage()->findAll('css', $element);

if (isset($nodes[$index - 1])) {
$nodes[$index - 1]->click();
}
else {
throw new \Exception("The element '$element' number $index was not found anywhere in the page");
}
$node = $this->findElement('css', $element, $index);
$node->click();
}

/**
Expand All @@ -75,17 +69,21 @@ public function iClickOnTheNthElement($index, $element)
*/
public function iFollowTheNthLink($index, $link)
{
$page = $this->getSession()->getPage();

$links = $page->findAll('named', [
'link', $this->getSession()->getSelectorsHandler()->xpathLiteral($link)
]);

if (!isset($links[$index - 1])) {
throw new \Exception("The $index element '$link' was not found anywhere in the page");
}
$element = ['link', $this->getSession()->getSelectorsHandler()->xpathLiteral($link)];
$node = $this->findElement('named', $element, $index);
$node->click();
}

$links[$index - 1]->click();
/**
* Presses the nth specified button
*
* @When (I )press the :index :button button
*/
public function pressTheNthButton($index, $button)
{
$element = ['button', $this->getSession()->getSelectorsHandler()->xpathLiteral($button)];
$node = $this->findElement('named', $element, $index);
$node->click();
}

/**
Expand Down
6 changes: 5 additions & 1 deletion tests/features/browser.feature
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ Feature: Browser Feature
And the "months_selector" select box should not contain "december"
And the "months_selector" select box should contain "january"
When I click on the 1st "ul li" element
Then I should see "You clicked First"
Then I should see "You clicked First LI"
When I press the 2nd "Submit" button
Then I should see "You clicked Second BUTTON"
When I follow the 1st "Second" link
Then I should see "You clicked Second A"

@javascript
Scenario: Frames testing
Expand Down
9 changes: 7 additions & 2 deletions tests/fixtures/www/browser/elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
</ul>
<span id="element"></span>
</div>
<a href="#">First</a>
<a href="#">Second</a>
<form>
<input type="text" name="today" />

<button type="button" title="Submit">First</button>
<button type="button" title="Submit">Second</button>
</form>
<script>
[].forEach.call(document.querySelectorAll('li'), function(el) {
[].forEach.call(document.querySelectorAll('li, a, button'), function(el) {
el.addEventListener('click', function() {
document.getElementById('element').innerHTML = 'You clicked ' + el.innerHTML;
document.getElementById('element').innerHTML = 'You clicked ' + el.innerHTML + ' ' + el.tagName;
})
});
</script>
Expand Down

0 comments on commit 977fa9e

Please sign in to comment.