import re import time import random from appium.webdriver.common.appiumby import AppiumBy as By from selenium.common.exceptions import TimeoutException from helpers.platform.ios.locators import ios_name_contains, ios_other_by_name from page_objects.base_screen import BaseScreen from page_objects.ios_pom.home_screen import HomeScreen class ProfileScreen(BaseScreen): profile_detail = 'profileDetail' settings_title = 'SETTINGS' default_language_btn = 'Language English' notifications_btn = 'Notifications' about_title = 'ABOUT' privacy_policy_btn = 'privacyPolicyLink' terms_and_conditions_btn = 'termsAndConditionsLink' version = ios_other_by_name('version') url_bar = 'TabBarItemTitle' existing_languages = { "German": "Deutsch , (German)", "English": "English", "Spanish": "Español , (Spanish)", "French": "Français , (French)", "Italian": "Italiano , (Italian)", "Portuguese": "Português , (Portuguese)", "Russian": "Pусский , (Russian)", "Turkish": "Türkçe , (Turkish)", "Japanese": "日本語 , (Japanese)", "Simplified Chinese": "简体中文 , (Simplified Chinese)", "Traditional Chinese": "繁體中文 , (Traditional Chinese)", "Korean": "한국어 , (Korean)" } def verify_expected_labels_and_buttons_present(self): elements_to_check = [ self.settings_title, self.default_language_btn, self.notifications_btn, self.about_title, self.privacy_policy_btn, self.terms_and_conditions_btn ] for id_ in elements_to_check: self.waiter(10, By.ACCESSIBILITY_ID, id_) assert (self.waiter(1, By.ACCESSIBILITY_ID, self.profile_detail). text.startswith('qa_insights_e2etest+')) # TODO: # The check will be temporarily removed until there is a clear # understanding of how the version display will look in the # master branch of the application. # version = (self.waiter(1, By.IOS_CLASS_CHAIN, self.version). # get_attribute('label')) # assert self.check_version_format(version) @staticmethod def _ensure_current_language(context): current = getattr(context, "current_language", None) or 'English' context.current_language = current return current def _open_language_picker(self, context): current_lang = self._ensure_current_language(context) if current_lang == 'English': self.click_with_retry( 10, By.ACCESSIBILITY_ID, self.default_language_btn) else: language_entry = self.existing_languages[current_lang] current_lang_loc = language_entry.split(",")[0].strip() full_loc = ios_name_contains(current_lang_loc) self.waiter(10, By.IOS_CLASS_CHAIN, full_loc, multiple=True)[-1].click() def select_random_language_different_from_current(self, context): self._open_language_picker(context) choices = [(lang, loc) for lang, loc in self.existing_languages.items() if lang != context.current_language] random_language, loc = random.choice(choices) loc = loc.replace(',', '') self.click_with_retry(10, By.IOS_CLASS_CHAIN, ios_other_by_name(loc, index=1)) context.current_language = random_language def select_default_language(self, context): self._open_language_picker(context) default_language = 'English ' # trailing space is mandatory here self.click_with_retry(10, By.ACCESSIBILITY_ID, default_language) context.current_language = default_language.strip() def tap_privacy_policy(self, context): bundle_id = ( self.driver.execute_script("mobile: activeAppInfo"))['bundleId'] context.bundle_id = bundle_id self.waiter(10, By.ACCESSIBILITY_ID, self.privacy_policy_btn).click() def verify_navigation_to_privacy_policy(self, context): time.sleep(10) assert ('sonymusic.com' in self.waiter(3, By.ACCESSIBILITY_ID, self.url_bar).text) def tap_terms_and_conditions(self): self.waiter(10, By.ACCESSIBILITY_ID, self.terms_and_conditions_btn).click() def verify_navigation_to_terms_and_conditions(self, context): time.sleep(10) assert ('sonymusic.com' in self.waiter(3, By.ACCESSIBILITY_ID, self.url_bar).text) def tap_notifications_and_select(self, item): items = { 'labels': 'LabelSettingsTestId', 'artists': 'participantSettingsTestId', 'songs': 'soundRecordingsSettingsTestId' } self.waiter(10, By.ACCESSIBILITY_ID, self.notifications_btn).click() self.waiter(10, By.ACCESSIBILITY_ID, items[item]).click() @staticmethod def check_version_format(version_text): pattern = r'^v?\d+\.\d+\.\d+ \([0-9a-fA-F]+\)$' if re.match(pattern, version_text): return True else: return False def verify_profile_screen_on_browser_exit(self, context): """Due to the code push functionality, the app may randomly reload upon returning or reopening. This could lead to either the home screen or the profile screen.""" try: HomeScreen(context.driver).go_home() except TimeoutException: self.waiter(3, By.ACCESSIBILITY_ID, self.my_account_title)