class BaseLoggedInPage(View): """This page should be subclassed by any page that models any other page that is available as logged in. """ CSRF_TOKEN = '//meta[@name="csrf-token"]' flash = FlashMessages( './/div[starts-with(@class, "flash_text_div") or @id="flash_text_div"]' ) help = NavDropdown( './/li[./a[@id="dropdownMenu1"]]|.//li[./a[@id="help-menu"]]') settings = NavDropdown('.//li[./a[@id="dropdownMenu2"]]') navigation = VerticalNavigation('#maintab') @property def is_displayed(self): return self.logged_in_as_current_user def logged_in_as_user(self, user): if self.logged_out: return False return user.name == self.current_fullname @property def logged_in_as_current_user(self): return self.logged_in_as_user(self.extra.appliance.user) @property def current_username(self): try: return self.extra.appliance.user.principal except AttributeError: return None @property def current_fullname(self): return self.settings.text.strip().split('|', 1)[0].strip() @property def logged_in(self): return self.settings.is_displayed @property def logged_out(self): return not self.logged_in def logout(self): self.settings.select_item('Logout') self.browser.handle_alert(wait=None) self.extra.appliance.user = None @property def csrf_token(self): return self.browser.get_attribute('csrf-token', self.CSRF_TOKEN) @csrf_token.setter def csrf_token(self, value): self.browser.set_attribute('csrf-token', value, self.CSRF_TOKEN)
class BaseLoggedInPage(View): """This page should be subclassed by any page that models any other page that is available as logged in. """ flash = FlashMessages('div#flash_text_div') help = NavDropdown('.//li[./a[@id="dropdownMenu1"]]') settings = NavDropdown('.//li[./a[@id="dropdownMenu2"]]') navigation = VerticalNavigation('#maintab') @property def is_displayed(self): return self.logged_in_as_current_user def logged_in_as_user(self, user): if self.logged_out: return False return user.name == self.current_fullname @property def logged_in_as_current_user(self): return self.logged_in_as_user(self.extra.appliance.user) @property def current_username(self): try: return self.extra.appliance.user.principal except AttributeError: return None @property def current_fullname(self): return self.settings.text.strip().split('|', 1)[0].strip() @property def logged_in(self): return self.settings.is_displayed @property def logged_out(self): return not self.logged_in def logout(self): self.settings.select_item('Logout') self.browser.handle_alert(wait=None) self.extra.appliance.user = None
class settings(ParametrizedView): # noqa PARAMETERS = ("user_name",) setting = NavDropdown(ParametrizedLocator('.//li[./a[@title={user_name|quote}]]')) def text(self): return self.setting.text def is_displayed(self): return self.setting.is_displayed def select_item(self, option): return self.setting.select_item(option)
class BaseLoggedInPage(View): """This page should be subclassed by any page that models any other page that is available as logged in. """ CSRF_TOKEN = '//meta[@name="csrf-token"]' flash = FlashMessages('.//div[@id="flash_msg_div"]') help = NavDropdown('.//li[./a[@id="dropdownMenu1"]]|.//li[./a[@id="help-menu"]]') settings = SettingsNavDropdown('.//li[./a[@id="dropdownMenu2"]]') navigation = VerticalNavigation('#maintab') @property def is_displayed(self): return self.logged_in_as_current_user def logged_in_as_user(self, user): if self.logged_out: return False return user.name == self.current_fullname def change_group(self, group_name): """ From the settings menu change to the group specified by 'group_name' Only available in versions >= 5.9 User is required to be currently logged in """ if self.extra.appliance.version < '5.9': raise CFMEException("Changing the user group is not supported in versions < 5.9") if not self.logged_in_as_user: raise CFMEException("Unable to change group when a user is not logged in") if group_name not in self.group_names: raise CFMEException("{} is not an assigned group for {}".format( group_name, self.current_username)) self.settings.groups.select_item(group_name) return True @property def logged_in_as_current_user(self): return self.logged_in_as_user(self.extra.appliance.user) @property def current_username(self): try: return self.extra.appliance.user.principal except AttributeError: return None @property def current_fullname(self): try: # When the view isn't displayed self.settings.text is None, resulting in AttributeError return self.settings.text.strip().split('|', 1)[0].strip() except AttributeError: return None @property def current_groupname(self): current_groups = self.settings.groups.items # User is only assigned to one group if len(current_groups) == 1: return current_groups[0] for group in current_groups: if self.settings.groups.SELECTED_GROUP_MARKER in group: return group.replace(self.settings.groups.SELECTED_GROUP_MARKER, '') else: # Handle some weird case where we don't detect a current group raise CFMEException("User is not currently assigned to a group") @property def group_names(self): """ Return a list of the logged in user's assigned groups. Returns: Version >= 5.9 - list containing all groups the logged in user is assigned to Version < 5.9 - single item list containing the user's current group """ return [ group.replace(self.settings.groups.SELECTED_GROUP_MARKER, '') for group in self.settings.groups.items] @property def logged_in(self): return self.settings.is_displayed @property def logged_out(self): return not self.logged_in def logout(self): self.settings.select_item('Logout') self.browser.handle_alert(wait=None) self.extra.appliance.user = None @property def csrf_token(self): return self.browser.get_attribute('content', self.CSRF_TOKEN) @csrf_token.setter def csrf_token(self, value): self.browser.set_attribute('content', value, self.CSRF_TOKEN) @property def unexpected_error(self): if not self.browser.elements('//h1[contains(., "Unexpected error encountered")]'): return None try: err_el = self.browser.element('//h2[contains(., "Error text:")]/following-sibling::h3') return self.browser.text(err_el) except NoSuchElementException: return None
class BaseLoggedInPage(View): """This page should be subclassed by any page that models any other page that is available as logged in. """ CSRF_TOKEN = '//meta[@name="csrf-token"]' flash = FlashMessages( './/div[@id="flash_msg_div"]/div[@id="flash_text_div" or ' 'contains(@class, "flash_text_div")] | ' './/div[starts-with(@class, "flash_text_div") or @id="flash_text_div"]' ) help = NavDropdown( './/li[./a[@id="dropdownMenu1"]]|.//li[./a[@id="help-menu"]]') settings = NavDropdown('.//li[./a[@id="dropdownMenu2"]]') navigation = VerticalNavigation('#maintab') @property def is_displayed(self): return self.logged_in_as_current_user def logged_in_as_user(self, user): if self.logged_out: return False return user.name == self.current_fullname @property def logged_in_as_current_user(self): return self.logged_in_as_user(self.extra.appliance.user) @property def current_username(self): try: return self.extra.appliance.user.principal except AttributeError: return None @property def current_fullname(self): return self.settings.text.strip().split('|', 1)[0].strip() @property def current_groupname(self): return self.settings.items[1].strip() @property def logged_in(self): return self.settings.is_displayed @property def logged_out(self): return not self.logged_in def logout(self): self.settings.select_item('Logout') self.browser.handle_alert(wait=None) self.extra.appliance.user = None @property def csrf_token(self): return self.browser.get_attribute('content', self.CSRF_TOKEN) @csrf_token.setter def csrf_token(self, value): self.browser.set_attribute('content', value, self.CSRF_TOKEN) @property def unexpected_error(self): if not self.browser.elements( '//h1[contains(., "Unexpected error encountered")]'): return None try: err_el = self.browser.element( '//h2[contains(., "Error text:")]/following-sibling::h3') return self.browser.text(err_el) except NoSuchElementException: return None
class DashboardView(View): """Dashboard view.""" user_dropdown = NavDropdown(locator=Locator(css="li.dropdown:nth-child(2)")) logout = Button("Log out") nav = VerticalNavigation(locator=(Locator(css=".list-group")))
class SSUIBaseLoggedInPage(View): """This page should be subclassed by any page that models any other page that is available as logged in. """ flash = FlashMessages('div#flash_text_div') help = NavDropdown('.//li[./a[@id="dropdownMenu1"]]') navigation = SSUIVerticalNavigation('//ul[@class="list-group"]') domain_switcher = Button(id="domain-switcher") shopping_cart = Text('.//li/a[@title="Shopping cart"]') @ParametrizedView.nested class settings(ParametrizedView): # noqa PARAMETERS = ("user_name",) setting = NavDropdown(ParametrizedLocator('.//li[./a[@title={user_name|quote}]]')) def text(self): return self.setting.text def is_displayed(self): return self.setting.is_displayed def select_item(self, option): return self.setting.select_item(option) @property def is_displayed(self): return self.logged_in_as_current_user def logged_in_as_user(self, user): if self.logged_out: return False return user.name == self.current_fullname @property def logged_in_as_current_user(self): return self.logged_in_as_user(self.extra.appliance.user) @property def current_username(self): try: return self.extra.appliance.user.principal except AttributeError: return None @property def current_fullname(self): return self.settings(self.extra.appliance.user.credential.principal).text() @property def logged_in(self): return ( self.settings(self.extra.appliance.user.credential.principal).is_displayed() and self.shopping_cart.is_displayed) @property def logged_out(self): return not self.logged_in def logout(self): self.settings(self.extra.appliance.user.credential.principal).select_item('Logout') self.browser.handle_alert(wait=None) self.extra.appliance.user = None
class BaseLoggedInPage(View): """This page should be subclassed by any page that models any other page that is available as logged in. """ CSRF_TOKEN = '//meta[@name="csrf-token"]' flash = FlashMessages( './/div[@id="flash_msg_div"]/div[@id="flash_text_div" or ' 'contains(@class, "flash_text_div")] | ' './/div[starts-with(@class, "flash_text_div") or @id="flash_text_div"]' ) help = NavDropdown( './/li[./a[@id="dropdownMenu1"]]|.//li[./a[@id="help-menu"]]') settings = NavDropdown('.//li[./a[@id="dropdownMenu2"]]') # 5.9 Locator for Settings item that replaces current group name when user has multiple groups group_list_locator = ( './/ul/li[contains(@class, "dropdown-submenu") and contains(., "Change Group")]' ) navigation = VerticalNavigation('#maintab') @property def is_displayed(self): return self.logged_in_as_current_user def logged_in_as_user(self, user): if self.logged_out: return False return user.name == self.current_fullname @property def logged_in_as_current_user(self): return self.logged_in_as_user(self.extra.appliance.user) @property def current_username(self): try: return self.extra.appliance.user.principal except AttributeError: return None @property def current_fullname(self): try: # When the view isn't displayed self.settings.text is None, resulting in AttributeError return self.settings.text.strip().split('|', 1)[0].strip() except AttributeError: return None @property def current_groupname(self): # TODO: Move these locators and accessors to a widget "Group Dropdown" widget # 5.9 Locators for finding current group when user has multiple groups current_group_locator = '{}/ul/li/a[@title="Currently Selected Group"]'.format( self.group_list_locator) current_group_marker = ' (Current Group)' try: current_group = self.browser.element(current_group_locator) return self.browser.text(current_group).replace( current_group_marker, '') except NoSuchElementException: return self.settings.items[1].strip() @property def group_names(self): """ Return a list of the logged in user's assigned groups. Returns: Version >= 5.9 - list of all groups the logged in user is assigned to Version < 5.9 - single item list containing the user's current group """ # TODO: Move these locators and accessors to a widget "Group Dropdown" widget group_list_locator = '{}/ul/li'.format(self.group_list_locator) current_group_marker = ' (Current Group)' group_list = self.browser.elements(group_list_locator) if group_list: return [ self.browser.text(group).replace(current_group_marker, '') for group in group_list ] else: return [self.current_groupname] @property def logged_in(self): return self.settings.is_displayed @property def logged_out(self): return not self.logged_in def logout(self): self.settings.select_item('Logout') self.browser.handle_alert(wait=None) self.extra.appliance.user = None @property def csrf_token(self): return self.browser.get_attribute('content', self.CSRF_TOKEN) @csrf_token.setter def csrf_token(self, value): self.browser.set_attribute('content', value, self.CSRF_TOKEN) @property def unexpected_error(self): if not self.browser.elements( '//h1[contains(., "Unexpected error encountered")]'): return None try: err_el = self.browser.element( '//h2[contains(., "Error text:")]/following-sibling::h3') return self.browser.text(err_el) except NoSuchElementException: return None