googlemapsAPIを使用して現在地が表示される地図を実装したのですが、Rspecのsystem_specで地図が表示されるページにvisit
すると「ユーザーの現在地の認識」のポップアップが出てきて地図表示のテストがパスしません。
save_and_open_page
でブラウザー上に表示して手動で「ユーザーの現在地の認識」を許可すると地図が表示されるため、system_spec内で許可する動作を記述できれば良いかと考えているのですが、実装方法がわかりません。
ご教示のほどお願いいたします。
##設定
/capybara.rb
ruby
1RSpec.configure do |config| 2 Capybara.register_driver :selenium_chrome do |app| 3 options = ::Selenium::WebDriver::Chrome::Options.new 4 5 options.add_argument('--headless') 6 options.add_argument('--no-sandbox') # chrootで隔離された環境(Sandbox)での動作を無効 7 options.add_argument('--disable-gpu') 8 options.add_argument('--window-size=1400,1400') 9 10 Capybara::Selenium::Driver.new(app, browser: :chrome, options: options) 11 end 12 13 config.before(:each, type: :system) do 14 driven_by :rack_test 15 end 16 17 config.before(:each, type: :system, js: true) do 18 driven_by :selenium_chrome 19 end 20end
##テストコード
system/maps_spec.rb
ruby
1 2RSpec.describe 'Spots', type: :system do 3 describe '表示とリンクのテスト', js: true do 4 before do 5 page.accept_confirm do 6 visit spots_path 7 end 8 end 9 10 it '地図が表示されること' do 11 expect(page).to have_css '.gm-style' 12 end 13 end 14end 15
##地図表示ページのコード
maps/index.rb
ruby
1<div class="container"> 2 <div class="row"> 3 <div class="col-12 my-5 d-flex justify-content-center"> 4 <h2><i class="fas fa-map-marked-alt"></i>現在地から探す</h2> 5 </div> 6 <div id="map" class="mt-5 mx-auto"></div> 7 </div> 8</div> 9 10<script> 11 let map, geocoder, icon, storeMarker = [], infoWindow = []; 12 const stores = gon.stores; 13 let currentInfoWindow = null; 14 15 function initMap(){ 16 geocoder = new google.maps.Geocoder() 17 if(!navigator.geolocation) { 18 alert('Geolocation APIに対応していません'); 19 return false; 20 } 21 // 現在地が取得できたら地図の中心にピンを立てて表示、取得に失敗したらアラートを表示 22 navigator.geolocation.getCurrentPosition(function(position) { 23 latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 24 map = new google.maps.Map(document.getElementById('map'), { 25 center: latLng, 26 zoom: 12, 27 }); 28 image = { 29 url: "<%= asset_path('gmap_icon.png') %>", 30 scaledSize: new google.maps.Size(50, 50), 31 } 32 marker = new google.maps.Marker({ 33 position: latLng, 34 map: map, 35 icon: image, 36 animation: google.maps.Animation.DROP, 37 title:"現在地", 38 }); 39 40 for (let i = 0; i < stores.length; i++){ 41 storeMarker[i] = new google.maps.Marker({ 42 map: map, 43 position: { 44 lat: stores[i].latitude, 45 lng: stores[i].longitude, 46 } 47 }); 48 contentString = 49 `<p class="mb-0">施設名:<a href='/stores/${stores[i].id}'>${stores[i].name}</a></p>` + 50 '<hr class="my-0">' + 51 `<p class="mt-3 mb-0">施設紹介:${stores[i].introduction}</p>` + 52 `<p class="mt-1 mb-0">住所 :${stores[i].prefecture_code}${stores[i].city}</p>`; 53 54 infoWindow[i] = new google.maps.InfoWindow({ 55 content: contentString, 56 }); 57 storeMarker[i].addListener("click", () => { 58 if (currentInfoWindow) { 59 currentInfoWindow.close(); 60 } 61 infoWindow[i].open(map,storeMarker[i]); 62 currentInfoWindow = infoWindow[i]; 63 }); 64 } 65 }, function() { 66 alert('位置情報取得に失敗しました!位置情報へのアクセスが許可されているか確認してください'); 67 }); 68 } 69</script> 70 71<script async defer 72 src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=<%= Rails.application.credentials.g_map[:api] %>&callback=initMap"> 73</script>
あなたの回答
tips
プレビュー