Skip to content

Commit

Permalink
refactored switch to new context
Browse files Browse the repository at this point in the history
  • Loading branch information
hesteban-tuenti committed Nov 17, 2023
1 parent b74b631 commit c577338
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions toolium/pageobjects/common_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def utils(self):
"""
return self.driver_wrapper.utils

def _switch_to_new_context(self, context):
""" Change to a new context if its different than the current one"""
if self.driver.context != context:
self.driver.switch_to.context(context)

def _android_automatic_context_selection(self):
"""Change context selection depending if the element is a webview for android devices"""
# we choose the appPackage webview context, and select the first window returned by mobile: getContexts
Expand All @@ -80,22 +85,19 @@ def _android_automatic_context_selection(self):
self.driver.capabilities['appPackage'])
if app_web_context in self.driver.contexts:
context = app_web_context
if self.driver.context != context:
self.driver.switch_to.context(context)
self._switch_to_new_context(context)
window_handle = self.driver.window_handles[0]
else:
raise KeyError("WEBVIEW context not found")

if context:
if self.driver.context != context:
self.driver.switch_to.context(context)
self._switch_to_new_context(context)
if self.driver.current_window_handle != window_handle:
self.driver.switch_to.window(window_handle)
else:
raise KeyError("WEBVIEW context not found")
else:
if self.driver.context != CommonObject.native_context:
self.driver.switch_to.context(CommonObject.native_context)
self._switch_to_new_context(CommonObject.native_context)

def _ios_automatic_context_selection(self):
"""Change context selection depending if the element is a webview for ios devices"""
Expand All @@ -115,5 +117,4 @@ def _ios_automatic_context_selection(self):
else:
raise KeyError("WEBVIEW context not found")
else:
if self.driver.context != CommonObject.native_context:
self.driver.switch_to.context(CommonObject.native_context)
self._switch_to_new_context(CommonObject.native_context)

0 comments on commit c577338

Please sign in to comment.