import logging import unittest import allure import pytest from ui_automation_framework.core import TestStatus from ui_automation_framework.utils import AppConfig from ui_automation_framework.utils import logger as cl from app.api_client import ApiClient from app.mysql_db_client_custom import MySqlClient from app.src.pages.chart_digest import ChartDigestPage from app.src.pages.left_navigation_panel import LeftNavigationPanel from app.src.pages.login_page import LoginPage from app.src.pages.track_page import TrackPage @pytest.mark.usefixtures("set_up", "one_time_set_up") class DashboardPageTest(unittest.TestCase): us = AppConfig.get("market")["us"] @pytest.fixture(autouse=True) def classSetup(self): self.loginPage = LoginPage(self.driver) self.left_panel = LeftNavigationPanel(self.driver) self.chart_digest = ChartDigestPage(self.driver) self.trackPage = TrackPage(self.driver) self.api_client = ApiClient() self.sql_client = MySqlClient() self.log = cl.Logger(logging.DEBUG) self.ts = TestStatus(self.driver) @allure.story("AP-1482") @allure.severity(allure.severity_level.NORMAL) @allure.title("Verification of ‘Sony Track’ label on Daily Charts Digest (Dashboard) sections for Spotify tracks") @allure.description( """TC Verifies that the label ‘Sony Track’ is shown only for Sony tracks in Top 10, Major Moves, Bubbling Under and Entries/Exits modules on the Dashboard.""" ) def test_sony_label_for_spotify_tracks(self): user = self.loginPage.get_user_email_with_worker() token = self.api_client.authorize(user=user) self.loginPage.login_with_worker() self.loginPage.is_logged_in() self.left_panel.open_chart_digest_page() self.chart_digest.is_opened() self.trackPage.select_market(self.us["name"]) tv_names = self.chart_digest.get_names_from_table(self.chart_digest.TOP_VIEW) self.chart_digest.scroll_to_section(self.chart_digest.BUBBLING_UNDER) self.chart_digest.page_has_loaded() bu_names = self.chart_digest.get_names_from_table(self.chart_digest.BUBBLING_UNDER) mm_names = self.chart_digest.get_names_from_table(f"{self.chart_digest.MAJOR_MOVES}-table") self.chart_digest.scroll_to_section(self.chart_digest.CHART_CHANGES) cc_names = self.chart_digest.get_names_from_table(self.chart_digest.CHART_CHANGES) add_names = cc_names[: int(len(cc_names) / 2)] self.log.info(f"add names: {add_names}") rem_names = cc_names[int(len(cc_names) / 2) :] self.log.info(f"rem names: {rem_names}") # step 7 for t in [ ( mm_names, "moves/?market=us&vendor=spotify&fields=is_sony&fields=is_re_enter&fields=trends&offset=0&limit=20&order_by=moves", ), (bu_names, "top/?start=51&end=60&vendor=spotify&market=us&fields=is_sony&fields=is_re_enter&fields=trends"), (add_names, "additions/?vendor=spotify&market=us&fields=is_sony&fields=is_re_enter&fields=trends&limit=3"), ( rem_names, "removals/?vendor=spotify&market=us&fields=is_sony&fields=is_re_enter&fields=trends&offset=0&limit=3", ), (tv_names, "top/?vendor=spotify&market=us&fields=is_sony&fields=is_re_enter&fields=trends&end=10"), ]: track = self.verify_tracks_sony_non_sony(names=t[0], url_api=t[1], market=self.us, token=token) if len(list(track)) > 0 and track not in [None, ""]: # step 8 self.trackPage.open_spotify_track_page(track_id=track[0]["id"]) self.trackPage.is_sony_label_present() self.trackPage.select_market(AppConfig.get("market")["global"]["name"]) self.trackPage.is_sony_label_present() self.left_panel.open_chart_digest_page() self.chart_digest.is_opened() # step 9 self.trackPage.select_market(AppConfig.get("market")["global"]["name"]) tv_names = self.chart_digest.get_names_from_table(self.chart_digest.TOP_VIEW) tracks_api = self.api_client.get_dashboard_charts( added_url="top/?vendor=spotify&market=global&fields=is_sony&fields=is_re_enter&fields=trends&end=10", token=token, )["tracks"] for t in tracks_api: if t["is_sony"]: self.ts.markFinal( "SONY" in tv_names[list(tracks_api).index(t)], f"api and ui match for sony label: {tv_names[list(tracks_api).index(t)]}", ) else: self.ts.markFinal( "SONY" not in tv_names[list(tracks_api).index(t)], f"api and ui match for non sony label: {tv_names[list(tracks_api).index(t)]}", ) @allure.step("Chart Digest - verify track sony/non sony") def verify_tracks_sony_non_sony(self, names, url_api, market, token): tracks = self.api_client.get_dashboard_charts(added_url=url_api, token=token)["tracks"] if "additions" or "removals" in url_api: tracks = tracks[:3] self.log.info(f"api: {url_api},\ntrack in api count: \n{len(tracks)},\n ui: {len(list(names))}") self.chart_digest.page_has_loaded() tracks_ui = names sony_tracks = [] non_sony_tracks = [] for t in tracks: if t["is_sony"]: sony_tracks.append(t) self.ts.markFinal( "SONY" in tracks_ui[list(tracks).index(t)], "sony label is present in UI track: {}".format(tracks_ui[list(tracks).index(t)]), ) else: non_sony_tracks.append(t) self.ts.markFinal( "SONY" not in tracks_ui[list(tracks).index(t)], "sony label is not present in UI track: {}, index: {}".format(tracks_ui[list(tracks).index(t)], list(tracks).index(t)), ) # step 1 if len(sony_tracks) > 0: self.log.info(f'image url full: {sony_tracks[0]["image_url"]}') full_url = str(sony_tracks[0]["image_url"]) if "_" in full_url: album_id = full_url.split("_")[1] else: album_id = full_url.split("/")[-1] self.log.info(f"album id: {album_id}") try: upc_sony = tuple(self.sql_client.get_upc_for_album_id(album_id))[0] except TypeError: upc_sony = None if upc_sony is not None: is_sony_album = self.api_client.get_is_sony_album(market=market["market"], token=token, upc=upc_sony)[0]["isSony"] # step 2 self.ts.markFinal(is_sony_album, "album is sony") # step 3 music_id = self.sql_client.get_music_service_id(upc=upc_sony, market=market["market"]) self.ts.markFinal(music_id[0] == 1, f"music id is: {music_id}") if len(non_sony_tracks) > 0: # step 4, 5 full_url = str(non_sony_tracks[0]["image_url"]) if "_" in full_url: album_id = full_url.split("_")[1] else: album_id = full_url.split("/")[-1] self.log.info(f"album id: {album_id}") upc_non_sony = self.sql_client.get_upc_for_album_id(album_id) if upc_non_sony is not None: upc_non_sony = tuple(upc_non_sony)[0] is_sony_album = self.api_client.get_is_sony_album(market=market["market"], upc=upc_non_sony, token=token)[0]["isSony"] self.ts.markFinal(not is_sony_album, "album is not sony") # step 6 music_id = self.sql_client.get_music_service_id(upc=upc_non_sony[0], market=market["market"]) self.ts.markFinal(music_id is None, "music id is not present") return sony_tracks