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."

[((422, 431), 'selenium.webdriver.chrome.options.Options', 'Options', ([], {}), '()\n', (429, 431), False, 'from selenium.webdriver.chrome.options import Options\n'), ((1060, 1073), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (1070, 1073), False, 'import time\n'), ((2047, 2060), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (2057, 2060), False, 'import time\n'), ((2154, 2207), 'basic_function.find_element', 'find_element', (['self.driver', 'By.CLASS_NAME', '"""swal-text"""'], {}), "(self.driver, By.CLASS_NAME, 'swal-text')\n", (2166, 2207), False, 'from basic_function import basic_login, find_element\n'), ((2359, 2430), 'basic_function.find_element', 'find_element', (['self.driver', 'By.XPATH', '"""//button[normalize-space()=\'OK\']"""'], {}), '(self.driver, By.XPATH, "//button[normalize-space()=\'OK\']")\n', (2371, 2430), False, 'from basic_function import basic_login, find_element\n'), ((2554, 2567), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (2564, 2567), False, 'import time\n'), ((3340, 3353), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (3350, 3353), False, 'import time\n'), ((3427, 3440), 'time.sleep', 'time.sleep', (['(3)'], {}), '(3)\n', (3437, 3440), False, 'import time\n'), ((3709, 3722), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (3719, 3722), False, 'import time\n'), ((3848, 3872), 'basic_function.basic_login', 'basic_login', (['self.driver'], {}), '(self.driver)\n', (3859, 3872), False, 'from basic_function import basic_login, find_element\n'), ((3960, 3973), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (3970, 3973), False, 'import time\n'), ((4234, 4247), 'time.sleep', 'time.sleep', (['(3)'], {}), '(3)\n', (4244, 4247), False, 'import time\n'), ((4490, 4503), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (4500, 4503), False, 'import time\n'), ((4904, 4917), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (4914, 4917), False, 'import time\n'), ((5027, 5040), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (5037, 5040), False, 'import time\n'), ((5213, 5226), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (5223, 5226), False, 'import time\n'), ((5359, 5372), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (5369, 5372), False, 'import time\n'), ((5537, 5550), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (5547, 5550), False, 'import time\n'), ((5996, 6009), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (6006, 6009), False, 'import time\n'), ((6106, 6130), 'basic_function.basic_login', 'basic_login', (['self.driver'], {}), '(self.driver)\n', (6117, 6130), False, 'from basic_function import basic_login, find_element\n'), ((6225, 6238), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (6235, 6238), False, 'import time\n'), ((6485, 6498), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (6495, 6498), False, 'import time\n'), ((6616, 6640), 'basic_function.basic_login', 'basic_login', (['self.driver'], {}), '(self.driver)\n', (6627, 6640), False, 'from basic_function import basic_login, find_element\n'), ((6925, 6938), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (6935, 6938), False, 'import time\n'), ((7470, 7483), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (7480, 7483), False, 'import time\n'), ((7576, 7589), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (7586, 7589), False, 'import time\n'), ((7858, 7871), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (7868, 7871), False, 'import time\n'), ((8004, 8028), 'basic_function.basic_login', 'basic_login', (['self.driver'], {}), '(self.driver)\n', (8015, 8028), False, 'from basic_function import basic_login, find_element\n'), ((9660, 9684), 'basic_function.basic_login', 'basic_login', (['self.driver'], {}), '(self.driver)\n', (9671, 9684), False, 'from basic_function import basic_login, find_element\n'), ((9812, 9825), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (9822, 9825), False, 'import time\n'), ((9976, 9989), 'time.sleep', 'time.sleep', (['(3)'], {}), '(3)\n', (9986, 9989), False, 'import time\n'), ((10104, 10117), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (10114, 10117), False, 'import time\n'), ((10217, 10230), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (10227, 10230), False, 'import time\n'), ((10546, 10570), 'basic_function.basic_login', 'basic_login', (['self.driver'], {}), '(self.driver)\n', (10557, 10570), False, 'from basic_function import basic_login, find_element\n'), ((10665, 10678), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (10675, 10678), False, 'import time\n'), ((10760, 10797), 'os.path.exists', 'os.path.exists', (['"""my_last_article.txt"""'], {}), "('my_last_article.txt')\n", (10774, 10797), False, 'import os\n'), ((10918, 10931), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (10928, 10931), False, 'import time\n'), ((11204, 11217), 'time.sleep', 'time.sleep', (['(3)'], {}), '(3)\n', (11214, 11217), False, 'import time\n'), ((11709, 11733), 'basic_function.basic_login', 'basic_login', (['self.driver'], {}), '(self.driver)\n', (11720, 11733), False, 'from basic_function import basic_login, find_element\n'), ((11827, 11840), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (11837, 11840), False, 'import time\n'), ((11953, 11986), 'os.path.exists', 'os.path.exists', (['"""titles_list.csv"""'], {}), "('titles_list.csv')\n", (11967, 11986), False, 'import os\n'), ((12839, 12863), 'basic_function.basic_login', 'basic_login', (['self.driver'], {}), '(self.driver)\n', (12850, 12863), False, 'from basic_function import basic_login, find_element\n'), ((12957, 12970), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (12967, 12970), False, 'import time\n'), ((1400, 1413), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (1410, 1413), False, 'import time\n'), ((4372, 4411), 'csv.reader', 'csv.reader', (['article_file'], {'delimiter': '""";"""'}), "(article_file, delimiter=';')\n", (4382, 4411), False, 'import csv\n'), ((6802, 6841), 'csv.reader', 'csv.reader', (['article_file'], {'delimiter': '""";"""'}), "(article_file, delimiter=';')\n", (6812, 6841), False, 'import csv\n'), ((10811, 10843), 'os.remove', 'os.remove', (['"""my_last_article.txt"""'], {}), "('my_last_article.txt')\n", (10820, 10843), False, 'import os\n'), ((12000, 12028), 'os.remove', 'os.remove', (['"""titles_list.csv"""'], {}), "('titles_list.csv')\n", (12009, 12028), False, 'import os\n'), ((12434, 12472), 'csv.reader', 'csv.reader', (['csv_titles2'], {'delimiter': '""";"""'}), "(csv_titles2, delimiter=';')\n", (12444, 12472), False, 'import csv\n'), ((13240, 13253), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (13250, 13253), False, 'import time\n'), ((8141, 8180), 'csv.reader', 'csv.reader', (['article_file'], {'delimiter': '""";"""'}), "(article_file, delimiter=';')\n", (8151, 8180), False, 'import csv\n'), ((511, 532), 'webdriver_manager.chrome.ChromeDriverManager', 'ChromeDriverManager', ([], {}), '()\n', (530, 532), False, 'from webdriver_manager.chrome import ChromeDriverManager\n'), ((8419, 8432), 'time.sleep', 'time.sleep', (['(4)'], {}), '(4)\n', (8429, 8432), False, 'import time\n'), ((9067, 9080), 'time.sleep', 'time.sleep', (['(1)'], {}), '(1)\n', (9077, 9080), False, 'import time\n'), ((9197, 9210), 'time.sleep', 'time.sleep', (['(2)'], {}), '(2)\n', (9207, 9210), False, 'import time\n'), ((9539, 9552), 'time.sleep', 'time.sleep', (['(4)'], {}), '(4)\n', (9549, 9552), False, 'import time\n')]