import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

class PageTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        # self.driver = webdriver.Chrome()

    def testPage(self):
        driver = self.driver
        driver.get('http://localhost:8080')
        self.assertIn('Блог', driver.title)
        elem = driver.find_element_by_id('reg')
        elem.click()
        self.assertIn('Регистрация', driver.title)
        elem = driver.find_element_by_name('firstName')
        elem.clear()
        elem.send_keys('Имя')
        elem = driver.find_element_by_name('lastName')
        elem.clear()
        elem.send_keys('Фамилия')
        elem = driver.find_element_by_name('userName')
        elem.clear()
        elem.send_keys('login')
        elem = driver.find_element_by_name('password')
        elem.clear()
        elem.send_keys('password')
        elem = driver.find_element_by_xpath("//button[@type='submit']")
        elem.click()
        self.assertIn('Авторизация', driver.title)
        elem = driver.find_element_by_name('userName')
        elem.clear()
        elem.send_keys('login')
        elem = driver.find_element_by_name('password')
        elem.clear()
        elem.send_keys('password')
        elem = driver.find_element_by_xpath("//button[@type='submit']")
        elem.click()
        self.assertIn('Редактор постов', driver.title)
        elem = driver.find_element_by_xpath("//a[@href='/create']")
        elem.click()
        self.assertIn('Создать пост', driver.title)
        elem = driver.find_element_by_name('title')
        elem.clear()
        elem.send_keys('Тестовый пост 1')
        elem = driver.find_element_by_name('text')
        elem.clear()
        elem.send_keys('Тестовый пост для автотестирования 1')
        elem = driver.find_element_by_name('sandbox')
        elem.click()
        elem = driver.find_element_by_xpath("//button[@type='submit']")
        elem.click()
        self.assertIn('Редактор постов', driver.title)
        elem = driver.find_element_by_xpath("//div[@class='title']")
        self.assertIn(elem.text, 'Тестовый пост 1')
        elem = driver.find_element_by_css_selector('div.btn.delete')
        elem.click()
        self.assertIn('Редактор постов', driver.title)
        try:
            elem = driver.find_element_by_xpath("//div[@class='title']")
        except NoSuchElementException:
            self.assertEqual('Delete post', 'Delete post')
        else:
            self.assertEqual('ERR Delete post', 'Delete post')
        elem = driver.find_element_by_xpath("//a[@href='/create']")
        elem.click()
        self.assertIn('Создать пост', driver.title)
        elem = driver.find_element_by_name('title')
        elem.clear()
        elem.send_keys('Тестовый пост 2')
        elem = driver.find_element_by_name('text')
        elem.clear()
        elem.send_keys('Тестовый пост для автотестирования 2')
        elem = driver.find_element_by_name('sandbox')
        elem.click()
        elem = driver.find_element_by_xpath("//button[@type='submit']")
        elem.click()
        self.assertIn('Редактор постов', driver.title)
        elem = driver.find_element_by_css_selector('div.btn.edit')
        elem.click()
        self.assertIn('Редактировать пост', driver.title)
        elem = driver.find_element_by_name('title')
        elem.send_keys(' + Редактирование')
        elem = driver.find_element_by_name('text')
        elem.send_keys(' + Редактирование')
        elem = driver.find_element_by_name('sandbox')
        elem.click()
        elem = driver.find_element_by_xpath("//button[@type='submit']")
        elem.click()
        self.assertIn('Редактор постов', driver.title)
        elem = driver.find_element_by_xpath("//div[@class='title']")
        self.assertIn(elem.text, 'Тестовый пост 2 + Редактирование')
        elem = driver.find_element_by_xpath("//a[@href='/logout']")
        elem.click()
        self.assertIn('Блог', driver.title)
        elem = driver.find_element_by_xpath("//div[@class='autor']")
        elem.click()
        self.assertIn('Блог Имя Фамилия', driver.title)
        elem = driver.find_element_by_xpath("//div[@class='title']")
        self.assertIn('Тестовый пост 2 + Редактирование', elem.text)
        elem = driver.find_element_by_css_selector('div.btn.read')
        elem.click()
        self.assertIn('Пост Имя Фамилия', driver.title)

    def tearDown(self):
        self.driver.close()

if __name__ == '__main__':
    unittest.main()
