Skip to content

Commit

Permalink
Update test helper example to match the test example. Thanks @mochetts
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Nov 18, 2020
1 parent 672a999 commit 96c6c8d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rails.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ jobs:
- name: Precompile Assets
run: bin/bundle exec rake webpacker:compile
- name: Run RSpecs
run: bin/bundle exec rspec
- name: Run Cucumber
run: bin/bundle exec cucumber
- name: Run RSpecs
run: bin/bundle exec rspec
- name: Upload coverage results to GitHub
uses: actions/upload-artifact@main
with:
Expand Down
1 change: 1 addition & 0 deletions examples/rails_app/features/cities/manage.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@selenium_chrome_headless
Feature: Cities
Background:
Given there is an "NYC" city
Expand Down
8 changes: 8 additions & 0 deletions examples/rails_app/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@

# Make the default helpers available in all files.
config.include(DefaultTestHelpers, type: :system)

config.before(:all, type: :system) {
driven_by(:selenium_chrome_headless)
}

config.before(:each, type: :system) {
driven_by(:selenium_chrome_headless)
}
end

begin
Expand Down
55 changes: 55 additions & 0 deletions examples/rails_app/test_helpers/downloads_test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

class DownloadsTestHelper < BaseTestHelper
DOWNLOADS_DIR = Pathname.new(__dir__).join('../tmp', "capybara_downloads#{ ENV['TEST_ENV_NUMBER'] }")
DOWNLOAD_WAIT_TIME = 5.seconds

# Getters: A convenient way to get related data or nested elements.
private \
def downloaded_files
Dir.glob(DOWNLOADS_DIR.join('*')).sort_by { |path| file_timestamp(path) }.reverse
end

private \
def file_timestamp(path)
File.mtime(path)
rescue StandardError
5.years.from_now
end

# Public: Returns the path for the last downloaded file.
def downloaded_file_path(name: nil, extension: nil, wait: DOWNLOAD_WAIT_TIME)
synchronize_expectation(wait: wait) {
expect(downloaded_files).to be_present, "No download files in #{ DOWNLOADS_DIR }"
file_path = downloaded_files.find { |file| file.include?(name.to_s) && file.include?(extension.to_s) } || downloaded_files.first
expect(file_path).not_to include('.part'), "Incomplete download file: #{ file_path.inspect }"
expect(file_path).not_to include('crdownload'), "Incomplete download file: #{ file_path.inspect }"
expect(file_path).to include(name), "Download does not match #{ name.inspect } name: #{ file_path.inspect }" if name
expect(file_path).to match(/\.#{ extension }$/), "Download does not match .#{ extension } extension: #{ file_path.inspect }" if extension
file_path
}
end

# Actions: Encapsulate complex actions to provide a cleaner interface.
def create_download_dir
FileUtils.mkdir_p(DOWNLOADS_DIR)
delete_earlier_downloads
end

def delete_earlier_downloads
FileUtils.rm Dir.glob(DOWNLOADS_DIR.join('*'))
end

def delete_download_dir
FileUtils.remove_dir(DOWNLOADS_DIR) if Dir.exist?(DOWNLOADS_DIR)
end

# Assertions: Allow to check on element properties while keeping it DRY.
def should_have_file_by_name(filename, **options)
name, extension = filename.split('.')
downloaded_filename = File.basename(downloaded_file_path(extension: extension, **options))

# Since the current date is inserted into file names, we are matching using regex.
expect(downloaded_filename).to match(/^#{ name }/)
end
end

0 comments on commit 96c6c8d

Please sign in to comment.