import logging import unittest from collections import Counter import allure import pytest from assertpy import assert_that 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.album_page import AlbumPage from app.src.pages.comparison_page import TrackComparisonPage 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 DeepLinkingTrackPageTest(unittest.TestCase): start_date = "2020-10-01" end_date = "2020-10-28" url_to_go = f"&str_gr_dsp=3&str_gr_view=2&str_gr_type=2&str_gr_country=gb&str_gr_start_date={start_date}&str_gr_end_date={end_date}" @pytest.fixture(autouse=True) def classSetup(self): self.loginPage = LoginPage(self.driver) self.tp = TrackPage(self.driver) self.ap = AlbumPage(self.driver) self.cp = TrackComparisonPage(self.driver) self.leftPanel = LeftNavigationPanel(self.driver) self.api_client = ApiClient() self.log = cl.Logger(logging.DEBUG) self.ts = TestStatus(self.driver) @allure.story("AP-2306") @allure.severity(allure.severity_level.NORMAL) @allure.title("[AP-1689] Amazon: Chart legend calculation") @allure.description( """TC verifies that user can see track’s streaming data: graph+legend for Amazon dsp. All option as selection graph view, country, source and downloading csv file are available for Amazon chart. """ ) def test_amazon_chart_legend_calculation(self): self.loginPage.login_with_worker() self.tp.open_spotify_track_page_with_market_id(track_id=AppConfig.get("track25")["id"], market_id=AppConfig.get("market")["uk"]["url"]) self.tp.open_current_url_with_param(self.url_to_go) # step 1 lf = self.tp.graph_get_legend_columns(self.tp.lf_locator) assert_that(lf).is_equal_to(self.tp.lf_legend) # step 8 lb = self.tp.graph_get_legend_columns(self.tp.lb_locator) assert_that(lb).is_equal_to(self.tp.lb_legend) # step 10 dt = self.tp.graph_get_legend_columns(self.tp.dt_locator) assert_that(dt).is_equal_to(self.tp.dt_legend) # step 14 os = self.tp.graph_get_legend_columns(self.tp.os_locator) assert_that(os).is_equal_to(self.tp.os_legend) # step 2-7,9 lb_lf_ui = self.tp.get_lean_values() # step 11-13 dt_ui = self.tp.get_device_type_tooltips() # step 15-17 os_ui = self.tp.graph_get_os_tooltips() api_resp = self.api_client.get_amazon_tracks(isrc=AppConfig.get("track25")["isrc"], start_date=self.start_date, end_date=self.end_date) resp = self.get_resp_for_market(response=api_resp, market=AppConfig.get("market")["uk"]["market"]) self.compare_ui_and_api_tooltips_lb_lf(legend_lb_lf=lb_lf_ui, legend_dt=dt_ui, legend_os=os_ui, response=resp) @allure.step("GET market values from amazon tracks response") def get_resp_for_market(self, response, market): market_resp = [] for r in response["items"]: if r["country_code"] == market: market_resp.append(r) merged_resp = {"selection_source_type": {}, "device_type": {}, "operating_system": {}} for e in market_resp: merged_resp["selection_source_type"] = dict(Counter_tweaked(merged_resp["selection_source_type"]) + Counter_tweaked(e["selection_source_type"])) merged_resp["device_type"] = dict(Counter_tweaked(merged_resp["device_type"]) + Counter_tweaked(e["device_type"])) merged_resp["operating_system"] = dict(Counter_tweaked(merged_resp["operating_system"]) + Counter_tweaked(e["operating_system"])) self.log.info(f"merged resp: {merged_resp}") return merged_resp @allure.step("TP - Compare UI graph tooltip values and api response and Lean Back/Forward legend") def compare_ui_and_api_tooltips_lb_lf(self, response, legend_lb_lf, legend_dt, legend_os): lb_ui = legend_lb_lf["lean_back_by_source"] lf_ui = legend_lb_lf["lean_forward_by_source"] legend_total = int(self.tp.get_streams_in_period_total().replace(",", "")) count_days = self.tp.graph_get_count_days_from_calendar() sst = response["selection_source_type"] dt = response["device_type"] os = response["operating_system"] search = sst["search"] artist = sst["artist"] album = sst["album"] songs = sst["songs"] station = ( sst["amf_station"] + sst["amf_station_seed"] + sst["custom_mix"] + sst["error"] + sst["prime_station"] + sst["prime_station_seed"] + sst["similarity"] + sst["similarity_station"] + sst["station"] + sst["undefined"] + sst["unknown"] + sst["unl_station_seed"] + sst["unlimited_station"] + sst["unlimited_station_se"] ) playlists = sst["gno_prime_playlist"] + sst["gno_unl_playlist"] + sst["golden_playlist"] + sst["prime_playlist"] + sst["shared_playlist"] + sst["unlimited_playlist"] + sst["personalize_playlist"] user_playlist = sst["auto_playlist"] + sst["recently_added"] + sst["recently_played"] + sst["user_playlist"] + sst["genre"] api_values_tooltip = [search, artist, album, songs, station, playlists, user_playlist] api_values_tooltip = api_values_tooltip + [sum(api_values_tooltip)] api_values_lb = [playlists, station] api_values_lf = [user_playlist, songs, album, artist, search] other_dt = dt["connected_home_audio_tether"] + dt["connected_home_tv"] + dt["gaming_system"] + dt["in_dash_car_integration"] + dt["mobile_to_car_tether"] + dt["other"] + dt["tablet_app"] api_values_dt = [dt["voice_controlled_device"], dt["cell_phone_app_mobile_web"], dt["desktop"], other_dt] other_os = os["fire_os"] + os["third_party_device_os"] + os["unknown"] api_values_os = [os["alexa"], os["android"], os["ios"], os["mac_osx"], os["windows"], other_os] # legend lb_lf assert_that(api_values_lf).is_equal_to(lf_ui) assert_that(api_values_lb).is_equal_to(lb_ui) # device type assert_that(api_values_dt).is_equal_to(legend_dt["streams"]) # operating system assert_that(api_values_os).is_equal_to(legend_os["streams"]) # legend total assert_that(api_values_tooltip[-1]).is_equal_to(legend_total) # verify daily avg assert_that([int(self.tp.apply_rounding_percentage(l / count_days)) for l in legend_dt["streams"]]).is_equal_to(legend_dt["avg"]) assert_that([int(self.tp.apply_rounding_percentage(l / count_days)) for l in legend_os["streams"]]).is_equal_to(legend_os["avg"]) assert_that([int(self.tp.apply_rounding_percentage(l / count_days)) for l in lf_ui]).is_equal_to(legend_lb_lf["lf_avg"]) assert_that([int(self.tp.apply_rounding_percentage(l / count_days)) for l in lb_ui]).is_equal_to(legend_lb_lf["lb_avg"]) # verify % lf_percent_ui = [int(l / legend_total * 100) for l in lf_ui] lb_percent_ui = [int(l / legend_total * 100) for l in lb_ui] lf_percent_exp = [int(l) for l in legend_lb_lf["lf_perc"]] lb_percent_exp = [int(l) for l in legend_lb_lf["lb_perc"]] dt_percent_exp = [int(l) for l in legend_dt["percentage"]] os_percent_exp = [int(l) for l in legend_os["percentage"]] assert_that(lf_percent_exp).is_equal_to(lf_percent_ui) assert_that(lb_percent_exp).is_equal_to(lb_percent_ui) assert_that([int(l / legend_total * 100) for l in legend_os["streams"]]).is_equal_to(os_percent_exp) assert_that([int(l / legend_total * 100) for l in legend_dt["streams"]]).is_equal_to(dt_percent_exp) class Counter_tweaked(Counter): def __add__(self, other): if not isinstance(other, Counter): return NotImplemented result = Counter_tweaked() for elem, count in self.items(): newcount = count + other[elem] result[elem] = newcount for elem, count in other.items(): if elem not in self: result[elem] = count return result