Skip to content

Commit

Permalink
[#303] Updated WaitTrait.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Oct 26, 2024
1 parent 061f871 commit 18fa574
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
3 changes: 3 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ A migration map of the step definitions available in v2 to v3.
|------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **[`ParagraphsTrait`](src/ParagraphsTrait.php) ([example](tests/behat/features/paragraphs.feature))** | |
| `When :field_name in :bundle :entity_type with :entity_field_name of :entity_field_identifer has :paragraph_type paragraph:` | `Given the following fields for the paragraph :paragraph_type exist in the field :parent_field within the :parent_bundle :parent_entity_type identified by the field :parent_lookup_field and the value :parent_lookup_value:` |
| **[`WaitTrait`](src/WaitTrait.php) ([example](tests/behat/features/wait.feature))** | |
| `Then /^(?:\|I )wait (\d+) second(s?)$/` | `When I wait for :number second(s)` |
| `Given I wait :timeout seconds for AJAX to finish` | `When I wait for :number second(s) for AJAX to finish` |
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ For migration from v2 to v3, see [MIGRATION.md](MIGRATION.md).
| `Then /^(?:\|I )should not see a visually hidden "(?P<selector>[^"]*)" element(?: with top offset of "([^"]*)" pixels)?$/` | Assert that the element with the specified CSS selector is visually hidden on the page. |
| &nbsp; | |
| **[`WaitTrait`](src/WaitTrait.php) ([example](tests/behat/features/wait.feature))** | |
| `Then /^(?:\|I )wait (\d+) second(s?)$/` | Wait for the specified number of seconds. |
| `Given I wait :timeout seconds for AJAX to finish` | Wait for AJAX to finish. |
| `When I wait for :number second(s)` | Wait for a specified number of seconds. |
| `When I wait for :number second(s) for AJAX to finish` | Wait for the AJAX calls to finish. |
| &nbsp; | |
| **[`WysiwygTrait`](src/WysiwygTrait.php) ([example](tests/behat/features/wysiwyg.feature))** | |
| `When I fill in WYSIWYG :field with :value` | Set the value for the WYSIWYG field. |
Expand Down
24 changes: 10 additions & 14 deletions src/WaitTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Trait WaitTrait.
*
* Wait for time or other actions on the page.
* Wait for a specific time or other actions on the page.
*
* @package DrevOps\BehatSteps
*/
Expand All @@ -18,21 +18,21 @@ trait WaitTrait {
/**
* Wait for a specified number of seconds.
*
* @Then /^(?:|I )wait (\d+) second(s?)$/
* @When I wait for :seconds second(s)
*/
public function waitSeconds(int|string $seconds): void {
sleep((int) $seconds);
}

/**
* Wait for AJAX to finish.
* Wait for the AJAX calls to finish.
*
* @see \Drupal\FunctionalJavascriptTests\JSWebAssert::assertWaitOnAjaxRequest()
*
* @Given I wait :timeout seconds for AJAX to finish
* @When I wait for :seconds second(s) for AJAX to finish
*/
public function waitForAjaxToFinish(string|int $timeout): void {
$timeout = intval($timeout);
public function waitForAjaxToFinish(string|int $seconds): void {
$seconds = intval($seconds);

$driver = $this->getSession()->getDriver();

Expand All @@ -48,24 +48,20 @@ public function waitForAjaxToFinish(string|int $timeout): void {
function isAjaxing(instance) {
return instance && instance.ajaxing === true;
}
var d7_not_ajaxing = true;
if (typeof Drupal !== 'undefined' && typeof Drupal.ajax !== 'undefined' && typeof Drupal.ajax.instances === 'undefined') {
for(var i in Drupal.ajax) { if (isAjaxing(Drupal.ajax[i])) { d7_not_ajaxing = false; } }
}
var d8_not_ajaxing = (typeof Drupal === 'undefined' || typeof Drupal.ajax === 'undefined' || typeof Drupal.ajax.instances === 'undefined' || !Drupal.ajax.instances.some(isAjaxing))
var not_ajaxing = (typeof Drupal === 'undefined' || typeof Drupal.ajax === 'undefined' || typeof Drupal.ajax.instances === 'undefined' || !Drupal.ajax.instances.some(isAjaxing))
return (
// Assert no AJAX request is running (via jQuery or Drupal) and no
// animation is running.
(typeof jQuery === 'undefined' || (jQuery.active === 0 && jQuery(':animated').length === 0)) &&
d7_not_ajaxing && d8_not_ajaxing
not_ajaxing
);
}());
JS;

$result = $this->getSession()->wait($timeout * 1000, $condition);
$result = $this->getSession()->wait($seconds * 1000, $condition);

if (!$result) {
throw new \RuntimeException('Unable to complete AJAX request.');
throw new \RuntimeException('Unable to complete an AJAX request.');
}
}

Expand Down
18 changes: 9 additions & 9 deletions tests/behat/features/wait.feature
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
@api @javascript
Feature: Check that WaitTrait works

Scenario: Anonymous user visits homepage
Scenario: Assert "When I wait for :seconds second(s)"
Given I go to the homepage
And wait 1 second
And I wait for 1 second
Then I save screenshot
And wait 2 seconds
And I wait for 2 seconds
Then I save screenshot
And I wait 1 second
And I wait for 1 second
Then I save screenshot
And I wait 2 seconds
And I wait for 2 seconds

Scenario: Wait ajax
Scenario: Assert "When I wait for :seconds second(s) for AJAX to finish"
Given I am logged in as a user with the "administrator" role
Then I visit "admin/structure/types/manage/page/form-display"
Then I should not see an "input[name=fields\[title\]\[settings_edit_form\]\[settings\]\[placeholder\]]" element
Then I press the "title_settings_edit" button
Then I wait "5" seconds for AJAX to finish
Then I wait for "5" seconds for AJAX to finish
Then I should see an "input[name=fields\[title\]\[settings_edit_form\]\[settings\]\[placeholder\]]" element

@trait:WaitTrait
Scenario: Assert wait number of seconds for AJAX to finish can be used only with JS-capable driver
Scenario: Assert that negative assertion for "When I wait for :seconds second(s) for AJAX to finish" can be used only with JS-capable driver
Given some behat configuration
And scenario steps tagged with "@api":
"""
Given I am logged in as a user with the "administrator" role
Then I visit "admin/structure/types/manage/page/form-display"
Then I should not see an "input[name=fields\[title\]\[settings_edit_form\]\[settings\]\[placeholder\]]" element
Then I press the "title_settings_edit" button
Then I wait "5" seconds for AJAX to finish
Then I wait for "5" seconds for AJAX to finish
Then I should see an "input[name=fields\[title\]\[settings_edit_form\]\[settings\]\[placeholder\]]" element
"""
When I run "behat --no-colors"
Expand Down

0 comments on commit 18fa574

Please sign in to comment.