You are currently browsing the tag archive for the ‘Capybara’ tag.
Tag Archive
Testing geolocation with capybara + selenium + firefox
2012-04-19 in JS, Ruby | Tags: Capybara, HTML5, Javascript, JS, RSpec, Ruby | Leave a comment
Just built this little snippet in a hacknight, it simulates a geolcation via capybara, so you can test if you geo-magic actually works
View
<p>Finding your location... <span id="status">checking...</span></p>
<script>
jQuery(function () {
var timeout = (document.location.href.indexOf('test_location') >= 0 ? 100 : 0);
setTimeout(function(){
navigator.geolocation.getCurrentPosition(function(){
jQuery('#status').html("found you!");
});
}, timeout)
});
</script>
Capybara test via selenium in rspec
def simulate_location(lat, lng)
page.driver.browser.execute_script <<-JS
window.navigator.geolocation.getCurrentPosition = function(success){
var position = {"coords" : { "latitude": "#{lat}", "longitude": "#{lng}" }};
success(position);
}
JS
end
it "can use location", :js => true do
visit '/?test_location=true'
simulate_location 20, 20
sleep 0.2
page.should have_content "found you!"
end
Capybara: use current_path_info instead of current_path / current_url
2011-11-26 in Ruby | Tags: Capybara | Leave a comment
Capybara has current_url, which returns an url that is unmatcheable since it includes a randomized port, and current_path with contains neither query nor fragment(aka hash/anchor) and is therefore rather useless.
Behold the perfect current_path_info, which returns the full path and query + fragment.
Code
def current_path_info
current_url.sub(%r{.*?://},'')[%r{[/\?\#].*}] || '/'
end
