Skip to content

Commit

Permalink
Initialize driver in before_feature when reuse_driver is true
Browse files Browse the repository at this point in the history
  • Loading branch information
rgonalo committed May 2, 2017
1 parent b4b2848 commit 6b845fa
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 25 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.rst
Expand Up @@ -8,8 +8,10 @@ v1.2.6

- Fix visual screenshot filename error when behave feature name contains :
- Add a config property 'explicitly_wait' in [Driver] section to set the default timeout used in *wait_until* methods
- Drivers will be closed after each behave feature, even if reuse_driver is true
- Add @reset_driver tag to restart driver before the scenario, even if reuse_driver is true
- When reuse_driver is true using behave, driver is initialized in *before_feature* method and closed in *after_feature*
method
- Add @reuse_driver feature tag to reuse driver in a behave feature, even if reuse_driver is false
- Add @reset_driver scenario tag to restart driver before a behave scenario, even if reuse_driver is true

v1.2.5
------
Expand Down
34 changes: 13 additions & 21 deletions toolium/behave/environment.py
Expand Up @@ -67,6 +67,10 @@ def before_feature(context, feature):
"""
context.global_status = {'test_passed': True}

# Start driver if it should be reused in feature
if context.toolium_config.getboolean_optional('Driver', 'reuse_driver') or 'reuse_driver' in feature.tags:
start_driver(context)


def before_scenario(context, scenario):
"""Scenario initialization
Expand All @@ -85,8 +89,12 @@ def before_scenario(context, scenario):
os.environ["AppiumCapabilities_noReset"] = 'false'
os.environ["AppiumCapabilities_fullReset"] = 'true'

# If driver is already started or reuse_driver is configured, then driver should be reused
context.reuse_driver = (context.toolium_config.getboolean_optional('Driver', 'reuse_driver')
or ('driver' in context and context.driver is not None))

# Force to reset driver before each scenario if it has @reset_driver tag
if 'reset_driver' in scenario.tags and context.toolium_config.getboolean_optional('Driver', 'reuse_driver'):
if 'reset_driver' in scenario.tags and context.reuse_driver:
stop_reused_driver()

# Skip android_only or ios_only scenarios
Expand All @@ -107,19 +115,14 @@ def bdd_common_before_scenario(context_or_world, scenario):
:param scenario: running scenario
"""
# Initialize and connect driver wrapper
create_and_configure_wrapper(context_or_world)
connect_wrapper(context_or_world)
start_driver(context_or_world)

# Add assert screenshot methods with scenario configuration
add_assert_screenshot_methods(context_or_world, scenario)

# Configure Jira properties
save_jira_conf()

# Set implicitly wait timeout in web and mobile tests
if context_or_world.driver:
context_or_world.utils.set_implicitly_wait()

context_or_world.logger.info("Running new scenario: %s", scenario.name)


Expand Down Expand Up @@ -226,17 +229,16 @@ def bdd_common_after_scenario(context_or_world, scenario, status):
DriverWrappersPool.save_all_webdriver_logs(scenario.name, test_passed)

# Close browser and stop driver if it must not be reused
reuse_driver = context_or_world.toolium_config.getboolean_optional('Driver', 'reuse_driver')
restart_driver_fail = context_or_world.toolium_config.getboolean_optional('Driver', 'restart_driver_fail')
maintain_default = reuse_driver and (test_passed or not restart_driver_fail)
maintain_default = context_or_world.reuse_driver and (test_passed or not restart_driver_fail)
DriverWrappersPool.close_drivers_and_download_videos(scenario_file_name, test_passed, maintain_default)

# Start driver if it has been closed due to a failed test
if reuse_driver and not test_passed and restart_driver_fail:
if context_or_world.reuse_driver and not test_passed and restart_driver_fail:
start_driver(context_or_world)

# Save test status to be updated later
previous_status = context_or_world.global_status['test_passed'] if reuse_driver else True
previous_status = context_or_world.global_status['test_passed'] if context_or_world.reuse_driver else True
context_or_world.global_status['test_passed'] = previous_status and test_passed
add_jira_status(get_jira_key_from_scenario(scenario), test_status, test_comment)

Expand Down Expand Up @@ -305,13 +307,3 @@ def start_driver(context):
"""
create_and_configure_wrapper(context)
connect_wrapper(context)


def restart_reused_driver(context, video_name='multiple_tests'):
"""Restart driver if it has been reused
:param context: behave context
:param video_name: downloaded video name
"""
stop_reused_driver(context, video_name)
start_driver(context)
3 changes: 3 additions & 0 deletions toolium/driver_wrapper.py
Expand Up @@ -267,6 +267,9 @@ def connect(self, maximize=True):
# Discard previous logcat logs
self.utils.discard_logcat_logs()

# Set implicitly wait timeout
self.utils.set_implicitly_wait()

return self.driver

def is_android_test(self):
Expand Down
1 change: 1 addition & 0 deletions toolium/lettuce/terrain.py
Expand Up @@ -37,6 +37,7 @@ def setup_driver(scenario):

world.global_status = {'test_passed': True}
bdd_common_before_scenario(world, scenario)
world.reuse_driver = world.toolium_config.getboolean_optional('Driver', 'reuse_driver')


def teardown_driver(scenario):
Expand Down
2 changes: 0 additions & 2 deletions toolium/test_cases.py
Expand Up @@ -135,8 +135,6 @@ def assert_screenshot_page_element(self, filename, threshold=0, exclude_elements

# Get common configuration of reusing driver
self.reuse_driver = self.driver_wrapper.config.getboolean_optional('Driver', 'reuse_driver')
# Set implicitly wait timeout
self.utils.set_implicitly_wait()
# Call BasicTestCase setUp
super(SeleniumTestCase, self).setUp()

Expand Down

0 comments on commit 6b845fa

Please sign in to comment.