From d179d2d8717cdde4d82c2e283386934d5d533c20 Mon Sep 17 00:00:00 2001 From: Adrien Di Pasquale Date: Tue, 28 Jun 2022 16:30:54 +0200 Subject: [PATCH] clarify AJAX wait behaviour example in README I explained with some more details to the `!has_xpath/has_no_xpath` differences. I tried to use simple english and to --- README.md | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e2c01d26..97b9b205 100644 --- a/README.md +++ b/README.md @@ -795,13 +795,23 @@ Be aware that because of this behaviour, the following two statements are **not* equivalent, and you should **always** use the latter! ```ruby -!page.has_xpath?('a') -page.has_no_xpath?('a') +# consider a page where the `a` tag is removed through AJAX after 1s +visit(some_path) +!page.has_xpath?('a') # is false +page.has_no_xpath?('a') # is true ``` -The former would immediately fail because the content has not yet been removed. -Only the latter would wait for the asynchronous process to remove the content -from the page. +First expression: +- `has_xpath?('a')` is called right after the page visit. It is `true` because the link has not yet been removed +- Capybara does not wait upon successful assertions, therefore **it returns `true` right ahead** +- The expression returns `false` (because it is inverted with the leading `!`) + +Second expression: +- `has_no_xpath?('a')` is called right after the page visit. It is `false` because the link has not yet been removed. +- Capybara waits upon failed assertions, therefore **it does not return `false` right ahead** +- Capybara will periodically re-check the assertion up to the `default_max_wait_time` defined +- after 1s, the assertion becomes `true` (because the link has been removed) +- The expression returns `true` Capybara's RSpec matchers, however, are smart enough to handle either form. The two following statements are functionally equivalent: