Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for next page being loaded with Capybara and Selenium?

Let's suppose you want to submit a form to create a post, wait for it to be created, fetch the post from database and check if the image from the post is being displayed on the next page:

visit url
fill_in the_form
click_on 'Create'
post = Post.first
img = page.find '.post .image'
assert_equal post.file.thumb.url, URI(img[:src]).path

But this way, post will be nil more often than not. Since when you fetch it from database, it might not have been created yet. How do I make sure next page is loaded?

like image 649
x-yuri Avatar asked Nov 16 '25 01:11

x-yuri


1 Answers

find will wait up to Capybara.default_max_wait_time seconds for a matching element to appear on the screen. Therefore, to do what you're asking, check for content you expect to be on the next page before loading the item from the DB

visit url
fill_in the_form
click_on 'Create'
img = page.find '.post .image'
post = Post.first
assert_equal post.file.thumb.url, URI(img[:src]).path

If done in this order the page.find will wait for the element to appear on the page which guarantees the Post has already been saved so you can then load it.

like image 154
Thomas Walpole Avatar answered Nov 18 '25 20:11

Thomas Walpole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!