import os from time import sleep from appium.webdriver.common.appiumby import AppiumBy as By from selenium.common.exceptions import NoSuchElementException, \ StaleElementReferenceException class HeaderInfoMixin: os_env = os.getenv('OS').lower() if not os_env: raise EnvironmentError( "OS environment variable is not set. " "Please define 'OS' with 'android' or 'ios'.") locator_template = { "android": "(//android.widget.TextView[@text='{key}']/" "../..//android.widget.TextView)[2]", "ios": "//XCUIElementTypeOther[@label='{key}']/" "following-sibling::XCUIElementTypeOther[1]" } header_info_fields = { "Appears on": None, "Artist": None, "ISRC": None, "UPC": None, "Release Date": None, "Label": None, "Imprint": None, "Brand": None, "Parent Co.": None, } def header_info(self): header_info = {key: None for key in self.header_info_fields} for key in header_info.keys(): try: element_xpath = ( self.locator_template[self.os_env].format(key=key)) header_info[key] = self.driver.find_element( By.XPATH, element_xpath).text except NoSuchElementException: pass return header_info def get_all_header_fields(self): header_info = {key: None for key in self.header_info_fields} for key in header_info.keys(): try: element_xpath = ( self.locator_template[self.os_env].format(key=key)) header_info[key] = ( self.driver. find_element(By.XPATH, element_xpath).text.strip()) except NoSuchElementException: continue return header_info def scroll_header(self, direction, hard_wait=3): try: header_element = self.waiter(20, By.XPATH, self.header) except StaleElementReferenceException: sleep(1) header_element = self.waiter(20, By.XPATH, self.header) rect = header_element.rect y_position = rect['y'] + (rect['height'] / 2) if direction == 'left': x_start = rect['x'] + (rect['width'] * 0.8) x_end = rect['x'] + (rect['width'] * 0.2) else: x_start = rect['x'] + (rect['width'] * 0.2) x_end = rect['x'] + (rect['width'] * 0.8) self.driver.swipe(x_start, y_position, x_end, y_position, 400) sleep(hard_wait)