Skip to content

Commit

Permalink
feat: stop playwright when driver is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
rgonalo committed Jun 10, 2024
1 parent d9b3435 commit c80dc7a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
16 changes: 6 additions & 10 deletions toolium/behave/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ def create_and_configure_wrapper(context):
# Configure wrapper
context.driver_wrapper.configure(context.config_files, behave_properties=behave_properties)

# Activate behave async context to execute playwright
if (context.driver_wrapper.config.get_optional('Driver', 'web_library') == 'playwright'
and context.driver_wrapper.async_loop is None):
use_or_create_async_context(context)
context.driver_wrapper.async_loop = context.async_context.loop

# Copy config object
context.toolium_config = context.driver_wrapper.config

Expand Down Expand Up @@ -227,12 +233,6 @@ def after_scenario(context, scenario):
DriverWrappersPool.close_drivers(scope='function', test_name=scenario.name,
test_passed=scenario.status in ['passed', 'skipped'], context=context)

# Stop playwright
if context.toolium_config.get_optional('Driver', 'web_library') == 'playwright' and hasattr(context, 'playwright'):
# TODO: reuse driver like in close_drivers
loop = context.async_context.loop
loop.run_until_complete(context.playwright.stop())

# Save test status to be updated later
if jira_test_status:
add_jira_status(get_jira_key_from_scenario(scenario), jira_test_status, jira_test_comment)
Expand Down Expand Up @@ -289,10 +289,6 @@ def start_driver(context, no_driver):
:param context: behave context
:param no_driver: True if this is an api test and driver should not be started
"""
if context.toolium_config.get_optional('Driver', 'web_library') == 'playwright':
# Activate behave async context to execute playwright
use_or_create_async_context(context)
context.driver_wrapper.async_loop = context.async_context.loop
create_and_configure_wrapper(context)
if not no_driver:
connect_wrapper(context)
21 changes: 16 additions & 5 deletions toolium/driver_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class DriverWrapper(object):
remote_node_video_enabled = False #: True if the remote grid node has the video recorder enabled
logger = None #: logger instance
async_loop = None #: async loop for playwright tests
playwright = None #: playwright instance
playwright_browser = None #: playwright browser instance

# Configuration and output files
config_properties_filenames = None #: configuration filenames separated by commas
Expand Down Expand Up @@ -204,7 +206,7 @@ def configure(self, tc_config_files, is_selenium_test=True, behave_properties=No
self.configure_visual_baseline()

def connect(self):
"""Set up the selenium driver and connect to the server
"""Set up the driver and connect to the server
:returns: selenium or playwright driver
"""
Expand Down Expand Up @@ -251,14 +253,23 @@ def connect_playwright(self, async_loop):
:returns: playwright page
"""
# TODO: should playwright and browser be saved in driver_wrapper?
playwright = async_loop.run_until_complete(async_playwright().start())
self.playwright = async_loop.run_until_complete(async_playwright().start())
# TODO: select browser from config
headless_mode = self.config.getboolean_optional('Driver', 'headless')
browser = async_loop.run_until_complete(playwright.chromium.launch(headless=headless_mode))
page = async_loop.run_until_complete(browser.new_page())
self.playwright_browser = async_loop.run_until_complete(self.playwright.chromium.launch(headless=headless_mode))
page = async_loop.run_until_complete(self.playwright_browser.new_page())
return page

def stop(self):
"""Stop selenium or playwright driver"""
if self.async_loop:
# Stop playwright driver
self.async_loop.run_until_complete(self.playwright_browser.close())
#self.async_loop.run_until_complete(self.playwright.stop())
else:
# Stop selenium driver
self.driver.quit()

def resize_window(self):
"""Resize and move browser window"""
if self.is_maximizable():
Expand Down
2 changes: 1 addition & 1 deletion toolium/driver_wrappers_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def stop_drivers(cls, maintain_default=False):
if not driver_wrapper.driver:
continue
try:
driver_wrapper.driver.quit()
driver_wrapper.stop()
except Exception as e:
driver_wrapper.logger.warning(
"Capture exceptions to avoid errors in teardown method due to session timeouts: \n %s" % e)
Expand Down

0 comments on commit c80dc7a

Please sign in to comment.