import datetime import logging from random import randint import allure from assertpy import assert_that from selenium.webdriver import ActionChains from ui_automation_framework.core import BasePage, TestStatus from ui_automation_framework.utils import logger as cl from app.api_client import ApiClient from app.src.pages.track_page import TrackPage from app.src.pages.tt_dashboard_page import TTDashboardPage class TTDashboardTOP10Popup(BasePage): log = cl.Logger(logging.DEBUG) def __init__(self, driver): super().__init__(driver) self.driver = driver self.tp = TrackPage(self.driver) self.tt_dashboard = TTDashboardPage(self.driver) self.api_client = ApiClient() self.ts = TestStatus(self.driver) @allure.step("TT DashboardPage Top 10 Popup- open") def open(self): index = randint(1, 10) self.log.info(f"index for top 10 popup: {index}") loc = f"(//*[contains(@id, 'tiktok-dashboard-table-cell-top-markets-')]//../*[contains(@class, 'icon-chart')])[{index}]" popup_title_loc = "top-charts-top-markets-tiktok-modal-window" self.wait_for_element_visible(loc, "xpath") popup_icon = self.wait_for_element_clickable(loc, "xpath") self.click_on_element_js(element=popup_icon) title_el = self.wait_for_element_visible(popup_title_loc) assert_that(self.is_element_displayed(element=title_el)).is_true() @allure.step("TT DashboardPage Top 10 Popup- get creations in period") def get_creations_in_period_ui(self): all_loc = "chartProgressbarLegend-creations-in-period-value" top_loc = "chartProgressbarLegend-selected-markets-value" perc_loc = "chartProgressbarLegend-selected-markets-value-percent" values = [] for loc in [all_loc, top_loc, perc_loc]: el = self.wait_for_element_visible(loc) values.append(int(self.get_text(element=el).replace(",", "").replace("(", "").replace(")", "").replace("%", ""))) return {"all": values[0], "top": values[1], "percentage": values[2]} @allure.step("TT DashboardPage Top 10 Popup- get top markets") def get_top_market_ui(self): loc = "//*[contains(@id, 'item-title')]" self.wait_for_element_visible(loc, "xpath") top_markets_el = self.get_elements(loc, "xpath") top_markets = [self.get_text(element=m).lower() for m in top_markets_el] return top_markets @allure.step("TT DashboardPage Top 10 Popup- get track id") def get_track_id(self): loc = "//*[contains(@id, 'track-link')]" track_el = self.wait_for_element_clickable(loc, "xpath") id = self.get_attribute_value(element=track_el, attribute="href").split("/")[-1] return id @allure.step("TT DashboardPage Top 10 Popup- get data from bars") def get_data_from_bars(self): loc = "//*[contains(@id, 'item-title')]" loc_perc_bars = "//*[contains(@id, 'item-percent')]" self.wait_for_element_visible(loc, "xpath") top_markets_el = self.get_elements(loc, "xpath") percentage_bars = self.get_elements(loc_perc_bars, "xpath") values = {} for bar, percent in zip(top_markets_el, percentage_bars): ActionChains(self.driver).move_to_element(to_element=bar).perform() market = self.get_text(element=bar).lower() percentage_bar = int(self.get_text(element=percent).replace("%", "")) percentage_tooltip = float(self.get_text("//*[contains(@id, 'item-value')]", "xpath").replace("%", "")) creations_els = self.get_elements("//*[contains(@id, 'item-streams')]", "xpath") avg_els = self.get_elements("//*[contains(@id, 'item-avg')]", "xpath") assert_that(percentage_bar).is_equal_to(self.tp.apply_rounding_percentage(percentage_tooltip)) assert_that(self.get_text(element=creations_els[0])).is_equal_to("Creations") assert_that(self.get_text(element=avg_els[0])).is_equal_to("Daily Average") values.update({market: {"percentage": percentage_tooltip, "creations": int(self.get_text(element=creations_els[1]).replace(",", "")), "avg": int(self.get_text(element=avg_els[1]).replace(",", ""))}}) self.log.info(f"top 10 tooltip bars values: {values}") return values @allure.step("TT DashboardPage Top 10 Popup- verify column hover") def verify_column_hover(self): loc = "(//*[contains(@id, 'tiktok-dashboard-table-cell-top-markets-')]//../*[contains(@class, 'icon-chart')])[1]" loc_column = "tiktok-dashboard-table-cell-top-markets" self.wait_for_element_visible(loc, "xpath") popup_icon = self.wait_for_element_clickable(loc, "xpath") cursor = popup_icon.value_of_css_property("cursor") assert_that(cursor).is_equal_to("pointer") self.hover_on_element(loc_column) tooltip = self.get_text("//*[contains(@id, 'tooltip')]", "xpath") assert_that(tooltip).is_equal_to( """Top 10 Markets list compiled by aggregating creations for the past 7 days from the selected date.""" ) @allure.step("TT DashboardPage Top 10 Popup- verify elements") def verify_elements(self): loc_ids = [ "//*[@id='top-charts-top-markets-tiktok-modal-window-hint-icon']", "//*[contains(@id, 'top-charts-top-markets-tiktok-modal-window-track-image-cover')]", "//*[text()='Top 10 Markets']", "//*[contains(@id, 'top-charts-top-markets-tiktok-modal-window-track-link')]", "//*[contains(@id, 'top-charts-top-markets-tiktok-modal-window-artist-link')]", "//*[contains(@id, 'graphId')]", "//*[contains(@id, 'creations-in-period-title')]", ] for loc in loc_ids: el = self.wait_for_element_visible(loc, "xpath") assert_that(self.is_element_displayed(element=el)).described_as(loc).is_true() @allure.step("TT DashboardPage Top 10 Popup- verify track name link") def verify_track(self): loc = "//*[contains(@id, 'top-charts-top-markets-tiktok-modal-window-track-link')]" track_el = self.wait_for_element_clickable(loc, "xpath") assert_that(self.get_attribute_value(element=track_el, attribute="href")).contains("/spotify/track/") @allure.step("TT DashboardPage Top 10 Popup- verify artist link") def verify_artist(self): loc = "//*[contains(@id, 'top-charts-top-markets-tiktok-modal-window-artist-link')]/..//a" artist_el = self.wait_for_element_clickable(loc, "xpath") assert_that(self.get_attribute_value(element=artist_el, attribute="href")).contains("/spotify/artist/") @allure.step("TT DashboardPage Top 10 Popup- close popup") def close(self): loc = "top-charts-top-markets-tiktok-modal-window-close-icon" tt_page_title_loc = "//*[text()='Sony TikTok Toplists']" icon = self.wait_for_element_clickable(loc) self.click_element(element=icon) title_ttp = self.wait_for_element_visible(tt_page_title_loc, "xpath") assert_that(self.is_element_displayed(element=title_ttp)).is_true() @allure.step("TT DashboardPage Top 10 Popup - verify axes and legend") def verify_axis_and_legend(self): axes_loc = "//*[contains(@class, 'highcharts') and contains(@class, 'title')][string-length(text()) > 0]" legend_locs = [ "//*[contains(@class, 'icon-tiktok')]", "//*[text()='Creations in Period']", "//*[text()='All Markets']", "//*[@id='chartProgressbarLegend-selected-markets-item--title'][text()='Top Markets']", "//*[@id='chartProgressbarLegend-creations-in-period-value']", "//*[@id='chartProgressbarLegend-selected-markets-value']", "//*[@id='chartProgressbarLegend-top-markets-title'][text()='Top Markets']", ] self.wait_for_element_visible(axes_loc, "xpath") axes_text = [self.get_text(element=a) for a in self.get_elements(axes_loc, "xpath")] items_percentage = [int(self.get_text(element=i).replace("%", "")) for i in self.get_elements("//*[contains(@id, 'chartProgressbarLegend') and contains(@id, 'item-percent')]", "xpath")] assert_that(axes_text).is_equal_to(["CREATIONS COUNT", "DATE"]) assert_that(len(self.get_elements("//*[contains(@id, 'chartProgressbarLegend') and contains(@id, 'item-title')]", "xpath"))).is_less_than_or_equal_to(10) assert_that(items_percentage).is_sorted(reverse=True) for el_popup_loc in legend_locs: el = self.wait_for_element_visible(el_popup_loc, "xpath") assert_that(self.is_element_displayed(element=el)).described_as(el_popup_loc).is_true() @allure.step("TT DashboardPage Top 10 Popup - get tooltip") def get_tooltip(self): selector = "//*[contains(@class, 'highcharts-plot-background')]" e = self.wait_for_element_visible(selector, "xpath") height = int(e.size["height"]) width = int(e.size["width"]) + 1 self.log.info("height: {height}, width: {width}".format(height=height, width=width)) step = int(width / 7) self.log.info("step: {step}".format(step=step)) tooltip = "" for t in range(0, width, step): if len(tooltip) < 1: e = self.wait_for_element_visible(selector, "xpath") ActionChains(self.driver).move_to_element_with_offset(to_element=e, xoffset=t, yoffset=height).perform() t_el = self.wait_for_element_visible(self.tp.hint_locator, "xpath") if t_el is not None: t_el = self.get_text(element=t_el) text = str(t_el) self.log.info(f"Tooltip text: {text}") if text not in [None, "None", ["None"], [None], ""]: assert_that("Creations" in text).described_as(text).is_true() assert_that("All Markets Total" in text).described_as(text).is_true() tooltip = { "country": text.split("\n")[0], "date": datetime.datetime.strptime(text.split(", ")[1].split("\n")[0], self.tp.get_date_format_from_locale()).date(), "creations": int(text.split("Creations\n")[1].split(" ")[0].split("\n")[0].replace(",", "")), "percent": int(text.split("(")[1].split("%")[0]), "all": int(text.split("\n")[-1].replace(",", "")), } self.log.info("tooltip text: {}".format(text)) self.log.info("tooltip content: {}".format(tooltip)) return tooltip @allure.step("TT DashboardPage Top 10 popup - get track name") def get_track_name(self): track_link_loc = "//*[contains(@id, 'track-link')]" return self.get_text(track_link_loc, "xpath") @allure.step("TT DashboardPage Top 10 popup - get artist name") def get_artist_name(self): loc = "//*[contains(@id, 'artist-link')]" return self.get_text(loc, "xpath")