import datetime 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.chart_digest import ChartDigestPage 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.third_party.applecharts_page import AppleCharts from app.src.pages.top_charts_page import TopChartsPage from app.src.pages.track_page import TrackPage @pytest.mark.usefixtures("set_up", "one_time_set_up") class TopChartsPageTest(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.trackPage = TrackPage(self.driver) self.top_charts_page = TopChartsPage(self.driver) self.apple_charts = AppleCharts(self.driver) self.comparison_page = TrackComparisonPage(self.driver) self.chart_digest = ChartDigestPage(self.driver) self.api_client = ApiClient() self.ts = TestStatus(self.driver) @allure.story("AP-1771") @allure.severity(allure.severity_level.NORMAL) @allure.title("Signify songs entering Apple charts for the first time") @allure.description( """TC verifies that a user is able to distinguish new tracks quickly and identify newcomers in the scope of tracks that just did a major move.""" ) def test_top_apple_new(self): self.loginPage.login_with_worker() self.loginPage.is_logged_in() self.top_charts_page.select_market(self.us["name"]) self.left_panel.open_chart_digest_page() self.chart_digest.is_opened() self.chart_digest.select_tab(self.chart_digest.TAB_APPLE) mm_trends = self.chart_digest.get_position_trends_for_table("{}-table".format(self.chart_digest.MAJOR_MOVES)) self.ts.markFinal(all(str(t).isdigit() for t in mm_trends), "no newcomers in mm table") self.top_charts_page.open_top_chart_page() self.top_charts_page.top_chart_page_is_opened() self.top_charts_page.switch_tab(self.top_charts_page.APPLE_TYPE) # step 1-6 self.verify_new_and_reentered_apple(self.us["api"]) def verify_new_and_reentered_apple(self, market): ui_data_from_table = self.top_charts_page.get_all_from_table_apple() entry_reentry_ui_tracks = [] for i, j in zip(ui_data_from_table[1]["trends"], ui_data_from_table[1]["names"]): if i == "": entry_reentry_ui_tracks.append(str(j).split("\n")[0]) chart_date = datetime.datetime.strptime(self.top_charts_page.get_chosen_date(), self.trackPage.get_date_format_from_locale()).strftime("%Y-%m-%d") apple_api = self.api_client.get_apple_top_chart_tracks(chart_date=chart_date, market=market)["items"] entry_api_tracks = [] reentry_api_tracks = [] for i in range(len(apple_api)): if apple_api[i]["metrics"]["is_entry"] is True and datetime.datetime.strptime(apple_api[i]["lifetime_metrics"]["earliest_position_date"], "%Y-%m-%d").date() == datetime.datetime.strptime(apple_api[i]["lifetime_metrics"]["latest_position_date"], "%Y-%m-%d").date(): entry_api_tracks.append(apple_api[i]["public_meta"]["name"]) elif apple_api[i]["metrics"]["is_entry"] is True and datetime.datetime.strptime(apple_api[i]["lifetime_metrics"]["earliest_position_date"], "%Y-%m-%d").date() < datetime.datetime.strptime(apple_api[i]["lifetime_metrics"]["latest_position_date"], "%Y-%m-%d").date(): reentry_api_tracks.append(apple_api[i]["public_meta"]["name"]) self.top_charts_page.log.info(f"New entry tracks: {entry_api_tracks}") self.top_charts_page.log.info(f"Re entry tracks: {reentry_api_tracks}") if len(entry_api_tracks) > 0: assert_that(entry_api_tracks).is_subset_of(entry_reentry_ui_tracks) elif len(reentry_api_tracks) > 0: assert_that(reentry_api_tracks).is_subset_of(entry_reentry_ui_tracks) assert_that(len(entry_api_tracks + reentry_api_tracks)).is_equal_to(len(entry_reentry_ui_tracks))