import math import unittest import allure import pytest from assertpy import assert_that from ui_automation_framework.core import TestStatus from ui_automation_framework.utils import AppConfig from app.api_client import ApiClient from app.src.pages.album_page import AlbumPage from app.src.pages.apple_playlist_page import ApplePlaylistPage 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): date_to_check = "2021-01-15" track42 = AppConfig.get("track42") url_to_go = "spotify/track/{id}?market={market}&str_gr_dsp=3&str_gr_view=2&str_gr_type=2&str_gr_start_date={date}&str_gr_end_date={date}" @pytest.fixture(autouse=True) def classSetup(self): self.loginPage = LoginPage(self.driver) self.tp = TrackPage(self.driver) self.ap = AlbumPage(self.driver) self.app = ApplePlaylistPage(self.driver) self.cp = TrackComparisonPage(self.driver) self.leftPanel = LeftNavigationPanel(self.driver) self.api_client = ApiClient() self.ts = TestStatus(self.driver) @allure.story("AP-2313") @allure.severity(allure.severity_level.NORMAL) @allure.title("[AP-4323][AP-1689] Amazon: Stream data calculation for global and local markets for a particular date") @allure.description( """TC verifies that user can see track’s streaming data: graph+legend for Amazon dsp for any particular date for local and global markets. Displayed streaming data on a tooltip and legend is the same as data, that is got from Amazon api.""" ) def test_streams_global_local_for_one_date(self): self.loginPage.login_with_worker() url_before = self.tp.get_url() api_resp = self.api_client.get_amazon_tracks(isrc=self.track42["isrc"], start_date=self.date_to_check, end_date=self.date_to_check) for m in [AppConfig.get("market")["us"], AppConfig.get("market")["global"]]: url_to_open = self.url_to_go.format(date=self.date_to_check, id=self.track42["id"], market=m["url"]) self.tp.open_url(url_before + url_to_open) # step 1-3, 8-9 tooltip_totals = self.tp.graph_get_tooltip_totals(count=2)[0] resp = self.get_resp_for_market(response=api_resp, market=m["api"]) # step 4 lb_lf_ui = self.tp.get_lean_values() # step 5, 10 dt_ui = self.tp.get_device_type_tooltips()["streams"] # step 6, 11 os_ui = self.tp.graph_get_os_tooltips()["streams"] # step 7, 12 self.compare_ui_and_api_tooltips_lb_lf(tooltip=tooltip_totals, 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 = r return market_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, tooltip, response, legend_lb_lf, legend_dt, legend_os): tooltip_values = tooltip["values"] tooltip_percentage = tooltip["percentage"] 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(",", "")) 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] percentage_exp = [] for t in tooltip_values[:-1]: percentage = t / tooltip_values[-1] * 100 if percentage - math.floor(percentage) < 0.5: percentage = math.floor(percentage) else: percentage = math.ceil(percentage) percentage_exp.append(int(percentage)) # tooltip assert_that(api_values_tooltip).is_equal_to(tooltip_values) assert_that(percentage_exp).is_equal_to(tooltip_percentage) # 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) # operating system assert_that(api_values_os).is_equal_to(legend_os) # legend total assert_that(api_values_tooltip[-1]).is_equal_to(legend_total)