import os import time import allure import pytest from ui_automation_framework.core import DriverFactory from ui_automation_framework.core.constants import DriverType from ui_automation_framework.utils import CoreConfig from app.src.api.facade import ApiClient from app.src.api.models.auth_model import AuthData from app.src.helpers.api_data_provider import ApiDataProvider from app.src.helpers.bs_helper import attach_video_and_logs from app.src.pages.login_page import LoginPage from app.src.pages.notifications_page import NotificationsPage from app.src.pages.playlist_page import PlaylistPage from app.src.pages.profile_page import ProfilePage from app.src.pages.search_page import SearchPage from app.src.pages.starred_page import StarredPage from app.src.pages.track_page import TrackPage @pytest.fixture() def set_up(): print("Running method level setUp") yield print("Running method level tearDown") @pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): outcome = yield rep = outcome.get_result() setattr(item, "rep_" + rep.when, rep) return rep @pytest.fixture(scope="function", autouse=True) def one_time_set_up(request): print("Running class setUp") wdf = DriverFactory() driver = wdf.get_driver() if request.cls is not None: request.cls.driver = driver yield driver if request.node.rep_call.failed and driver is not None: screenshot_on_failure(driver) if CoreConfig.SELENIUM_ENV == DriverType.MOBILE_BS: driver.quit() attach_video_and_logs(driver.session_id) attach_pixel_match_diff() if driver is not None: driver.quit() else: pass print("Running class tearDown") def screenshot_on_failure(driver): file_name = "screenshot" + str(round(time.time() * 1000)) + ".png" if len(file_name) >= 200: file_name = str(round(time.time() * 1000)) + ".png" screenshot_dir = "../../../screenshots/" relative_name = screenshot_dir + file_name current_dir = os.path.dirname(__file__) destination_file = os.path.join(current_dir, relative_name) destination_dir = os.path.join(current_dir, screenshot_dir) if not os.path.exists(destination_dir): os.makedirs(destination_dir) driver.save_screenshot(destination_file) allure.attach(driver.get_screenshot_as_png(), name=file_name, attachment_type=allure.attachment_type.PNG) def attach_pixel_match_diff(): screenshot_dir = "../../../screenshots_for_pixel_match/" current_dir = os.path.dirname(__file__) destination_dir = os.path.join(current_dir, screenshot_dir) diff_path = os.path.join(destination_dir, "pixel_match_diff.png") screenshot_for_comparing_path = os.path.join(destination_dir, "screenshot_for_comparing.png") if os.path.exists(diff_path): allure.attach.file(diff_path, name="pixel_match_diff", attachment_type=allure.attachment_type.PNG) if os.path.exists(screenshot_for_comparing_path): allure.attach.file(screenshot_for_comparing_path, name="screenshot_for_comparing", attachment_type=allure.attachment_type.PNG) # pages # TODO: elements should provide pages @pytest.fixture() def login_page(one_time_set_up, authorized_api): return LoginPage(one_time_set_up, authorized_api) @pytest.fixture() def search_page(one_time_set_up, authorized_api): return SearchPage(one_time_set_up, authorized_api) @pytest.fixture() def notifications_page(one_time_set_up): return NotificationsPage(one_time_set_up) @pytest.fixture() def track_page(one_time_set_up, authorized_api): return TrackPage(one_time_set_up, authorized_api) @pytest.fixture() def starred_page(one_time_set_up, authorized_api): return StarredPage(one_time_set_up, authorized_api) @pytest.fixture() def profile_page(one_time_set_up): return ProfilePage(one_time_set_up) @pytest.fixture() def playlist_page(one_time_set_up, authorized_api): return PlaylistPage(one_time_set_up, authorized_api) @pytest.fixture(scope="session") def api(): return ApiClient() @pytest.fixture(scope="session") def api_user(): return AuthData() @pytest.fixture(scope="session") def authorized_api(api, api_user): print("AUTH API FIXTURE") api.authorize(api_user) return api @pytest.fixture() def data_provider(authorized_api): return ApiDataProvider(authorized_api) @pytest.fixture() def sony_tracks_isrcs(data_provider): return data_provider.get_sony_tracks_isrcs()[0] @pytest.fixture() def sony_track_isrc(request, sony_tracks_isrcs): return sony_tracks_isrcs[request.param] @pytest.fixture() def with_default_settings(authorized_api): default = authorized_api.apollo.get_user_data_api_v1_settings().default authorized_api.apollo.put_user_data_api_v1_settings(default) yield authorized_api.apollo.put_user_data_api_v1_settings(default)