import pytest import allure import re import random from assertpy import assert_that from src import config from src.pages.discovery_page import DiscoveryPage from src.pages.saved_discoveries_page import SavedDiscoveriesPage from src.pages.categories_page import CategoriesPage from src.pages.category_page import CategoryPage from src.pages.discovery_results_page import DiscoveryResultsPage from src.pages.profile_page import ProfilePage from src.pages.artist_page import ArtistPage from src.pages.track_page import TrackPage from src.data.entities_data import entity_types tested_pages = [DiscoveryPage, SavedDiscoveriesPage, CategoriesPage, DiscoveryResultsPage, ProfilePage, CategoryPage, ArtistPage, TrackPage] @pytest.mark.usefixtures("app") class TestSearchByNameFromHeader: @allure.title("[Search by name] – Search from header panel - {tested_page.__name__} - {entity_type}") @allure.description("As user I want to search for artists and tracks by name from anywhere.") @allure.testcase("https://data-analytics.atlassian.net/browse/DNAD-1617") @pytest.mark.smoke @pytest.mark.parametrize('tested_page', tested_pages) @pytest.mark.parametrize('entity_type', entity_types) def test_search_by_name_from_header(self, tested_page, entity_type): search_word = ['beatles', 'hello'][entity_types.index(entity_type)] target_page = tested_page(self.app.page) if target_page.__class__.__name__ == 'CategoryPage': category_id = self.app.api.prepare_category_with_entity() self.app.go_to(self.categories_page.url + f"/{category_id}") elif target_page.__class__.__name__ == 'ArtistPage': if entity_type == 'track': pytest.skip('Cannot test track for Artist page') entity_id = self.app.api.get_random_entity(entity_type)['id'] self.app.page.goto(self.app.base_url + f"/{target_page.entity_type}/{entity_id}") elif target_page.__class__.__name__ == 'TrackPage': if entity_type == 'artist': pytest.skip('Cannot test artist for Track page') entity_id = self.app.api.get_random_entity(entity_type)['id'] self.app.page.goto(self.app.base_url + f"/{target_page.entity_type}/{entity_id}") elif target_page.__class__.__name__ == 'DiscoveryResultsPage': self.app.go_to(self.discovery_page.url) self.discovery_page.is_page_loaded() if entity_type == 'track': self.discovery_page.switch_scope_to_tracks() self.discovery_page.dsp_tiktok_on(True) self.discovery_page.bn_discovery.click() else: self.app.go_to(target_page.url) target_page.is_page_loaded() self.home_page.placeholder_search.exists() self.home_page.placeholder_search.click() self.home_page.lb_start_search.exists() # self.home_page.placeholder_search.click() with self.app.page.expect_response(self.home_page.api_search_by_name[entity_type], timeout=5000) as search_resp: self.home_page.placeholder_search.input_text(search_word) self.home_page.verify_found_cards_response_vs_page(search_word, entity_type, search_resp) self.app.page.keyboard.press("Enter") self.search_page.is_page_loaded() assert_that(self.search_page.placeholder_search.get_attribute('value')).is_equal_to(search_word) self.search_page.verify_found_cards_response_vs_page(search_word, entity_type, search_resp) # Return back self.search_page.btn_return.click() target_page.is_page_loaded() @pytest.mark.usefixtures("app") class TestSearchByNameRecentResults: @allure.title("[Search by name] - Recent Search - Entity added") @allure.description("As user I want to search for artists by name.") @allure.testcase("https://data-analytics.atlassian.net/browse/DNAD-728") @pytest.mark.smoke @pytest.mark.parametrize('tested_page', tested_pages) @pytest.mark.parametrize('entity_type', entity_types) def test_recent_search_appears(self, tested_page, entity_type): target_page = tested_page(self.app.page) if target_page.__class__.__name__ == 'CategoryPage': category_id = self.app.api.prepare_category_with_entity() self.app.go_to(self.categories_page.url + f"/{category_id}") elif target_page.__class__.__name__ == 'ArtistPage': if entity_type == 'track': pytest.skip('Cannot test track for Artist page') entity_id = self.app.api.get_random_entity(entity_type)['id'] self.app.page.goto(self.app.base_url + f"/{target_page.entity_type}/{entity_id}") elif target_page.__class__.__name__ == 'TrackPage': if entity_type == 'artist': pytest.skip('Cannot test artist for Track page') entity_id = self.app.api.get_random_entity(entity_type)['id'] self.app.page.goto(self.app.base_url + f"/{target_page.entity_type}/{entity_id}") elif target_page.__class__.__name__ == 'DiscoveryResultsPage': self.app.go_to(self.discovery_page.url) self.discovery_page.is_page_loaded() if entity_type == 'track': self.discovery_page.switch_scope_to_tracks() self.discovery_page.dsp_tiktok_on(True) self.discovery_page.bn_discovery.click() else: self.app.go_to(target_page.url) target_page.is_page_loaded() search_word = 'dua lipa' self.home_page.placeholder_search.click() self.home_page.lb_start_search.exists() self.home_page.placeholder_search.input_text(search_word) # Get entity name and id entity_name_1 = self.home_page.search_card_name.get_by(f"{entity_type.capitalize()}s", 1).get_text() # Proceed to Entity to trigger adding to recent search if config.BROWSER == 'Safari' and target_page.__class__.__name__ == 'DiscoveryResultsPage': entity_href = self.home_page.search_card_name.get_by(f"{entity_type.capitalize()}s", 1).get_attribute( "href") self.app.page.goto(config.ENV_BASE_URL + entity_href) else: self.home_page.search_card_name.get_by(f"{entity_type.capitalize()}s", 1).click() self.artist_page.is_page_loaded() assert_that(self.artist_page.entity_name.get_text()).is_equal_to(entity_name_1) # Add entity to category to check green mark entity_id = re.findall(r'[0-9]+', self.app.page.url)[0] category_id = self.app.api.insert_category('recent search category') self.app.api.insert_entity_to_category(category_id, entity_id, entity_type) assert_that(self.app.api.get_category_entities(category_id)).is_not_none() self.app.page.go_back() target_page.is_page_loaded() self.app.page.reload() target_page.is_page_loaded() # Check Recent Results section items: self.search_page.placeholder_search.input_text('') recent_search_name_1 = self.home_page.search_card_name.get_by("Recent Results", 1).get_text() assert_that(recent_search_name_1).is_equal_to(entity_name_1) # + added to category self.home_page.search_card_green_mark.get_by("Recent Results", 1).exists() @allure.title("[Search by name] - Recent Search - Count limit and sorting") @allure.description("As user I want to search for artists by name.") @allure.testcase("https://data-analytics.atlassian.net/browse/DNAD-728") @pytest.mark.smoke @pytest.mark.parametrize('tested_page', tested_pages) @pytest.mark.parametrize('entity_type', entity_types) def test_search_appears_limit_and_sorting(self, tested_page, entity_type): # Add 21 entities to Recent Results entities = self.app.api.get_entities(entity_type, 21) recent_results = [] for entity in entities: print(entity['id'], entity['name']) recent_results.append(entity['name']) self.app.api.add_to_recent_searches(entity['id'], entity_type) target_page = tested_page(self.app.page) if target_page.__class__.__name__ == 'CategoryPage': category_id = self.app.api.prepare_category_with_entity() self.app.go_to(self.categories_page.url + f"/{category_id}") elif target_page.__class__.__name__ == 'ArtistPage': if entity_type == 'track': pytest.skip('Cannot test track for Artist page') entity_id = self.app.api.get_random_entity(entity_type)['id'] self.app.page.goto(self.app.base_url + f"/{target_page.entity_type}/{entity_id}") elif target_page.__class__.__name__ == 'TrackPage': if entity_type == 'artist': pytest.skip('Cannot test artist for Track page') entity_id = self.app.api.get_random_entity(entity_type)['id'] self.app.page.goto(self.app.base_url + f"/{target_page.entity_type}/{entity_id}") elif target_page.__class__.__name__ == 'DiscoveryResultsPage': self.app.go_to(self.discovery_page.url) self.discovery_page.is_page_loaded() self.discovery_page.dsp_tiktok_on(True) self.discovery_page.random_artist_tiktok_changes() self.discovery_page.bn_discovery.click() else: self.app.go_to(target_page.url) target_page.is_page_loaded() self.home_page.placeholder_search.click() # Check limitations assert_that(self.home_page.search_card_item.get_by("Recent Results").get_count()).is_equal_to(20) # Check entities as shown in Recent Results section with DESC sorting for i in range(1, 21): assert_that(self.home_page.search_card_name.get_by("Recent Results", i).get_text()).is_equal_to( recent_results[-i]) # Add random entity to Recent Results random_n = random.choice(range(20)) random_entity_id = entities[random_n]['id'] random_entity_name = entities[random_n]['name'] self.app.api.add_to_recent_searches(random_entity_id, entity_type) # ...and check it's no top self.app.page.reload() target_page.is_page_loaded() self.home_page.placeholder_search.click() assert_that(self.home_page.search_card_name.get_by("Recent Results", 1).get_text()).is_equal_to( random_entity_name)