import unittest from datetime import datetime, timedelta 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 app.api_client import ApiClient from app.src.pages.global_track_priorities_page import GlobalTrackPrioritiesPage from app.src.pages.login_page import LoginPage from app.src.pages.track_page import TrackPage from app.src.pages.tt_dashboard_page import TTDashboardPage from app.src.pages.tt_dashboard_pot_popup import TTDashboardPOTPopup from app.src.pages.tt_dashboard_top10 import TTDashboardTOP10Popup @pytest.mark.usefixtures("set_up", "one_time_set_up") class TTDashboardPageTest(unittest.TestCase): @pytest.fixture(autouse=True) def classSetup(self): self.loginPage = LoginPage(self.driver) self.tt_dashboard = TTDashboardPage(self.driver) self.trackPage = TrackPage(self.driver) self.tt_dashboard_potp = TTDashboardPOTPopup(self.driver) self.tt_dashboard_top10 = TTDashboardTOP10Popup(self.driver) self.gtp = GlobalTrackPrioritiesPage(self.driver) self.api_client = ApiClient() self.ts = TestStatus(self.driver) @parameterized.expand( [ ("daily", "by-creations-change"), ("daily", "by-creations"), ("7days", "by-creations-change"), ("7days", "by-creations"), ] ) @allure.story("AP-7338") @allure.severity(allure.severity_level.NORMAL) @allure.title("[AP-6913] TikTok Dashboard. Popup - Top 10 Markets. Legend calculations") @allure.description("TC verifies the correctness of metrics in the graph legend on the Top 10 Markets popup on the Sony TikTok Toplist page.") def test_tt_dashboard_top10_legend_calculations_verification(self, view, tab): self.loginPage.login_with_worker() self.trackPage.select_market(AppConfig.get("market")["us"]["name"]) self.tt_dashboard.open() self.tt_dashboard.select_tab(tab) self.tt_dashboard.select_view(view) end = self.tt_dashboard.get_selected_date_ui() end = datetime.strptime(end, self.trackPage.get_date_format_from_locale()).strftime("%Y-%m-%d") start = (datetime.strptime(end, "%Y-%m-%d") - timedelta(days=6)).date() # step 1, 9 self.tt_dashboard_top10.open() creation_in_period_ui = self.tt_dashboard_top10.get_creations_in_period_ui() creations_all_ui = creation_in_period_ui["all"] top_markets_ui = self.tt_dashboard_top10.get_top_market_ui() spotify_id = self.tt_dashboard_top10.get_track_id() isrc = self.api_client.get_isrc_by_spotify_id("_gl", spotify_id)["external_ids"]["isrc"] api_all = self.api_client.get_tiktok_tracks_analytics(isrc, start, end, top_markets_ui + ["worldwide"]) api_resp = api_all["breakdowns"]["totals"] api_days = int(api_all["days_count"]) assert_that(api_resp["worldwide"]["creations"]).is_equal_to(creations_all_ui) # step 2 creations_top_ui = creation_in_period_ui["top"] assert_that(sum(api_resp[c]["creations"] for c in top_markets_ui)).is_equal_to(creations_top_ui) # step 3 creations_percentage_ui = creation_in_period_ui["percentage"] assert_that(round(creations_top_ui / creations_all_ui * 100)).is_equal_to(creations_percentage_ui) # steps 4-5 market_1_bar = top_markets_ui[0] tooltip_data = self.tt_dashboard_top10.get_data_from_bars()[market_1_bar] # step 6 assert_that(round(tooltip_data["creations"] / creations_all_ui * 100, 1)).is_equal_to(tooltip_data["percentage"]) # step 7 assert_that(api_resp[market_1_bar]["creations"]).is_equal_to(tooltip_data["creations"]) # step 8 assert_that(round(api_resp[market_1_bar]["creations"] / api_days)).is_equal_to(tooltip_data["avg"])