from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import os
import time
import csv
from webdriver_manager.chrome import ChromeDriverManager
import math
from basic_function import basic_login, find_element


class TestConduit(object):
    def setup(self):
        browser_options = Options()
        browser_options.headless = True
        self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=browser_options)
        self.driver.get("http://localhost:1667/#/")

    def teardown(self):
        self.driver.quit()

    # -------- A028, TC-0037 Cookie kezelési tájékoztató --------
    def test_cookie_process(self):

        assert self.driver.find_element_by_id("cookie-policy-panel").is_displayed()

        # Cookie-k elfogadása folyamat
        self.driver.find_element_by_xpath(
            "//button[@class='cookie__bar__buttons__button cookie__bar__buttons__button--accept']").click()

        time.sleep(2)

        # # Cookie-k elutasítása folyamat
        # self.driver.find_element_by_xpath(
        #     "//button[@class='cookie__bar__buttons__button cookie__bar__buttons__button--decline']").click()
        #
        # time.sleep(2)

        try:
            self.driver.find_element_by_id("cookie-policy-panel")

            time.sleep(2)

        except NoSuchElementException:
            return True
        return False

    # -------- A002, TC-0002 Regisztráció helyes adatokkal --------
    def test_registration_process(self):

        user_input_data = ["user200", "user200@hotmail.com", "Userpass1"]

        self.driver.find_element_by_xpath("//a[@href='#/register']").click()

        # Beviteli mezők feltöltése a random user adatokkal
        for i in range(len(user_input_data)):
            self.driver.find_element_by_xpath(f"//fieldset[{i + 1}]/input").send_keys(user_input_data[i])
        self.driver.find_element_by_tag_name("button").click()

        time.sleep(2)

        # Sikeres regisztrációs értesítési ablak szövegének ellenőrzése
        swal_text = find_element(self.driver, By.CLASS_NAME, "swal-text")
        assert swal_text.text == "Your registration was successful!"

        # time.sleep(2)

        # Értesítési ablak bezárása
        close_btn = find_element(self.driver, By.XPATH, "//button[normalize-space()='OK']")
        close_btn.click()
        # self.driver.find_element_by_xpath("//button[normalize-space()='OK']").click()

        time.sleep(1)

        # Bejelentkezés tényének ellenőrzése
        username_check = self.driver.find_element_by_xpath("//a[starts-with(@href, '#/@')]").text
        assert username_check == user_input_data[
            0], f"Test Failed: Username did not match expected ({user_input_data[0]})."

        # time.sleep(2)

    # -------- A004, TC-0010 Bejelentkezés helyes adatokkal --------
    def test_login_process(self):
        user_input_data = ["user200", "user200@hotmail.com", "Userpass1"]

        self.driver.find_element_by_xpath("//a[@href='#/login']").click()

        # Bejelentkezési űrlap feltöltése
        for i in range(len(user_input_data) - 1):
            self.driver.find_element_by_xpath(f"//fieldset[{i + 1}]/input").send_keys(user_input_data[i + 1])

        time.sleep(1)

        self.driver.find_element_by_tag_name("button").click()

        time.sleep(3)

        # Bejelentkezés tényének ellenőrzése
        username_check = self.driver.find_element_by_xpath("//a[starts-with(@href, '#/@')]").text
        assert username_check == user_input_data[0], f"Test Failed: User is not logged in ({user_input_data[0]})."

        time.sleep(2)

    # -------- A010, TC-0034 Saját profil szerkesztése, képcsere --------
    def test_edit_settings_process(self):
        basic_login(self.driver)

        self.driver.find_element_by_xpath("//a[@href='#/settings']").click()

        time.sleep(2)

        # Your Settings oldal megjelenésének ellenőrzése
        settings_check = self.driver.find_element_by_tag_name("h1").text
        assert settings_check == "Your Settings", f"Test Failed: Page names did not match expected ({settings_check})."

        time.sleep(3)

        # Beolvassuk az előkészített adatokat
        with open('edit_user.csv') as article_file:
            csv_reader = csv.reader(article_file, delimiter=';')
            for row in csv_reader:
                user_update = row

        time.sleep(2)

        # Feltöltjük az adatokkal a beviteli űrlap egyes sorait
        user_picture = self.driver.find_element_by_class_name("form-control")
        user_bio = self.driver.find_element_by_xpath("//textarea[@placeholder='Short bio about you']")

        user_picture.clear()
        user_picture.send_keys(user_update[0])
        user_bio.clear()
        user_bio.send_keys(user_update[1])

        time.sleep(1)

        self.driver.find_element_by_xpath("//button[normalize-space()='Update Settings']").click()

        time.sleep(2)

        # Sikeres update értesítési ablak szövegének ellenőrzése
        assert self.driver.find_element_by_class_name("swal-title").text == "Update successful!"

        time.sleep(2)

        # Értesítési ablak bezárása
        self.driver.find_element_by_xpath("//button[normalize-space()='OK']").click()

        time.sleep(1)

        # Ellenőrizzük a felhasználó profiljában történt változásokat
        self.driver.find_element_by_xpath("//a[starts-with(@href, '#/@')]").click()

        time.sleep(2)

        img_check = self.driver.find_element_by_class_name("user-img").get_attribute("src")
        assert img_check == user_update[
            0], f"Test Failed: Image did not match expected ({user_update[0]})."
        bio_check = self.driver.find_element_by_css_selector("div[class='user-info'] p").text
        assert bio_check == user_update[
            1], f"Test Failed: User's bio did not match expected ({user_update[1]})."

        time.sleep(2)

    # -------- A005, TC-0003 Kijelentkezés --------
    def test_logout_process(self):
        basic_login(self.driver)

        self.driver.find_element_by_xpath("//i[@class='ion-android-exit']").click()

        time.sleep(2)

        # Kijelentkezés tényének ellenőrzése
        sign_out_check = self.driver.find_element_by_xpath("//a[starts-with(@href, '#/login')]").text
        assert sign_out_check == f"{sign_out_check}", f"Test Failed: User is logged in."

        time.sleep(1)

    # -------- A006, TC-0015 Új poszt létrehozása helyes adatokkal --------
    def test_create_post(self):
        basic_login(self.driver)

        self.driver.find_element_by_xpath("//a[@href='#/editor']").click()

        with open('new_post_content.csv') as article_file:
            csv_reader = csv.reader(article_file, delimiter=';')
            for row in csv_reader:
                new_article_data = row

        time.sleep(2)

        # Beviteli űrlap feltöltése
        self.driver.find_element_by_xpath("//input[starts-with(@placeholder,'Article')]").send_keys(new_article_data[0])
        self.driver.find_element_by_xpath("//input[starts-with(@placeholder,'What')]").send_keys(new_article_data[1])
        self.driver.find_element_by_xpath("//textarea[starts-with(@placeholder,'Write')]").send_keys(
            new_article_data[2])
        self.driver.find_element_by_xpath("//input[@placeholder='Enter tags']").send_keys(new_article_data[3])

        time.sleep(1)

        self.driver.find_element_by_css_selector("button[type='submit']").click()

        time.sleep(2)

        # Bejegyzés létrejöttének ellenőrzése
        title_check = self.driver.find_element_by_tag_name("h1").text
        assert title_check == new_article_data[
            0], f"Test Failed: Content title did not match expected ({new_article_data[0]})."

        time.sleep(2)

    # -------- A006, TC-0015 Új adatbevitel helyes adatokkal (sorozatos) --------
    def test_create_posts_process(self):
        basic_login(self.driver)

        for i in range(1):
            with open('contents.csv') as article_file:
                csv_reader = csv.reader(article_file, delimiter=';')
                for row in csv_reader:
                    new_article_data = row

                    # Beviteli űrlap feltöltése
                    self.driver.find_element_by_xpath("//a[@href='#/editor']").click()
                    time.sleep(4)
                    self.driver.find_element_by_xpath("//input[@placeholder='Article Title']").send_keys(
                        new_article_data[0])
                    self.driver.find_element_by_xpath("//input[starts-with(@placeholder,'What')]").send_keys(
                        new_article_data[1])
                    self.driver.find_element_by_xpath("//textarea[starts-with(@placeholder,'Write')]").send_keys(
                        new_article_data[2])
                    self.driver.find_element_by_xpath("//input[@placeholder='Enter tags']").send_keys(
                        new_article_data[3])

                    time.sleep(1)

                    self.driver.find_element_by_css_selector("button[type='submit']").click()

                    time.sleep(2)

                    # Bejegyzés létrejöttének ellenőrzése
                    title_check = self.driver.find_element_by_tag_name("h1").text
                    assert title_check == new_article_data[
                        0], f"Test Failed: Content title did not match expected ({new_article_data[0]})."

                    time.sleep(4)

    # -------- A015, TC-0024 Saját poszt törlése --------
    def test_delete_post_process(self):
        basic_login(self.driver)

        my_articles = self.driver.find_element_by_xpath("//a[starts-with(@href, '#/@')]")
        my_articles.click()
        time.sleep(2)

        articles_list = self.driver.find_elements_by_tag_name("h1")
        if len(articles_list) > 0:
            articles_list[0].click()

        time.sleep(3)

        self.driver.find_element_by_xpath("//*[@id='app']/div/div[1]/div/div/span/button/span").click()

        time.sleep(2)

        # Ellenőrizzük, hogy valóban törlődött-e a bejegyzés
        my_articles.click()

        time.sleep(2)

        new_articles_list = self.driver.find_elements_by_tag_name("h1")

        assert not new_articles_list[0] == articles_list[
            0], f"Test Failed: Content is not deleted ({articles_list[0]})."

    # -------- A029 Adatok lementése felületről --------
    def test_export_my_last_post(self):
        basic_login(self.driver)

        self.driver.find_element_by_xpath("//a[starts-with(@href, '#/@')]").click()

        time.sleep(2)

        articles_list = self.driver.find_elements_by_tag_name("h1")

        if os.path.exists("my_last_article.txt"):
            os.remove("my_last_article.txt")
        else:
            pass

        articles_list[0].click()

        time.sleep(2)

        article_title = self.driver.find_element_by_tag_name("h1").text
        article_text = self.driver.find_element_by_tag_name("p").text
        with open("my_last_article.txt", "a") as my_txt:
            my_txt.write(f"{article_title};{article_text};\n")

        time.sleep(3)

        # a kiírt tartalom ellenőrzése
        with open("my_last_article.txt", "r") as my_txt2:
            my_txt = my_txt2.readline()
            my_txt_list = my_txt.split(";")

        assert my_txt_list[0] == article_title, f"Test Failed: Content title is not exported."
        assert my_txt_list[1] == article_text, f"Test Failed: Content text is not exported."

    # -------- A007, TC-0025 Bejegyzések listájának megtekintése --------
    def test_global_feed_list(self):
        basic_login(self.driver)

        self.driver.find_element_by_xpath("//a[starts-with(@href, '#/')]").click()

        time.sleep(2)

        articles_list = self.driver.find_elements_by_xpath("//div[@class='article-preview']/a/h1")

        if os.path.exists("titles_list.csv"):
            os.remove("titles_list.csv")
        else:
            pass

        for i in range(len(articles_list)):
            article_title = articles_list[i].text
            with open('titles_list.csv', 'a', encoding="utf-8") as csv_titles:
                csv_titles.write(f"{article_title};")

        # a lista hosszának ellenőrzése
        with open('titles_list.csv', 'r', encoding="utf-8") as csv_titles2:
            check_articles = csv.reader(csv_titles2, delimiter=';')
            for row in check_articles:
                check_articles_list = row

        assert len(articles_list) == len(
            check_articles_list) - 1, f"Test Failed: The length of the lists are not exactly the same."

    # -------- A007, TC-0025 Bejegyzések listájának megtekintése (lapozóval) --------
    def test_global_feed_pagination(self):
        basic_login(self.driver)

        self.driver.find_element_by_xpath("//a[starts-with(@href, '#/')]").click()

        time.sleep(2)

        articles_list = self.driver.find_elements_by_xpath("//div[@class='article-preview']/a/h1")

        # lapozógombok használata
        pages = self.driver.find_elements_by_class_name("page-link")

        for page in pages:
            page.click()
            time.sleep(1)

        # Az oldal bejárásának ellenőrzése
        assert len(pages) == int(math.ceil(
            len(articles_list) / 10)), f"Test Failed: The length of the list and pagination not exactly the same."

