Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added steps: scroll to element and check element at the top of page. #257

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ Modification of `behat.yml` configuration is not required.
| `When I do not accept confirmation dialogs` | Do not accept confirmation dialogs appearing on the page. |
| `When /^(?:\|I )click (an?\|on) "(?P<element>[^"]*)" element$/` | Click on the element defined by the selector. |
| `When I trigger JS :event event on :selector element` | Trigger an event on the specified element. |
| `Then /^I scroll to an? element with id "([^"]*)"$/` | Scroll to an element with ID. |
| `Then the element with id :id should be at the top of the page` | Assert the element with id at the top of page. |
| &nbsp; | |
| **[`KeyboardTrait`](src/KeyboardTrait.php) ([example](tests/behat/features/keyboard.feature))** | |
| `Given I press the :keys keys` | Press multiple keyboard keys. |
Expand Down
31 changes: 31 additions & 0 deletions src/JsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,37 @@ public function jsTriggerElementEvent(string $event, string $selector): void {
}
}

/**
* Scroll to an element with ID.
*
* @Then /^I scroll to an? element with id "([^"]*)"$/
*/
public function iScrollToElementWithId(string $id): void {
$this->getSession()->executeScript("
var element = document.getElementById('" . $id . "');
element.scrollIntoView( true );
");
}

/**
* Assert the element with id at the top of page.
*
* @Then the element with id :id should be at the top of the page
*/
public function assertElementAtTopOfPage(string $id): void {
$script = <<<JS
(function() {
var element = document.getElementById('$id');
var rect = element.getBoundingClientRect();
return (rect.top >= 0 && rect.top <= window.innerHeight);
})();
JS;
$result = $this->getSession()->evaluateScript($script);
if (!$result) {
throw new \Exception("Element with ID '$id' is not at the top of the page.");
}
}

/**
* Execute JS on an element provided by the selector.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/behat/features/js.feature
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ Feature: Check that JsTrait works
Then I do not accept confirmation dialogs
Then I press the "Test confirm" button
Then I should see the button "You canceled!"

@javascript
Scenario: Assert scroll to an element with ID.
Given I am an anonymous user
When I visit "/sites/default/files/relative.html"
Then I scroll to an element with id "main-inner"
Then the element with id "main-inner" should be at the top of the page