Skip to content

Commit

Permalink
Merge pull request #708 from bugsnag/tms/skip-after-appium-failure
Browse files Browse the repository at this point in the history
Skip remaining scenarios if the Appium session fails
  • Loading branch information
twometresteve authored Dec 18, 2024
2 parents a517018 + c5ad13e commit 256093b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 9.22.0 - 2024/12/xx

## Enhancements

- Skip remaining tests after Appium session failure [708](https://github.com/bugsnag/maze-runner/pull/708)

# 9.21.0 - 2024/11/28

## Enhancements
Expand Down
7 changes: 6 additions & 1 deletion lib/features/support/internal_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@
Before do |scenario|
Maze.scenario = Maze::Api::Cucumber::Scenario.new(scenario)

# Default to no dynamic try
# Skip scenario if the driver it needs has failed
if (Maze.mode == :appium || Maze.mode == :browser) && Maze.driver.failed?
skip_this_scenario
end

# Default to no dynamic retry
Maze.dynamic_retry = false

if ENV['BUILDKITE']
Expand Down
2 changes: 1 addition & 1 deletion lib/maze.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# providing an alternative to the proliferation of global variables or singletons.
module Maze

VERSION = '9.21.0'
VERSION = '9.22.0'

class << self
attr_accessor :check, :driver, :internal_hooks, :mode, :start_time, :dynamic_retry, :public_address,
Expand Down
2 changes: 1 addition & 1 deletion lib/maze/client/appium/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def log_run_outro
end

def stop_session
Maze.driver&.driver_quit
Maze.driver.driver_quit unless Maze.driver.failed?
Maze::AppiumServer.stop if Maze::AppiumServer.running
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/maze/client/selenium/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def log_run_outro
end

def stop_session
Maze.driver&.driver_quit
Maze.driver.driver_quit unless Maze.driver.failed?
end
end
end
Expand Down
32 changes: 22 additions & 10 deletions lib/maze/driver/appium.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Appium < Appium::Driver
attr_reader :device_type

# @!attribute [r] capabilities
# @return [Hash] The capabilities used to launch the BrowserStack instance
# @return [Hash] The capabilities used to launch the Appium session
attr_reader :capabilities

# Creates the Appium driver
Expand All @@ -32,6 +32,7 @@ def initialize(server_url, capabilities, locator = :id)
# Sets up identifiers for ease of connecting jobs
capabilities ||= {}

@failed = false
@element_locator = locator
@capabilities = capabilities

Expand Down Expand Up @@ -64,6 +65,17 @@ def start_driver
end
end

# Whether the driver has known to have failed (it may still have failed and us not know yet)
def failed?
@failed
end

# Marks the driver as failed
def fail_driver
$logger.error 'Appium driver failed, remaining scenarios will be skipped'
@failed = true
end

# Checks for an element, waiting until it is present or the method times out
#
# @param element_id [String] the element to search for
Expand All @@ -83,7 +95,7 @@ def wait_for_element(element_id, timeout = 15, retry_if_stale = true)
end
rescue Selenium::WebDriver::Error::ServerError => e
# Assume the remote appium session has stopped, so crash out of the session
Maze.driver = nil
fail_driver
raise e
else
true
Expand All @@ -94,7 +106,7 @@ def launch_app
super
rescue Selenium::WebDriver::Error::ServerError => e
# Assume the remote appium session has stopped, so crash out of the session
Maze.driver = nil
fail_driver
raise e
end

Expand All @@ -103,7 +115,7 @@ def close_app
super
rescue Selenium::WebDriver::Error::ServerError => e
# Assume the remote appium session has stopped, so crash out of the session
Maze.driver = nil
fail_driver
raise e
end

Expand All @@ -114,7 +126,7 @@ def find_element_timed(element_id)
end
rescue Selenium::WebDriver::Error::ServerError => e
# Assume the remote appium session has stopped, so crash out of the session
Maze.driver = nil
fail_driver
raise e
end

Expand All @@ -128,7 +140,7 @@ def click_element(element_id)
end
rescue Selenium::WebDriver::Error::ServerError => e
# Assume the remote appium session has stopped, so crash out of the session
Maze.driver = nil
fail_driver
raise e
end

Expand All @@ -146,7 +158,7 @@ def click_element_if_present(element_id)
false
rescue Selenium::WebDriver::Error::ServerError => e
# Assume the remote appium session has stopped, so crash out of the session
Maze.driver = nil
fail_driver
raise e
end

Expand All @@ -160,7 +172,7 @@ def clear_element(element_id)
end
rescue Selenium::WebDriver::Error::ServerError => e
# Assume the remote appium session has stopped, so crash out of the session
Maze.driver = nil
fail_driver
raise e
end

Expand All @@ -185,7 +197,7 @@ def send_keys_to_element(element_id, text)
end
rescue Selenium::WebDriver::Error::ServerError => e
# Assume the remote appium session has stopped, so crash out of the session
Maze.driver = nil
fail_driver
raise e
end

Expand Down Expand Up @@ -222,7 +234,7 @@ def clear_and_send_keys_to_element(element_id, text)
end
rescue Selenium::WebDriver::Error::ServerError => e
# Assume the remote appium session has stopped, so crash out of the session
Maze.driver = nil
fail_driver
raise e
end

Expand Down
13 changes: 13 additions & 0 deletions lib/maze/driver/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,23 @@ class Browser

def initialize(driver_for, selenium_url=nil, capabilities=nil)
capabilities ||= {}
@failed = false
@capabilities = capabilities
@driver_for = driver_for
@selenium_url = selenium_url
end

# Whether the driver has known to have failed (it may still have failed and us not know yet)
def failed?
@failed
end

# Marks the driver as failed
def fail_driver
$logger.error 'Selenium driver failed, remaining scenarios will be skipped'
@failed = true
end

def find_element(*args)
@driver.find_element(*args)
end
Expand Down Expand Up @@ -128,6 +140,7 @@ def create_driver(driver_for, selenium_url=nil)
end
$logger.info "Selenium driver started in #{(Time.now - time).to_i}s"
@driver = driver
@failed = false
rescue => error
Bugsnag.notify error
$logger.warn "Selenium driver failed to start in #{(Time.now - time).to_i}s"
Expand Down
4 changes: 2 additions & 2 deletions lib/maze/hooks/appium_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ def after(scenario)
# Reset the server to ensure that test fixtures cannot fetch
# commands from the previous scenario (in idempotent mode).
begin
Maze.driver.terminate_app Maze.driver&.app_id
Maze.driver.terminate_app Maze.driver.app_id
rescue Selenium::WebDriver::Error::UnknownError, Selenium::WebDriver::Error::InvalidSessionIdError
if Maze.config.appium_version && Maze.config.appium_version.to_f < 2.0
$logger.warn 'terminate_app failed, using the slower but more forceful close_app instead'
Maze.driver&.close_app
Maze.driver.close_app
else
$logger.warn 'terminate_app failed, future errors may occur if the application did not close remotely'
end
Expand Down

0 comments on commit 256093b

Please sign in to comment.