import logging import unittest import allure import pytest from assertpy import assert_that from parameterized import parameterized 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.src.pages.artist_page import ArtistPage 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 TopChartsPageTest(unittest.TestCase): log = cl.Logger(logging.DEBUG) @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.artist_page = ArtistPage(self.driver) self.api_client = ApiClient() self.ts = TestStatus(self.driver) @parameterized.expand(["us", "argentina"]) @allure.story("AP-660") @allure.severity(allure.severity_level.NORMAL) @allure.title("Verification of the 'Top 10' section for Spotify tracks") @allure.description("""TC verifies Top 10 section on the Dashboard page functionality works as expected for Spotify tracks.""") def test_top_10_spotify(self, market): 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(AppConfig.get("market")[market]["name"]) # step 1-3 mm_names_before = self.chart_digest.get_names_from_table(self.chart_digest.MAJOR_MOVES) cc_names_before = self.chart_digest.get_names_from_table(self.chart_digest.CHART_CHANGES) bu_names_before = self.chart_digest.get_names_from_table(self.chart_digest.BUBBLING_UNDER) self.chart_digest.select_tab(tab="{}-{}".format(self.chart_digest.TOP_VIEW, self.chart_digest.TAB_APPLE)) # step 4-6 for tab in [self.chart_digest.TAB_APPLE, self.chart_digest.TAB_SPOTIFY]: self.chart_digest.select_tab("{table}-{tab}".format(table=self.chart_digest.TOP_VIEW, tab=tab)) for section in [self.chart_digest.SECTION_EXITS_TOP_10, self.chart_digest.SECTION_TOP_10]: names_before = self.chart_digest.get_names_from_table(self.chart_digest.TOP_VIEW) self.chart_digest.select_section_for_table("{table}-{section}".format(table=self.chart_digest.TOP_VIEW, section=section)) names_after = self.chart_digest.get_names_from_table(self.chart_digest.TOP_VIEW) mm_names_after = self.chart_digest.get_names_from_table(self.chart_digest.MAJOR_MOVES) cc_names_after = self.chart_digest.get_names_from_table(self.chart_digest.CHART_CHANGES) bu_names_after = self.chart_digest.get_names_from_table(self.chart_digest.BUBBLING_UNDER) self.ts.markFinal(names_before != names_after, "top view names are changes") self.ts.markFinal(mm_names_before == mm_names_after, "mm is not changed") self.ts.markFinal(cc_names_before == cc_names_after, "cc is not changed") self.ts.markFinal(bu_names_before == bu_names_after, "bu is not changed") # step 7 images = self.chart_digest.get_track_images_for_table(table=self.chart_digest.TOP_VIEW) tracks = self.chart_digest.get_tracks_for_table(table=self.chart_digest.TOP_VIEW) trends = self.chart_digest.get_position_trends_for_table(table=self.chart_digest.TOP_VIEW, without_blue_dots=False) positions = self.chart_digest.get_track_position_for_table(table=self.chart_digest.TOP_VIEW) self.ts.markFinal( len(images) == len(tracks[0]) == len(trends) == len(positions) < 11, "images, positions, trends and tracks are displayed: " "i: {},\n t: {},\n td: {},\n p:{}".format(len(images), len(tracks[0]), len(trends), len(positions)), ) self.ts.markFinal(all(p in list(range(1, 11)) for p in positions), "positions are correct") # step 8 self.ts.markFinal(positions == sorted(positions), "tracks are sorted by positions") # step 9 t = self.chart_digest.get_text(element=tracks[1][0]) self.chart_digest.click_element(element=tracks[1][0]) self.trackPage.is_track_page_title_present(track=t) self.trackPage.go_back() # step 10 artists = self.chart_digest.get_artists_for_table(table=self.chart_digest.TOP_VIEW) artist = self.chart_digest.get_text(element=artists[1][0]) self.chart_digest.click_element(element=artists[1][0]) self.artist_page.verify_title(title=artist) self.trackPage.go_back() # step 11-14 self.compare_ui_api_spotify_top_10(token=token, market=AppConfig.get("market")[market]) self.chart_digest.verify_trend_calculation(table=self.chart_digest.TOP_VIEW, vendor=self.chart_digest.TAB_SPOTIFY, market=AppConfig.get("market")[market]["market"], token=token) def compare_ui_api_spotify_top_10(self, token, market): tracks = self.chart_digest.get_tracks_for_table(table=self.chart_digest.TOP_VIEW)[0] artists_ui_raw = self.chart_digest.get_artists_for_table(table=self.chart_digest.TOP_VIEW)[0] positions = self.chart_digest.get_track_position_for_table(table=self.chart_digest.TOP_VIEW) trends = self.chart_digest.get_position_trends_for_table(table=self.chart_digest.TOP_VIEW) self.ts.markFinal(all(p in list(range(1, 11)) for p in positions), "positions are correct") self.ts.markFinal(positions == sorted(positions), "tracks are sorted by positions") spotify_api = self.api_client.get_vendor_api_tracks(vendor=self.chart_digest.TAB_SPOTIFY, market=market["market"], token=token) top_spotify_api = spotify_api["tracks"] tracks_names_api = [top_spotify_api[n]["name"] for n in range(len(top_spotify_api))] tracks_artists_api = [spotify_api["tracks"][a]["artists"][b]["name"] for a in range(len(spotify_api["tracks"])) for b in range(len(spotify_api["tracks"][a]["artists"]))] tracks_trends_api = [abs(top_spotify_api[t]["trend"]) for t in range(len(top_spotify_api))] positions_api = [top_spotify_api[p]["position"] for p in range(len(top_spotify_api))] assert_that(tracks).is_equal_to(tracks_names_api) assert_that(artists_ui_raw).is_equal_to(tracks_artists_api) blue_dot_indexes = [tracks_trends_api.index(t_api) for t_api in tracks_trends_api if abs(t_api) > 1000] if len(blue_dot_indexes) > 0: for index in blue_dot_indexes: self.chart_digest.verify_trend_blue_dot_present(index=index + 1, table=self.chart_digest.TOP_VIEW) else: assert_that(trends).is_equal_to(tracks_trends_api) assert_that(positions).is_equal_to(positions_api)