import logging from contextlib import suppress import allure from ui_automation_framework.utils import logger as cl from waiting import TimeoutExpired, ALL from src.helpers.waiting_wrapper import wait from src.locators.common_locators import CommonLocators from src.ui_elements.button_ui_element import ButtonUIElement from src.ui_elements.input_ui_element import InputUIElement from src.ui_elements.label_ui_element import LabelUIElement, LabelUIByText from src.ui_elements.list_ui_element import ListUIElement from src.pages.navigation_page import NavigationPage class CommonPage(NavigationPage): log = cl.Logger(logging.DEBUG) def __init__(self, driver): super().__init__(driver) self.driver = driver @property def lb_percentage(self): return LabelUIElement(self.driver, CommonLocators.LB_DATA_PERCENT, " %") @property def charts_list(self): return ListUIElement(self.driver, by_position_locator=CommonLocators.TRACK_CHARTS_BY_POSITION, by_text_locator=CommonLocators.TRACK_CHARTS_DATA_BY_NAME, all_elements_locator=CommonLocators.TRACK_CHARTS_LIST) @property def track_chart_error(self): return LabelUIElement(self.driver, CommonLocators.TRACK_CHART_ERROR, "Track chart error") @property def lb_label(self): return LabelUIByText(self.driver, CommonLocators.LABEL_BY_NAME) @property def lb_chosen_market(self): return LabelUIByText(self.driver, CommonLocators.CHOSEN_MARKET) @property def chart_by_name(self): return LabelUIByText(self.driver, CommonLocators.TRACK_CHARTS_DATA_BY_NAME) @property def toggle_bn_chart_w(self): return ButtonUIElement(self.driver, CommonLocators.CHART_W_TOGGLE, "CHART W") @property def toggle_bn_2w(self): return ButtonUIElement(self.driver, CommonLocators.W2_TOGGLE, "2W range") @property def toggle_bn_1w(self): return ButtonUIElement(self.driver, CommonLocators.W2_TOGGLE, "1W range") @property def toggle_bn_4w(self): return ButtonUIElement(self.driver, CommonLocators.W4_TOGGLE, "4W range") @property def toggle_bn_6w(self): return ButtonUIElement(self.driver, CommonLocators.W6_TOGGLE, "6W range") @property def toggle_bn_8w(self): return ButtonUIElement(self.driver, CommonLocators.W8_TOGGLE, "8W range") @property def toggle_bn_26w(self): return ButtonUIElement(self.driver, CommonLocators.W26_TOGGLE, "26W range") @property def toggle_bn_1y(self): return ButtonUIElement(self.driver, CommonLocators.Y1_TOGGLE, "1Y range") @property def bn_choose_market(self): return ButtonUIElement(self.driver, CommonLocators.CHOOSE_MARKET, "Choose market") @property def lb_image(self): return LabelUIElement(self.driver, CommonLocators.IMAGE, "Track image") @property def lb_track_stats_alltime(self): return LabelUIElement(self.driver, CommonLocators.TRACK_STATS_ALLTIME, "Track statistics Alltime") @property def lb_track_stats_1w(self): return LabelUIElement(self.driver, CommonLocators.TRACK_STATS_1W, "Track statistics 1 week") @property def lb_track_stats_2w(self): return LabelUIElement(self.driver, CommonLocators.TRACK_STATS_2W, "Track statistics 2 weeks") @property def lb_track_stats_4w(self): return LabelUIElement(self.driver, CommonLocators.TRACK_STATS_4W, "Track statistics 4 weeks") @property def list_markets(self): return ListUIElement(self.driver, all_elements_locator=CommonLocators.MARKETS_LIST, by_position_locator=CommonLocators.MARKETS_LIST_BY_POSITION, by_text_locator=CommonLocators.MARKETS_LIST_BY_TEXT) @property def in_search(self): return InputUIElement(self.driver, CommonLocators.SEARCH_FIELD, "Search Market") @allure.step("Check item image presence") def is_image_present(self): with suppress(TimeoutExpired): self.wait_for_element_present(CommonLocators.IMAGE[1], "class-chain", timeout=120) return self.lb_image.enabled return False @allure.step("Check Alltime Track statistic presence") def is_track_alltime_statistics_present(self): with suppress(TimeoutExpired): try: alltime_value = self.lb_track_stats_alltime.attr_name.split(' ')[1] print("All time: {}".format(alltime_value)) int(alltime_value[0]) except ValueError: print("Alltime has no number!") else: return wait(ALL([self.lb_track_stats_alltime.is_visible])) return False @allure.step("Check 1w Track statistic presence") def is_track_1w_statistics_present(self): with suppress(TimeoutExpired): print(self.lb_track_stats_1w.attr_name) return wait(self.lb_track_stats_1w.is_visible) return False @allure.step("Check 2w Track statistic presence") def is_track_2w_statistics_present(self): with suppress(TimeoutExpired): print(self.lb_track_stats_2w.attr_name) return wait(self.lb_track_stats_2w.is_visible) return False @allure.step("Check 4w Track statistic presence") def is_track_4w_statistics_present(self): with suppress(TimeoutExpired): print(self.lb_track_stats_4w.attr_name) return wait(self.lb_track_stats_4w.is_visible) return False @allure.step("Check Track statistic presence for {chart_name}") def is_chart_present(self, chart_name): with suppress(TimeoutExpired): chart = self.charts_list.get_item_by_text(chart_name) print("Track has chart: {}".format(chart.text)) return chart.is_visible return False @allure.step("Check {label_name} label presence") def is_label_present(self, label_name): with suppress(TimeoutExpired): element = self.lb_label.get_item_by_text(label_name) return wait(element.is_visible) return False @allure.step("Check streams data presence and no error") def is_stream_data_present(self): if self.lb_percentage.is_visible() is False: return not self.track_chart_error.is_visible() else: return True @allure.step("Switch to CHART W range") def switch_range_to_chart_week(self): self.toggle_bn_chart_w.click() return self @allure.step("Switch to 1W range") def switch_range_to_1w(self): self.toggle_bn_1w.click() return self @allure.step("Switch to 2W range") def switch_range_to_2w(self): self.toggle_bn_2w.click() return self @allure.step("Switch to 6W range") def switch_range_to_6w(self): self.toggle_bn_6w.click() return self @allure.step("Switch to 4W range") def switch_range_to_4w(self): self.toggle_bn_4w.click() return self @allure.step("Switch to 8W range") def switch_range_to_8w(self): self.toggle_bn_8w.click() return self @allure.step("Switch to 2W range") def switch_range_to_26w(self): self.toggle_bn_26w.click() return self @allure.step("Switch to 1Y range") def switch_range_to_1y(self): self.toggle_bn_1y.click() return self @allure.step("Inspect Market value") def chosen_market_value(self, market_abbrev): with suppress(TimeoutExpired): element = self.lb_chosen_market.get_item_by_text(market_abbrev) return wait(element.is_visible) return False @allure.step("Selecting market: {market}") def select_market_by_name(self, market): self.bn_choose_market.click() self.in_search.click() self.in_search.send_text(market) self.press_return_to_hide_keyboard() # self.list_markets.select_by_position(1) self.list_markets.select_by_text(market) return self def get_chart_text(self, chart_name): with suppress(TimeoutExpired): chart = self.charts_list.get_item_by_text(chart_name) print(chart.text) return chart.text @staticmethod def check_demographic_elements_presence(source, chart_text): labels = {'SPOTIFY': ["Female", "Male", "Non-Binary", "Unknown", "Age Group", "Total %"], 'APPLE': ["Female", "Male", "Unknown", "Age Group", "Total %"], 'YOUTUBE': ["Female", "Male", "Unknown", "Age Group", "Total %"]} no_streams_msg = "This track doesn’t have any streams for this period" no_info_msg = "We don’t have any " return any([ all([label in chart_text for label in labels[source]]), no_streams_msg in chart_text, no_info_msg in chart_text ]) @allure.step("Check {source} Demographics all elements presence") def are_demographic_elements_present_for(self, source): chart_text = self.get_chart_text(source + " Demographics") return self.check_demographic_elements_presence(source, chart_text)