开发者

How can I alleviate timing/AJAX woes using Capybara/Capybara Webkit/RSpec click_button and page.select?

For the sake of simplicity, I've left out most of my test and only included the offending code. It is:

click_button('Search')  
page.select 'Preferred', :from => 'ticket_service_type'  

When I run this, I receive the following:

Failure/Error: page.select 'Preferred', :from => 'ticket_service_type'
Capybara::ElementNotFound:
  cannot select option, no select box with id, name, or label 'ticket_service_type' found`

The AJAX request this button click event triggers doesn't have anything to do with the select tag, so reversing the order in the test causes the test to pass. I know that Capybara's default wait time is 2 seconds and so I changed it to 10 with:

Capybara.default_wait_time = 10

This does not cause the test to pass. How can I get these two methods to play nice with one another and work in the order in which a user would operate the web page?

(Had I posted the code from my spec, I bet t开发者_运维技巧his would have been solved quickly.)


From The Cucumber Book (emphasis mine):

Luckily, Capybara knows how to deal with this situation in a simple way. If we add an explicit call to find, passing a CSS selector for a DOM element on the page that doesn’t yet exist, Capybara will wait a little (50ms) and try again until the element appears. If it doesn’t appear after a while (two seconds by default, though this is configurable), it will raise an exception, causing the step definition to fail.

So have your AJAX write something to the DOM then find() it in your step definition. It's not ideal. In my case I'm introducing a (hidden) DOM element just to facilitate testing but I haven't found another way.


Be sure to add :js => true to integration tests which depend upon JavaScript.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