How to add wait condition in capybara scenarios?

Member

by denis , in category: Third Party Scripts , 7 days ago

How to add wait condition in capybara scenarios?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 6 days ago

@denis 

To add a wait condition in Capybara scenarios, you can use the wait method provided by Capybara. This method waits for a specified amount of time for a certain condition to be true before continuing the test execution.


Here's an example of how to add a wait condition in a Capybara scenario:

1
2
# Wait for an element with id 'my-element' to be visible on the page for up to 10 seconds
page.has_css?('#my-element', wait: 10)


In the above example, page.has_css? is used to check if an element with the id 'my-element' is visible on the page. The wait: 10 option specifies that Capybara should wait for up to 10 seconds for the element to be visible before continuing the test execution.


You can also use the wait_until method to wait for a specific condition to be true. Here's an example:

1
2
# Wait until the element with id 'my-element' is visible on the page
page.wait_until { page.has_css?('#my-element') }


In this example, page.wait_until is used to wait until the condition inside the block (in this case page.has_css?('#my-element')) is true before continuing the test execution.


By using these methods, you can add wait conditions to your Capybara scenarios to handle scenarios where elements on the page may not be immediately available due to AJAX requests, animations, or other dynamic content loading.