def get_menu(self, label): """Get a :class:`MenuElement` instance corresponding to the specified label. :param label: The label of the menu, e.g., **File** or **View**. :returns: A :class:`MenuElement` instance. """ menu = [m for m in self.menus if m.get_attribute('label') == label] if not menu: raise NoSuchElementException('Could not find a menu with ' 'label "{}"'.format(label)) return menu[0]
def select(self, label): """Click on a menu item within this menu. :param label: The label of the menu item, e.g., **New Tab**. """ item = [l for l in self.items if l.get_attribute('label') == label] if not item: message = ("Item labeled '{}' not found in the '{}' menu" .format(label, self.get_attribute('label'))) raise NoSuchElementException(message) return item[0].click()
def get_menu_by_id(self, menu_id): """Get a :class:`MenuElement` instance corresponding to the specified ID. :param menu_id: The ID of the menu, e.g., **file-menu** or **view-menu**. :returns: A :class:`MenuElement` instance. """ menu = [m for m in self.menus if m.get_attribute('id') == menu_id] if not menu: raise NoSuchElementException('Could not find a menu with ' 'id "{}"'.format(menu_id)) return menu[0]
def select_by_id(self, menu_item_id): """Click on a menu item within this menu. :param menu_item_id: The ID of the menu item, e.g. **menu_newNavigatorTab**. """ item = [l for l in self.items if l.get_attribute('id') == menu_item_id] if not item: message = ("Item with ID '{}' not found in the '{}' menu" .format(menu_item_id, self.get_attribute('id'))) raise NoSuchElementException(message) return item[0].click()
def click(self, target=None): """ Overrides HTMLElement.click to provide a target to click. :param target: The label associated with the button to click on, e.g., `New Private Window`. """ if not target: return DOMElement.click(self) for button in self.buttons: if button.get_attribute('label') == target: return button.click() raise NoSuchElementException('Could not find "{}"" in the ' 'menu panel UI'.format(target))
def localize_entity(self, dtd_urls, entity_id): """Returns the localized string for the specified DTD entity id. To find the entity all given DTD files will be searched for the id. :param dtd_urls: A list of dtd files to search. :param entity_id: The id to retrieve the value from. :returns: The localized string for the requested entity. :raises NoSuchElementException: When entity id is not found in dtd_urls. """ # Add xhtml11.dtd to prevent missing entity errors with XHTML files try: return self._l10nMarionette.localize_entity(dtd_urls, entity_id) except UnknownCommandException: dtds = copy.copy(dtd_urls) dtds.append("resource:///res/dtd/xhtml11.dtd") dtd_refs = '' for index, item in enumerate(dtds): dtd_id = 'dtd_%s' % index dtd_refs += '<!ENTITY %% %s SYSTEM "%s">%%%s;' % \ (dtd_id, item, dtd_id) contents = """<?xml version="1.0"?> <!DOCTYPE elem [%s]> <elem id="entity">&%s;</elem>""" % (dtd_refs, entity_id) with self.marionette.using_context('chrome'): value = self.marionette.execute_script(""" Cu.importGlobalProperties(["DOMParser"]); var parser = new DOMParser(); parser.forceEnableDTD(); var doc = parser.parseFromString(arguments[0], "text/xml"); var node = doc.querySelector("elem[id='entity']"); return node ? node.textContent : null; """, script_args=[contents]) if not value: raise NoSuchElementException('DTD Entity not found: %s' % entity_id) return value
def localize_property(self, property_urls, property_id): """Returns the localized string for the specified property id. To find the property all given property files will be searched for the id. :param property_urls: A list of property files to search. :param property_id: The id to retrieve the value from. :returns: The localized string for the requested entity. :raises NoSuchElementException: When property id is not found in property_urls. """ try: return self._l10nMarionette.localize_property( property_urls, property_id) except UnknownCommandException: with self.marionette.using_context('chrome'): value = self.marionette.execute_script( """ let property = null; let property_id = arguments[1]; arguments[0].some(aUrl => { let bundle = Services.strings.createBundle(aUrl); try { property = bundle.GetStringFromName(property_id); return true; } catch (ex) { } }); return property; """, script_args=[property_urls, property_id]) if not value: raise NoSuchElementException('Property not found: %s' % property_id) return value