[((15, 26, 15, 35), 'selenium.webdriver.chrome.options.Options', 'Options', ({}, {}), '()', False, 'from selenium.webdriver.chrome.options import Options\n'), ((32, 8, 32, 21), 'time.sleep', 'time.sleep', ({(32, 19, 32, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((61, 8, 61, 21), 'time.sleep', 'time.sleep', ({(61, 19, 61, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((64, 20, 64, 73), 'basic_function.find_element', 'find_element', ({(64, 33, 64, 44): 'self.driver', (64, 46, 64, 59): 'By.CLASS_NAME', (64, 61, 64, 72): '"""swal-text"""'}, {}), "(self.driver, By.CLASS_NAME, 'swal-text')", False, 'from basic_function import basic_login, find_element\n'), ((70, 20, 70, 91), 'basic_function.find_element', 'find_element', ({(70, 33, 70, 44): 'self.driver', (70, 46, 70, 54): 'By.XPATH', (70, 56, 70, 90): '"""//button[normalize-space()=\'OK\']"""'}, {}), '(self.driver, By.XPATH, "//button[normalize-space()=\'OK\']")', False, 'from basic_function import basic_login, find_element\n'), ((74, 8, 74, 21), 'time.sleep', 'time.sleep', ({(74, 19, 74, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((93, 8, 93, 21), 'time.sleep', 'time.sleep', ({(93, 19, 93, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((97, 8, 97, 21), 'time.sleep', 'time.sleep', ({(97, 19, 97, 20): '(3)'}, {}), '(3)', False, 'import time\n'), ((103, 8, 103, 21), 'time.sleep', 'time.sleep', ({(103, 19, 103, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((107, 8, 107, 32), 'basic_function.basic_login', 'basic_login', ({(107, 20, 107, 31): 'self.driver'}, {}), '(self.driver)', False, 'from basic_function import basic_login, find_element\n'), ((111, 8, 111, 21), 'time.sleep', 'time.sleep', ({(111, 19, 111, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((117, 8, 117, 21), 'time.sleep', 'time.sleep', ({(117, 19, 117, 20): '(3)'}, {}), '(3)', False, 'import time\n'), ((125, 8, 125, 21), 'time.sleep', 'time.sleep', ({(125, 19, 125, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((136, 8, 136, 21), 'time.sleep', 'time.sleep', ({(136, 19, 136, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((140, 8, 140, 21), 'time.sleep', 'time.sleep', ({(140, 19, 140, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((145, 8, 145, 21), 'time.sleep', 'time.sleep', ({(145, 19, 145, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((150, 8, 150, 21), 'time.sleep', 'time.sleep', ({(150, 19, 150, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((155, 8, 155, 21), 'time.sleep', 'time.sleep', ({(155, 19, 155, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((164, 8, 164, 21), 'time.sleep', 'time.sleep', ({(164, 19, 164, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((168, 8, 168, 32), 'basic_function.basic_login', 'basic_login', ({(168, 20, 168, 31): 'self.driver'}, {}), '(self.driver)', False, 'from basic_function import basic_login, find_element\n'), ((172, 8, 172, 21), 'time.sleep', 'time.sleep', ({(172, 19, 172, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((178, 8, 178, 21), 'time.sleep', 'time.sleep', ({(178, 19, 178, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((182, 8, 182, 32), 'basic_function.basic_login', 'basic_login', ({(182, 20, 182, 31): 'self.driver'}, {}), '(self.driver)', False, 'from basic_function import basic_login, find_element\n'), ((191, 8, 191, 21), 'time.sleep', 'time.sleep', ({(191, 19, 191, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((200, 8, 200, 21), 'time.sleep', 'time.sleep', ({(200, 19, 200, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((204, 8, 204, 21), 'time.sleep', 'time.sleep', ({(204, 19, 204, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((211, 8, 211, 21), 'time.sleep', 'time.sleep', ({(211, 19, 211, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((215, 8, 215, 32), 'basic_function.basic_login', 'basic_login', ({(215, 20, 215, 31): 'self.driver'}, {}), '(self.driver)', False, 'from basic_function import basic_login, find_element\n'), ((250, 8, 250, 32), 'basic_function.basic_login', 'basic_login', ({(250, 20, 250, 31): 'self.driver'}, {}), '(self.driver)', False, 'from basic_function import basic_login, find_element\n'), ((254, 8, 254, 21), 'time.sleep', 'time.sleep', ({(254, 19, 254, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((260, 8, 260, 21), 'time.sleep', 'time.sleep', ({(260, 19, 260, 20): '(3)'}, {}), '(3)', False, 'import time\n'), ((264, 8, 264, 21), 'time.sleep', 'time.sleep', ({(264, 19, 264, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((269, 8, 269, 21), 'time.sleep', 'time.sleep', ({(269, 19, 269, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((278, 8, 278, 32), 'basic_function.basic_login', 'basic_login', ({(278, 20, 278, 31): 'self.driver'}, {}), '(self.driver)', False, 'from basic_function import basic_login, find_element\n'), ((282, 8, 282, 21), 'time.sleep', 'time.sleep', ({(282, 19, 282, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((286, 11, 286, 48), 'os.path.exists', 'os.path.exists', ({(286, 26, 286, 47): '"""my_last_article.txt"""'}, {}), "('my_last_article.txt')", False, 'import os\n'), ((293, 8, 293, 21), 'time.sleep', 'time.sleep', ({(293, 19, 293, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((300, 8, 300, 21), 'time.sleep', 'time.sleep', ({(300, 19, 300, 20): '(3)'}, {}), '(3)', False, 'import time\n'), ((312, 8, 312, 32), 'basic_function.basic_login', 'basic_login', ({(312, 20, 312, 31): 'self.driver'}, {}), '(self.driver)', False, 'from basic_function import basic_login, find_element\n'), ((316, 8, 316, 21), 'time.sleep', 'time.sleep', ({(316, 19, 316, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((320, 11, 320, 44), 'os.path.exists', 'os.path.exists', ({(320, 26, 320, 43): '"""titles_list.csv"""'}, {}), "('titles_list.csv')", False, 'import os\n'), ((341, 8, 341, 32), 'basic_function.basic_login', 'basic_login', ({(341, 20, 341, 31): 'self.driver'}, {}), '(self.driver)', False, 'from basic_function import basic_login, find_element\n'), ((345, 8, 345, 21), 'time.sleep', 'time.sleep', ({(345, 19, 345, 20): '(2)'}, {}), '(2)', False, 'import time\n'), ((43, 12, 43, 25), 'time.sleep', 'time.sleep', ({(43, 23, 43, 24): '(2)'}, {}), '(2)', False, 'import time\n'), ((121, 25, 121, 64), 'csv.reader', 'csv.reader', (), '', False, 'import csv\n'), ((187, 25, 187, 64), 'csv.reader', 'csv.reader', (), '', False, 'import csv\n'), ((287, 12, 287, 44), 'os.remove', 'os.remove', ({(287, 22, 287, 43): '"""my_last_article.txt"""'}, {}), "('my_last_article.txt')", False, 'import os\n'), ((321, 12, 321, 40), 'os.remove', 'os.remove', ({(321, 22, 321, 39): '"""titles_list.csv"""'}, {}), "('titles_list.csv')", False, 'import os\n'), ((332, 29, 332, 67), 'csv.reader', 'csv.reader', (), '', False, 'import csv\n'), ((354, 12, 354, 25), 'time.sleep', 'time.sleep', ({(354, 23, 354, 24): '(1)'}, {}), '(1)', False, 'import time\n'), ((219, 29, 219, 68), 'csv.reader', 'csv.reader', (), '', False, 'import csv\n'), ((17, 39, 17, 60), 'webdriver_manager.chrome.ChromeDriverManager', 'ChromeDriverManager', ({}, {}), '()', False, 'from webdriver_manager.chrome import ChromeDriverManager\n'), ((225, 20, 225, 33), 'time.sleep', 'time.sleep', ({(225, 31, 225, 32): '(4)'}, {}), '(4)', False, 'import time\n'), ((235, 20, 235, 33), 'time.sleep', 'time.sleep', ({(235, 31, 235, 32): '(1)'}, {}), '(1)', False, 'import time\n'), ((239, 20, 239, 33), 'time.sleep', 'time.sleep', ({(239, 31, 239, 32): '(2)'}, {}), '(2)', False, 'import time\n'), ((246, 20, 246, 33), 'time.sleep', 'time.sleep', ({(246, 31, 246, 32): '(4)'}, {}), '(4)', False, 'import time\n')]