开发者

How to test a scenario that requires javascript to complete?

开发者 https://www.devze.com 2023-04-09 10:22 出处:网络
A project I\'m working on fetches data from a 3rd party service and caches it locally along with specific info for my app.

A project I'm working on fetches data from a 3rd party service and caches it locally along with specific info for my app.

My environment:

  • I provide a search mechanism against the service's search API.
  • When a user browses to the item page for the first time, the my server fetches the info from the service and stores it
  • On subsequent requests the browser just requests the information from my server

.. however.. I want info from the 3rd party service to stay up to date.

My plan (feedback welcome!)

  • On a subsequent request after page load, some JS will ping the 3rd party service to check for updates
  • If the item has been updated, push an update to my copy server side

How to test this?

When I write a controller spec I don't think the page's javascript is executed (to fetch the since-updated item from the service)

I had quite a bit of trouble trying to describe this so a diagram might help.

How to test a scenario that requires javascript to complete?

Technology开发者_高级运维 I'm using: Rails/RSpec/Factory Girl/JQuery


Let's think out of the box a little to solve this.

Can you hit the 3rd Party Service from the Back End when the page is requested? Then you remove the dependance on JavaScript.

There are a few other methods that are better than this, like putting the "update item information" action into a queue, and then processing it in the background on the Back End. This would allow for the 3rd Party Service to not slow your site down.


It looks like there are a few ways to test this assuming my current solution.

Thoughtbot makes a library called capybara-webkit that will spin up webkit in the background and render/run your tests through it. Selenium is another.

github link

blog post

back on this project after a bit of a hiatus.. using capybara and :js => true seems to work - some tricky timing issues, but otherwise good to go

0

精彩评论

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

关注公众号