def clean_up_webdrivers(self): ''' Clean up webdrivers created during execution. ''' # Quit webdrivers. print_debug("WebdriverManager : Cleaning up webdrivers") try: if self.__use_shutdown_hook: for key in self.__registered_drivers.keys(): for driver in self.__registered_drivers[key]: try: print_debug("Closing webdriver for thread: ", key) driver.quit() except: pass except: pass
def create_page(page_object_class_or_interface, \ webdriver=None, **kwargs): """ Instantiate a page object from a given Interface or Abstract class. Instantiating a Page from PageObject from class usage: my_page_instance = PageFactory.create_page(MyPageClass) Instantiating a Page from an Interface or base class import pages.mysite.* my_page_instance = PageFactory.create_page(MyPageInterfaceClass) Instantiating a Page from a list of classes. my_page_instance = PageFactory.create_page([PossiblePage1, PossiblePage2]) Note: It'll only be able to detect pages that are imported. To it's best to do an import of all pages implementing a base class or the interface inside the __init__.py of the package directory. @param page_object_class_or_interface: Class, AbstractBaseClass, or Interface to attempt to consturct. @param webdriver: Selenium Webdriver to use to instantiate the page. @type webdriver: WebDriver """ if not webdriver: webdriver = WTF_WEBDRIVER_MANAGER.get_driver() # will be used later when tracking best matched page. current_matched_page = None # Walk through all classes of this sub class if type(page_object_class_or_interface) == list: subclasses = [] for page_class in page_object_class_or_interface: #attempt to instantiate class. page = PageFactory.__instantiate_page_object(page_class, \ webdriver, \ **kwargs) if isinstance(page, PageObject) and (current_matched_page == None or page > current_matched_page): current_matched_page = page #check for subclasses subclasses += PageFactory.__itersubclasses(page_class) else: # Try the original class page_class = page_object_class_or_interface page = PageFactory.__instantiate_page_object(page_class, webdriver, **kwargs) if isinstance(page, PageObject): current_matched_page = page #check for subclasses subclasses = PageFactory.__itersubclasses(page_object_class_or_interface) # Iterate over subclasses of the passed in classes to see if we have a better match. for pageClass in subclasses: try: page = pageClass(webdriver, **kwargs) if current_matched_page == None or page > current_matched_page: current_matched_page = page except InvalidPageError as e: print_debug("InvalidPageError", e) pass #This happens when the page fails check. except TypeError as e: print_debug("TypeError", e) pass #this happens when it tries to instantiate the original abstract class. except Exception as e: print_debug("Exception", e) #Unexpected exception. raise e # If no matching classes. if not isinstance(current_matched_page, PageObject): raise NoMatchingPageError("There's, no matching classes to this page. URL:%s" \ % webdriver.current_url) else: return current_matched_page
def create_page(page_object_class_or_interface, \ webdriver=None, **kwargs): """ Instantiate a page object from a given Interface or Abstract class. Args: page_object_class_or_interface (Class): PageObject class, AbstractBaseClass, or Interface to attempt to consturct. Kwargs: webdriver (WebDriver): Selenium Webdriver to use to instantiate the page. Returns: PageObject Raises: NoMatchingPageError Instantiating a Page from PageObject from class usage:: my_page_instance = PageFactory.create_page(MyPageClass) Instantiating a Page from an Interface or base class:: import pages.mysite.* # Make sure you import classes first, or else PageFactory will not know about it. my_page_instance = PageFactory.create_page(MyPageInterfaceClass) Instantiating a Page from a list of classes.:: my_page_instance = PageFactory.create_page([PossiblePage1, PossiblePage2]) Note: It'll only be able to detect pages that are imported. To it's best to do an import of all pages implementing a base class or the interface inside the __init__.py of the package directory. """ if not webdriver: webdriver = WTF_WEBDRIVER_MANAGER.get_driver() # will be used later when tracking best matched page. current_matched_page = None # Walk through all classes of this sub class if type(page_object_class_or_interface) == list: subclasses = [] for page_class in page_object_class_or_interface: #attempt to instantiate class. page = PageFactory.__instantiate_page_object(page_class, \ webdriver, \ **kwargs) if isinstance(page, PageObject) and (current_matched_page == None or page > current_matched_page): current_matched_page = page #check for subclasses subclasses += PageFactory.__itersubclasses(page_class) else: # Try the original class page_class = page_object_class_or_interface page = PageFactory.__instantiate_page_object(page_class, \ webdriver, \ **kwargs) if isinstance(page, PageObject): current_matched_page = page #check for subclasses subclasses = PageFactory.__itersubclasses( page_object_class_or_interface) # Iterate over subclasses of the passed in classes to see if we have a better match. for pageClass in subclasses: try: page = pageClass(webdriver, **kwargs) if current_matched_page == None or page > current_matched_page: current_matched_page = page except InvalidPageError as e: print_debug("InvalidPageError", e) pass #This happens when the page fails check. except TypeError as e: print_debug("TypeError", e) pass #this happens when it tries to instantiate the original abstract class. except Exception as e: print_debug("Exception", e) #Unexpected exception. raise e # If no matching classes. if not isinstance(current_matched_page, PageObject): raise NoMatchingPageError("There's, no matching classes to this page. URL:%s" \ % webdriver.current_url) else: return current_matched_page