diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb
index e196455206e4d339c5d015a74f0440dc77959a6e..9f672bc92fc4c239add8ed731e6d92d82cd6593f 100644
--- a/spec/support/capybara.rb
+++ b/spec/support/capybara.rb
@@ -41,13 +41,17 @@ RSpec.configure do |config|
   end
 
   config.before(:example, :js) do
+    session = Capybara.current_session
+
     allow(Gitlab::Application.routes).to receive(:default_url_options).and_return(
-      host: Capybara.current_session.server.host,
-      port: Capybara.current_session.server.port,
+      host: session.server.host,
+      port: session.server.port,
       protocol: 'http')
 
     # reset window size between tests
-    Capybara.current_session.current_window.resize_to(1240, 1400)
+    unless session.current_window.size == [1240, 1400]
+      session.current_window.resize_to(1240, 1400) rescue nil
+    end
   end
 
   config.after(:example, :js) do |example|