From 7805423ce283516e311a4d6a2316ea45ed50ebad Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Mon, 25 Jun 2018 11:21:20 -0700 Subject: [PATCH] Run remote testing with firefox too --- .travis.yml | 18 ++++++++- Rakefile | 4 +- docker-compose.yml | 10 ++++- spec/selenium_spec_chrome_remote.rb | 2 +- spec/selenium_spec_firefox_remote.rb | 57 ++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 spec/selenium_spec_firefox_remote.rb diff --git a/.travis.yml b/.travis.yml index 9daaa76ab..0f77dd67b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,6 +35,9 @@ matrix: - gemfile: Gemfile rvm: 2.5.1 env: CAPYBARA_CHROME_REMOTE=true + - gemfile: Gemfile + rvm: 2.5.1 + env: CAPYBARA_FIREFOX_REMOTE=true - gemfile: gemfiles/Gemfile.rspec-34 rvm: 2.3.6 env: CAPYBARA_FF=true @@ -71,6 +74,7 @@ matrix: - gemfile: gemfiles/Gemfile.beta-versions - gemfile: gemfiles/Gemfile.edge-marionette - env: CAPYBARA_CHROME_REMOTE=true + - env: CAPYBARA_FIREFOX_REMOTE=true before_install: - gem update --system - gem install bundler @@ -82,7 +86,7 @@ before_install: bundle config local.selenium-webdriver ../selenium/build/rb; fi before_script: - - if [[ -z $HEADLESS && -z $CAPYBARA_CHROME_REMOTE ]]; then + - if [[ -z $HEADLESS && -z $CAPYBARA_CHROME_REMOTE && -z $CAPYBARA_FIREFOX_REMOTE ]]; then export DISPLAY=:99.0; sh -e /etc/init.d/xvfb start; sleep 1; @@ -91,7 +95,7 @@ before_script: awesome & fi - if [[ $CAPYBARA_CHROME_REMOTE = true ]]; then - docker-compose up -d selenium + docker-compose up -d selenium_chrome TIMEOUT=10 @@ -100,5 +104,15 @@ before_script: sleep 1; done fi + - if [[ $CAPYBARA_FIREFOX_REMOTE = true ]]; then + docker-compose up -d selenium_firefox + + TIMEOUT=10 + + until wget --spider http://localhost:4445 > /dev/null 2>&1 || [ $TIMEOUT -eq 0 ]; do + echo "Waiting for selenium server, $((TIMEOUT--)) remaining attempts..."; + sleep 1; + done + fi script: "bundle exec rake travis" diff --git a/Rakefile b/Rakefile index d7a900e8f..346e69c18 100644 --- a/Rakefile +++ b/Rakefile @@ -14,7 +14,7 @@ RSpec::Core::RakeTask.new(:spec_marionette) do |t| t.pattern = './spec{,/*/**}/*{_spec.rb,_spec_marionette.rb}' end -%w[chrome ie edge chrome_remote].each do |driver| +%w[chrome ie edge chrome_remote firefox_remote].each do |driver| RSpec::Core::RakeTask.new(:"spec_#{driver}") do |t| t.rspec_opts = rspec_opts t.pattern = "./spec/*{_spec_#{driver}.rb}" @@ -46,6 +46,8 @@ task :travis do Rake::Task[:spec_edge].invoke elsif ENV['CAPYBARA_CHROME_REMOTE'] Rake::Task[:spec_chrome_remote].invoke + elsif ENV['CAPYBARA_FIREFOX_REMOTE'] + Rake::Task[:spec_firefox_remote].invoke else Rake::Task[:spec_chrome].invoke end diff --git a/docker-compose.yml b/docker-compose.yml index 735cd6248..064e8decb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,9 +2,17 @@ version: "2.1" services: - selenium: + selenium_chrome: network_mode: "host" image: "selenium/${SELENIUM_IMAGE:-standalone-chrome-debug}" volumes: - "/dev/shm:/dev/shm" - "${PWD}:${PWD}" # For making attach_file specs work + selenium_firefox: + network_mode: "host" + image: "selenium/${SELENIUM_IMAGE:-standalone-firefox-debug}" + volumes: + - "/dev/shm:/dev/shm" + - "${PWD}:${PWD}" # For making attach_file specs work + environment: + - SE_OPTS=-port 4445 diff --git a/spec/selenium_spec_chrome_remote.rb b/spec/selenium_spec_chrome_remote.rb index eb83ca5e2..ea13777a1 100644 --- a/spec/selenium_spec_chrome_remote.rb +++ b/spec/selenium_spec_chrome_remote.rb @@ -45,7 +45,7 @@ module TestSessions Capybara::SpecHelper.run_specs TestSessions::Chrome, CHROME_REMOTE_DRIVER.to_s, capybara_skip: skipped_tests -RSpec.describe "Capybara::Session with chrome" do +RSpec.describe "Capybara::Session with remote Chrome" do include Capybara::SpecHelper include_examples "Capybara::Session", TestSessions::Chrome, CHROME_REMOTE_DRIVER include_examples Capybara::RSpecMatchers, TestSessions::Chrome, CHROME_REMOTE_DRIVER diff --git a/spec/selenium_spec_firefox_remote.rb b/spec/selenium_spec_firefox_remote.rb new file mode 100644 index 000000000..514e940db --- /dev/null +++ b/spec/selenium_spec_firefox_remote.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'selenium-webdriver' +require 'shared_selenium_session' +require 'rspec/shared_spec_matchers' + +def selenium_host + ENV.fetch('SELENIUM_HOST', '0.0.0.0') +end + +def selenium_port + ENV.fetch('SELENIUM_PORT', 4445) +end + +def ensure_selenium_running! + TCPSocket.open(selenium_host, selenium_port) +rescue + raise 'Selenium is not running. ' \ + "You can run a selenium server easily with: \n" \ + ' $ docker-compose up -d selenium' +end + +Capybara.register_driver :selenium_firefox_remote do |app| + ensure_selenium_running! + + url = "http://#{selenium_host}:#{selenium_port}/wd/hub" + caps = Selenium::WebDriver::Remote::Capabilities.firefox + + Capybara::Selenium::Driver.new app, + browser: :remote, + desired_capabilities: caps, + url: url +end + +FIREFOX_REMOTE_DRIVER = :selenium_firefox_remote + +module TestSessions + Firefox = Capybara::Session.new(FIREFOX_REMOTE_DRIVER, TestApp) +end + +skipped_tests = %i[response_headers status_code trigger download] +# skip window tests when headless for now - closing a window not supported by chromedriver/chrome +skipped_tests << :windows if ENV['TRAVIS'] && (ENV['SKIP_WINDOW'] || ENV['HEADLESS']) + +Capybara::SpecHelper.run_specs TestSessions::Firefox, FIREFOX_REMOTE_DRIVER.to_s, capybara_skip: skipped_tests + +RSpec.describe "Capybara::Session with remote firefox" do + include Capybara::SpecHelper + include_examples "Capybara::Session", TestSessions::Firefox, FIREFOX_REMOTE_DRIVER + include_examples Capybara::RSpecMatchers, TestSessions::Firefox, FIREFOX_REMOTE_DRIVER + + it 'is considered to be firefox' do + expect(session.driver.send(:firefox?)).to be_truthy + expect(session.driver.send(:marionette?)).to be_truthy + end +end