class Test_001_Search:
    logger = LogGen.loggen()
    # TODO сделать логгер в режиме добавления записей
    logger.info("Test_001_Search")
    path_to_screenshots = os.path.join('.', 'screenshots')
    # TODO добавить скриншоты

    yandex_search_URL = ReadConfig.get_yandex_search_url()

    def test_check_search_box(self, setup):
        self.logger.info("Checking if a search field exists")
        self.driver = setup
        self.driver.get(self.yandex_search_URL)
        self.yp = YandexSearchPage(self.driver)
        try:
            self.yp.find_search_box()
            self.logger.info(
                f"Test passed: Search field found by {self.yp.search_box_locator}"
            )
            time.sleep(2)
        except TimeoutException:
            self.logger.error(
                f"Test failed: Search field not found by {self.yp.search_box_locator} for {self.yp.delay} seconds"
            )
        finally:
            self.driver.quit()
            self.logger.info("---------------")

    def test_suggests_in_search_box(self, setup):
        self.logger.info("Checking suggests in search field")
        self.driver = setup
        self.driver.get(self.yandex_search_URL)
        self.yp = YandexSearchPage(self.driver)
        try:
            self.yp.set_text_to_search_box('Тензор')
            self.yp.find_suggest_box()
            self.yp.send_enter_to_search_box()
            request_results = self.yp.get_request_results()
            if request_results:
                self.search_href_in_list_of_links(
                    request_results=request_results,
                    link_to_search=r"https://tensor.ru/",
                    number_results=5)
            else:
                self.logger.error(
                    "Test failed: There is no links in result of search")
        except TimeoutException:
            # TODO добавить отработку различных ненайденок
            self.logger.error(
                f"Test failed: Suggest field not found by {self.yp.suggest_box_locator} for {self.yp.delay} seconds"
            )
        finally:
            self.driver.quit()
            self.logger.info("---------------")

    def search_href_in_list_of_links(self, request_results,
                                     link_to_search: str, number_results: int):
        list_of_links = []
        for element in request_results:
            element_with_link = element.find_element_by_tag_name('a')
            link = element_with_link.get_attribute("href")
            list_of_links.append(link)

        if link_to_search in list_of_links:
            index = list_of_links.index(link_to_search) + 1
            if index <= number_results:
                self.logger.info(
                    f"Test passed: {link_to_search} find in {index} request position"
                )
            else:
                self.logger.error(
                    f"Test failed: {link_to_search} find, but in {index} request position"
                )
        else:
            self.logger.error(
                f"Test failed: {link_to_search} did not find in request")
class Test_002_Pictures:
    logger = LogGen.loggen()

    logger.info("Test_002_Pictures")
    path_to_screenshots = os.path.join('.', 'screenshots')
    # TODO добавить скриншоты

    yandex_search_URL = ReadConfig.get_yandex_search_url()
    yandex_pictures_URL = ReadConfig.get_yandex_pictures_url()

    def test_yandex_pictures_section_exist(self, setup):
        self.logger.info("Checking if a Pictures section exists")
        self.driver = setup
        self.driver.get(self.yandex_search_URL)
        self.yp = YandexSearchPage(self.driver)
        try:
            self.yp.find_yandex_pictures_section()
            self.logger.info(
                f"Test passed: Pictures section found by {self.yp.pictures_section_locator}"
            )
        except TimeoutException:
            self.logger.error(
                f"Test failed: Pictures section not found by {self.yp.pictures_section_locator} for {self.yp.delay} seconds"
            )
        finally:
            self.driver.quit()
            self.logger.info("---------------")

    def test_yandex_pictures_page_exist(self, setup):
        self.logger.info("Checking yandex pictures page exist")
        self.driver = setup
        self.driver.get(self.yandex_search_URL)
        self.yp = YandexSearchPage(self.driver)
        try:
            pictures_section = self.yp.find_yandex_pictures_section()
            pictures_section.click()
            self.yp.find_yandex_pictures_title()
            self.logger.info(
                f"Test passed: successful transition to the https://yandex.ru/images/"
            )
        except TimeoutException:
            self.logger.error(
                f"Test failed: can't find picture title by {self.yp.pictures_title_locator} for {self.yp.delay} seconds"
            )
            assert False
        finally:
            self.driver.quit()
            self.logger.info("---------------")

    def test_yandex_pictures_first_category(self, setup):
        self.logger.info("Checking the correctness of image search")
        self.driver = setup
        self.driver.get(self.yandex_pictures_URL)
        self.yp = YandexPicturesPage(self.driver)
        try:
            first_popular_request = self.yp.find_first_popular_request()
            first_popular_request.click()
            expected_text = first_popular_request.text

            picture_search_box = self.yp.find_picture_search_box()
            actual_text = picture_search_box.get_attribute("value")
            if expected_text == actual_text:
                self.logger.info(
                    f"Test passed: the text of the first category matches with the search text"
                )
            else:
                self.logger.error(
                    f"Test failed: the text of the first category does not match the search text"
                )
        except TimeoutException:
            self.logger.error(
                f"Test failed: can't find element {self.yp.pictures_title_locator} for {self.yp.delay} seconds"
            )
            assert False
        finally:
            self.driver.quit()
            self.logger.info("---------------")