From c1484fad36636860e6f36739b705a47f5f91c85f Mon Sep 17 00:00:00 2001 From: aguspe Date: Sun, 19 Nov 2023 16:09:39 +0100 Subject: [PATCH 1/6] Add support for multiple browser options --- lib/generators/templates/common/config.tt | 2 +- .../templates/common/partials/web_config.tt | 14 ++++++++++++++ .../templates/helpers/browser_helper.tt | 12 +++++++----- .../helpers/partials/driver_and_options.tt | 16 ++++++++++------ lib/generators/templates/helpers/spec_helper.tt | 4 ++-- 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/lib/generators/templates/common/config.tt b/lib/generators/templates/common/config.tt index 4905b8d1..a216d1a9 100644 --- a/lib/generators/templates/common/config.tt +++ b/lib/generators/templates/common/config.tt @@ -1,5 +1,5 @@ <% if %w[ios android cross_platform].include?(automation) -%> <%= ERB.new(File.read(File.expand_path('./partials/mobile_config.tt', __dir__))).result(binding) %> <% else -%> -<%= ERB.new(File.read(File.expand_path('./partials/web_config.tt', __dir__))).result(binding) %> +<%= ERB.new(File.read(File.expand_path('./partials/web_config.tt', __dir__)), trim_mode: '-').result(binding) %> <% end -%> \ No newline at end of file diff --git a/lib/generators/templates/common/partials/web_config.tt b/lib/generators/templates/common/partials/web_config.tt index e83fefc1..0b36afbc 100644 --- a/lib/generators/templates/common/partials/web_config.tt +++ b/lib/generators/templates/common/partials/web_config.tt @@ -1,3 +1,17 @@ browser: :chrome url: 'https://automationteststore.com/' +<%- if automation == 'selenium' -%> +driver_options: + :timeouts: + :implicit: 10000 # 10 seconds +<% end -%> + +browser_arguments: + :chrome: + - no-sandbox + - disable-dev-shm-usage + - ignore-certificate-errors + :firefox: + - acceptInsecureCerts + - no-sandbox \ No newline at end of file diff --git a/lib/generators/templates/helpers/browser_helper.tt b/lib/generators/templates/helpers/browser_helper.tt index 56e7e434..7ead600a 100644 --- a/lib/generators/templates/helpers/browser_helper.tt +++ b/lib/generators/templates/helpers/browser_helper.tt @@ -11,10 +11,12 @@ module BrowserHelper private + def config + @config ||= YAML.load_file('config/config.yml') + end + def create_browser(*args) - @config = YAML.load_file('config/config.yml') - browser = @config['browser'].to_sym - args = args.empty? ? @config['browser_options'] : args - @browser = Watir::Browser.new(browser, options: { args: args }) + args = args.empty? ? config['browser_arguments'][config['browser']] : args + Watir::Browser.new(config['browser'], options: { args: args }) end -end \ No newline at end of file +end diff --git a/lib/generators/templates/helpers/partials/driver_and_options.tt b/lib/generators/templates/helpers/partials/driver_and_options.tt index 613ebfdf..81d6cf8d 100644 --- a/lib/generators/templates/helpers/partials/driver_and_options.tt +++ b/lib/generators/templates/helpers/partials/driver_and_options.tt @@ -2,20 +2,24 @@ def create_driver(*opts) @config = YAML.load_file('config/config.yml') browser = @config['browser'].to_sym - Selenium::WebDriver.for(browser, options: browser_options(*opts)) + Selenium::WebDriver.for(browser, options: create_webdriver_options(*opts)) end - def browser_options(*opts) - opts = opts.empty? ? @config['browser_options'] : opts - create_options(*opts) + def browser_arguments(*opts) + opts.empty? ? @config['browser_arguments'][@config['browser']] : opts + end + + def driver_options + @config['driver_options'] end # :reek:FeatureEnvy - def create_options(*opts) + def create_webdriver_options(*opts) load_browser = @config['browser'].to_s browser = load_browser == 'ie' ? load_browser.upcase : load_browser.capitalize options = "Selenium::WebDriver::#{browser}::Options".constantize.new - opts.each { |option| options.add_argument(option) } + browser_arguments(*opts).each { |arg| options.add_argument(arg) } + driver_options.each { |opt| options.add_option(opt.first, opt.last) } options end diff --git a/lib/generators/templates/helpers/spec_helper.tt b/lib/generators/templates/helpers/spec_helper.tt index d9e453ac..e3b9c5b6 100644 --- a/lib/generators/templates/helpers/spec_helper.tt +++ b/lib/generators/templates/helpers/spec_helper.tt @@ -55,8 +55,8 @@ module SpecHelper end <%- end -%> - config.after(:each) do - example_name = self.class.descendant_filtered_examples.first.description + config.after(:each) do |example| + example_name = example.description <%= ERB.new(File.read(File.expand_path('./partials/screenshot.tt', __dir__)), trim_mode: '-').result(binding).strip! %> AllureHelper.add_screenshot example_name <%= ERB.new(File.read(File.expand_path('./partials/quit_driver.tt', __dir__)), trim_mode: '-').result(binding).strip! %> From da1b40970adb8f3058192b172111f4868066f37f Mon Sep 17 00:00:00 2001 From: KasperTonsgaard Date: Sun, 19 Nov 2023 18:31:55 +0100 Subject: [PATCH 2/6] Use Tmpdir to store screenshots and pass file object straight to Allure --- .../templates/helpers/allure_helper.tt | 8 ++++---- .../templates/helpers/partials/screenshot.tt | 8 ++++---- .../templates/helpers/spec_helper.tt | 20 ++++++++++++------- .../templates/helpers/visual_spec_helper.tt | 11 ++++++---- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/lib/generators/templates/helpers/allure_helper.tt b/lib/generators/templates/helpers/allure_helper.tt index 9cea4c81..6d98b52e 100644 --- a/lib/generators/templates/helpers/allure_helper.tt +++ b/lib/generators/templates/helpers/allure_helper.tt @@ -13,10 +13,10 @@ module AllureHelper end end - def add_screenshot(screenshot_name) + def add_screenshot(name, file) Allure.add_attachment( - name: name, - source: File.open("allure-results/screenshots/#{screenshot_name}.png"), + name:, + source: file, type: Allure::ContentType::PNG, test_case: true ) @@ -28,4 +28,4 @@ module AllureHelper end <%- end -%> end -end \ No newline at end of file +end diff --git a/lib/generators/templates/helpers/partials/screenshot.tt b/lib/generators/templates/helpers/partials/screenshot.tt index 38c01edf..bea41a58 100644 --- a/lib/generators/templates/helpers/partials/screenshot.tt +++ b/lib/generators/templates/helpers/partials/screenshot.tt @@ -1,8 +1,8 @@ <% case automation when 'selenium' %> -driver.save_screenshot("allure-results/screenshots/#{example_name}.png") +screenshot = driver.save_screenshot("#{temp_folder}/#{example_name}.png") <% when 'watir' %> -browser.screenshot.save("allure-results/screenshots/#{example_name}.png") +screenshot = browser.screenshot.save("#{temp_folder}/#{example_name}.png") <% else %> -driver.screenshot("allure-results/screenshots/#{example_name}.png") -<% end %> \ No newline at end of file +screenshot = driver.screenshot("#{temp_folder}/#{example_name}.png") +<% end %> diff --git a/lib/generators/templates/helpers/spec_helper.tt b/lib/generators/templates/helpers/spec_helper.tt index e3b9c5b6..5166cb91 100644 --- a/lib/generators/templates/helpers/spec_helper.tt +++ b/lib/generators/templates/helpers/spec_helper.tt @@ -2,6 +2,7 @@ # frozen_string_literal: true require 'rspec' +require 'tmpdir' require_relative 'allure_helper' require_relative 'driver_helper' require 'sparkling_watir' @@ -16,10 +17,12 @@ module SpecHelper app end - config.after(:each) do - example_name = self.class.descendant_filtered_examples.first.description - app.screenshot.save("allure-results/screenshots/#{example_name}.png") - AllureHelper.add_screenshot example_name + config.after(:each) do |example| + example_name = example.description + Dir.mktmpdir do |temp_folder| + app.screenshot.save("#{temp_folder}/#{example_name}.png") + AllureHelper.add_screenshot(example_name, screenshot) + end app.close end end @@ -28,6 +31,7 @@ end # frozen_string_literal: true require 'rspec' +require 'tmpdir' require_relative 'allure_helper' <%- if automation == 'watir' -%> require_relative 'browser_helper' @@ -57,10 +61,12 @@ module SpecHelper config.after(:each) do |example| example_name = example.description - <%= ERB.new(File.read(File.expand_path('./partials/screenshot.tt', __dir__)), trim_mode: '-').result(binding).strip! %> - AllureHelper.add_screenshot example_name + Dir.mktmpdir do |temp_folder| + <%= ERB.new(File.read(File.expand_path('./partials/screenshot.tt', __dir__)), trim_mode: '-').result(binding).strip! %> + AllureHelper.add_screenshot(example_name, screenshot) + end <%= ERB.new(File.read(File.expand_path('./partials/quit_driver.tt', __dir__)), trim_mode: '-').result(binding).strip! %> end end end -<%- end -%> \ No newline at end of file +<%- end -%> diff --git a/lib/generators/templates/helpers/visual_spec_helper.tt b/lib/generators/templates/helpers/visual_spec_helper.tt index 5d177063..cd882f4f 100644 --- a/lib/generators/templates/helpers/visual_spec_helper.tt +++ b/lib/generators/templates/helpers/visual_spec_helper.tt @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'rspec' +require 'tmpdir' require 'eyes_selenium' require_relative 'allure_helper' require_relative 'driver_helper' @@ -18,13 +19,15 @@ module SpecHelper @driver = @eyes.open(driver: driver) end - config.after(:each) do - example_name = self.class.descendant_filtered_examples.first.description - driver.save_screenshot("allure-results/screenshots/#{example_name}.png") + config.after(:each) do |example| + example_name = example.description + Dir.mktmpdir do |temp_folder| + <%= ERB.new(File.read(File.expand_path('./partials/screenshot.tt', __dir__)), trim_mode: '-').result(binding).strip! %> + AllureHelper.add_screenshot(example_name, screenshot) + end @eyes.close @driver.quit @eyes.abort_async - AllureHelper.add_screenshot example_name results = @grid_runner.get_all_test_results puts results end From 2a4b8cdf0b1956118e3264a7610655aa2e022fee Mon Sep 17 00:00:00 2001 From: KasperTonsgaard Date: Sun, 19 Nov 2023 18:47:36 +0100 Subject: [PATCH 3/6] Fix wrong indentation --- lib/generators/templates/helpers/visual_spec_helper.tt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/templates/helpers/visual_spec_helper.tt b/lib/generators/templates/helpers/visual_spec_helper.tt index cd882f4f..360cbf94 100644 --- a/lib/generators/templates/helpers/visual_spec_helper.tt +++ b/lib/generators/templates/helpers/visual_spec_helper.tt @@ -22,7 +22,7 @@ module SpecHelper config.after(:each) do |example| example_name = example.description Dir.mktmpdir do |temp_folder| - <%= ERB.new(File.read(File.expand_path('./partials/screenshot.tt', __dir__)), trim_mode: '-').result(binding).strip! %> + <%= ERB.new(File.read(File.expand_path('./partials/screenshot.tt', __dir__)), trim_mode: '-').result(binding).strip! %> AllureHelper.add_screenshot(example_name, screenshot) end @eyes.close From e9c7f40a86aa6c1cb163d2bfa1c5ce09d4c8b6fd Mon Sep 17 00:00:00 2001 From: aguspe Date: Sun, 19 Nov 2023 21:54:51 +0100 Subject: [PATCH 4/6] Remove screenshot folder --- lib/generators/common_generator.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/generators/common_generator.rb b/lib/generators/common_generator.rb index 2dab16e6..ff6f81d2 100644 --- a/lib/generators/common_generator.rb +++ b/lib/generators/common_generator.rb @@ -36,8 +36,4 @@ def generate_gitignore_file def create_allure_folder empty_directory "#{name}/allure-results" end - - def create_screenshots_folder - empty_directory "#{name}/allure-results/screenshots" - end end From afd6cac5d5d857a5ff0e34b88d341b03a77e9ae0 Mon Sep 17 00:00:00 2001 From: KasperTonsgaard Date: Sun, 19 Nov 2023 22:14:59 +0100 Subject: [PATCH 5/6] Remove creation of screenshot folder in GH Actions --- lib/generators/actions/templates/actions.tt | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/generators/actions/templates/actions.tt b/lib/generators/actions/templates/actions.tt index 2a1892d6..a6b907fa 100644 --- a/lib/generators/actions/templates/actions.tt +++ b/lib/generators/actions/templates/actions.tt @@ -31,9 +31,6 @@ jobs: - name: Create allure-results folder run: mkdir -p allure-results - - name: Create screenshots folder - run: mkdir -p allure-results/screenshots - - name: Build and test with rspec run: <%- if framework == 'cucumber' -%>cucumber features --format pretty <%- else -%>bundle exec rspec spec --format documentation<%- end -%> From d1be78003986ca5b33d83be5a8c6e428f212359e Mon Sep 17 00:00:00 2001 From: KasperTonsgaard Date: Sun, 19 Nov 2023 22:15:37 +0100 Subject: [PATCH 6/6] Use Tmpdir to store screenshots and pass file object straight to Allure --- .../cucumber/templates/partials/appium_env.tt | 16 +++++++++++----- .../cucumber/templates/partials/selenium_env.tt | 16 +++++++++++----- .../cucumber/templates/partials/watir_env.tt | 7 +++++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/generators/cucumber/templates/partials/appium_env.tt b/lib/generators/cucumber/templates/partials/appium_env.tt index 455af9a8..3a02c235 100644 --- a/lib/generators/cucumber/templates/partials/appium_env.tt +++ b/lib/generators/cucumber/templates/partials/appium_env.tt @@ -1,4 +1,5 @@ <%- if automation == 'sparkling_ios' -%> +require 'tmpdir' require_relative '../../helpers/allure_helper' require_relative '../../helpers/driver_helper' @@ -10,11 +11,14 @@ Before do end After do |scenario| - app.screenshot.save("allure-results/screenshots/#{scenario.name}.png") - AllureHelper.add_screenshot(scenario.name) + Dir.mktmpdir do |temp_folder| + screenshot = app.screenshot.save("#{temp_folder}/#{scenario.name}.png") + AllureHelper.add_screenshot(scenario.name, screenshot) + end app.close end <%- else -%> +require 'tmpdir' require_relative '../../helpers/allure_helper' require_relative '../../helpers/driver_helper' @@ -26,8 +30,10 @@ Before do end After do |scenario| - driver.screenshot("allure-results/screenshots/#{scenario.name}.png") - AllureHelper.add_screenshot(scenario.name) + Dir.mktmpdir do |temp_folder| + screenshot = driver.screenshot("#{temp_folder}/#{scenario.name}.png") + AllureHelper.add_screenshot(scenario.name, screenshot) + end driver.quit_driver end -<%- end -%> \ No newline at end of file +<%- end -%> diff --git a/lib/generators/cucumber/templates/partials/selenium_env.tt b/lib/generators/cucumber/templates/partials/selenium_env.tt index 14e667ff..3e63739b 100644 --- a/lib/generators/cucumber/templates/partials/selenium_env.tt +++ b/lib/generators/cucumber/templates/partials/selenium_env.tt @@ -1,6 +1,7 @@ <%- if visual_automation -%> # frozen_string_literal: true +require 'tmpdir' require_relative '../../helpers/allure_helper' require_relative '../../helpers/driver_helper' require_relative '../../helpers/visual_helper' @@ -17,8 +18,10 @@ Before do end After do |scenario| - driver.save_screenshot("allure-results/screenshots/#{scenario.name}.png") - AllureHelper.add_screenshot(scenario.name) + Dir.mktmpdir do |temp_folder| + screenshot = driver.save_screenshot("#{temp_folder}/#{scenario.name}.png") + AllureHelper.add_screenshot(scenario.name, screenshot) + end @eyes.close @driver.quit @eyes.abort_async @@ -29,6 +32,7 @@ end <%- else -%> # frozen_string_literal: true +require 'tmpdir' require_relative '../../helpers/driver_helper' require_relative '../../helpers/allure_helper' @@ -39,8 +43,10 @@ Before do end After do |scenario| - driver.save_screenshot("allure-results/screenshots/#{scenario.name}.png") - AllureHelper.add_screenshot(scenario.name) + Dir.mktmpdir do |temp_folder| + screenshot = driver.save_screenshot("#{temp_folder}/#{scenario.name}.png") + AllureHelper.add_screenshot(scenario.name, screenshot) + end driver.quit end -<%- end -%> \ No newline at end of file +<%- end -%> diff --git a/lib/generators/cucumber/templates/partials/watir_env.tt b/lib/generators/cucumber/templates/partials/watir_env.tt index 431b99b9..5e023911 100644 --- a/lib/generators/cucumber/templates/partials/watir_env.tt +++ b/lib/generators/cucumber/templates/partials/watir_env.tt @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'tmpdir' require_relative '../../helpers/allure_helper' require_relative '../../helpers/browser_helper' @@ -10,7 +11,9 @@ Before do end After do |scenario| - browser.screenshot.save("allure-results/screenshots/#{scenario.name}.png") - AllureHelper.add_screenshot(scenario.name) + Dir.mktmpdir do |temp_folder| + screenshot = browser.screenshot.save("#{temp_folder}/#{scenario.name}.png") + AllureHelper.add_screenshot(scenario.name, screenshot) + end browser.quit end