from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
import time


class UserTest(unittest.TestCase):
    def setUp(self):
        self.browser = webdriver.Firefox()

    def tearDown(self):
        self.browser.quit()

# Usecase: a user wants to check out one of the pages.
# this test covers all the page links as of the current version of thegoodplace
    def test_can_walk_and_display_all_pages(self):
        self.browser.get('http://localhost:8000/')

        self.assertIn('Home Directory', self.browser.title)
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Ingredients - List").click()
        time.sleep(0.2)
        self.assertIn('Ingredients - List', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Ingredients - Update").click()
        time.sleep(0.2)
        self.assertIn('Ingredients - Update', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Ingredients - Create").click()
        time.sleep(0.2)
        self.assertIn('Ingredients - Create', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Ingredients - Detail").click()
        time.sleep(0.2)
        self.assertIn('Ingredients - Detail', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Orders - List").click()
        time.sleep(0.2)
        self.assertIn('Orders - List', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Orders - Update").click()
        time.sleep(0.2)
        self.assertIn('Orders - Update', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Orders - Create").click()
        time.sleep(0.2)
        self.assertIn('Orders - Create', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Orders - Detail").click()
        time.sleep(0.2)
        self.assertIn('Orders - Detail', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Recipes - List").click()
        time.sleep(0.2)
        self.assertIn('Recipes - List', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Recipes - Update").click()
        time.sleep(0.2)
        self.assertIn('Recipes - Update', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Recipes - Create").click()
        time.sleep(0.2)
        self.assertIn('Recipes - Create', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.browser.find_element_by_link_text("Recipes - Detail").click()
        time.sleep(0.2)
        self.assertIn('Recipes - Detail', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)

        self.assertIn('Home Directory', self.browser.title)
        self.fail("Page Walking Test is passed")

# Usecase: A user wants to use the update and create functionality of the ingredients models.
    def test_create_and_update_pages_for_ingredients_work(self):
        self.browser.get('http://localhost:8000/')
        self.assertIn('Home Directory', self.browser.title)
        time.sleep(0.2)
        # Ingredients Create starts here
        self.browser.find_element_by_link_text("Ingredients - Create").click()
        time.sleep(0.2)
        self.assertIn('Ingredients - Create', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('ingredient')
        element.send_keys("lorem ipsum ingredients")
        self.assertIn('lorem ipsum ingredients', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('qty')
        element.send_keys("lorem ipsum qty")
        self.assertIn('lorem ipsum qty', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("create").click()
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)
        # Ingredients Update starts here
        self.browser.find_element_by_link_text("Ingredients - Update").click()
        time.sleep(0.2)
        self.assertIn('Ingredients - Update', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('ingredient')
        element.clear()
        element.send_keys("lorem ipsum ingredients")
        self.assertIn('lorem ipsum ingredients', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('qty')
        element.clear()
        element.send_keys("lorem ipsum qty")
        self.assertIn('lorem ipsum qty', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("update").click()
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)
        self.fail("Ingredients Update and Create Test is passed")

# Usecase: A user wants to use the update and create functionality of the orders models.
    def test_create_and_update_pages_for_orders_work(self):
        self.browser.get('http://localhost:8000/')
        self.assertIn('Home Directory', self.browser.title)
        time.sleep(0.2)
        # Orders Create starts here
        self.browser.find_element_by_link_text("Orders - Create").click()
        time.sleep(0.2)
        self.assertIn('Orders - Create', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('customer')
        element.send_keys("lorem ipsum customer")
        self.assertIn('lorem ipsum customer', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('size')
        element.send_keys("lorem ipsum size")
        self.assertIn('lorem ipsum size', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('base-recipe')
        element.send_keys("lorem ipsum base-recipe")
        self.assertIn('lorem ipsum base-recipe', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('customization')
        element.send_keys(
            "lorem ipsum customization1", Keys.ENTER, "lorem ipsum customization2")
        self.assertIn('lorem ipsum customization1',
                      self.browser.page_source)
        self.assertIn('lorem ipsum customization2',
                      self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("create").click()
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)
        # Orders Update starts here
        self.browser.find_element_by_link_text("Orders - Update").click()
        time.sleep(0.2)
        self.assertIn('Orders - Update', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('customer')
        element.clear()
        element.send_keys("lorem ipsum customer")
        self.assertIn('lorem ipsum customer', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('size')
        element.clear()
        element.send_keys("lorem ipsum size")
        self.assertIn('lorem ipsum size', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('base-recipe')
        element.clear()
        element.send_keys("lorem ipsum base-recipe", )
        self.assertIn('lorem ipsum base-recipe', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('customization')
        element.clear()
        element.send_keys(
            "lorem ipsum customization1", Keys.ENTER, "lorem ipsum customization2")
        self.assertIn('lorem ipsum customization1',
                      self.browser.page_source)
        self.assertIn('lorem ipsum customization2',
                      self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("update").click()
        time.sleep(0.2)
        self.fail("Orders Update and Create Test is passed")

# Usecase: A user wants to use the update and create functionality of the recipes models.
    def test_create_and_update_pages_for_recipes_work(self):
        self.browser.get('http://localhost:8000/')
        self.assertIn('Home Directory', self.browser.title)
        time.sleep(0.2)
        # Recipes Create Starts Here
        self.browser.find_element_by_link_text("Recipes - Create").click()
        time.sleep(0.2)
        self.assertIn('Recipes - Create', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('ingredient')
        element.send_keys("lorem ipsum ingredients")
        self.assertIn('lorem ipsum ingredients', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('qty')
        element.send_keys("lorem ipsum qty")
        self.assertIn('lorem ipsum qty', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("create").click()
        time.sleep(0.2)
        self.browser.find_element_by_link_text("Go Home").click()
        time.sleep(0.2)
        # Recipes Update Starts Here
        self.browser.find_element_by_link_text("Recipes - Update").click()
        time.sleep(0.2)
        self.assertIn('Recipes - Update', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('ingredient')
        element.clear()
        element.send_keys("lorem ipsum ingredients")
        self.assertIn('lorem ipsum ingredients', self.browser.page_source)
        time.sleep(0.2)
        element = self.browser.find_element_by_name('qty')
        element.clear()
        element.send_keys("lorem ipsum qty")
        self.assertIn('lorem ipsum qty', self.browser.page_source)
        time.sleep(0.2)
        self.browser.find_element_by_link_text("update").click()
        time.sleep(0.2)
        self.fail("Recipes Update and Create Test is passed")


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