from selenium import webdriver
import unittest
import time

class NewVisitorTest(unittest.TestCase):
	def setUp(self):
		self.browser = webdriver.Firefox()
		
	def tearDown(self):
		self.browser.quit()

	def test_can_display_a_homepage_and_other_pages(self):
		# A customer has heard about a new frozen yogurt specialty shop called The Good Place FroYo Shop.
		# He/She goes to check out its homepage
		
		self.browser.get('http://localhost:8000/home')
		
		# He/She notices the page title and header mention
		# 'The Good Place FroYo Shop'
		
		self.assertIn('The Good Place FroYo Shop Home Page', self.browser.title)
		
		# He/She sees a list containing ingredients, recipes, and orders
	
		Ingredients = self.browser.find_element_by_id('Ingredients')
		Recipes = self.browser.find_element_by_id('Recipes')
		Orders = self.browser.find_element_by_id('Orders')

		# When he/she selects ingredients/recipes/orders on the list, 
		# he/she is sent to another page containing more information about 
		# ingredients/recipes/orders (detail, update, create)
		Ingredients = self.browser.find_element_by_id('Ingredients')
		Ingredients.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/ingredients/', self.browser.current_url)
		self.assertIn('Ingredients - List', self.browser.title)
		
		# Goes to ingredients detail
		ingredientsDetail = self.browser.find_element_by_id('ingredientsDetail')
		ingredientsDetail.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/ingredients/detail/', self.browser.current_url)
		self.assertIn('Ingredients - Detail', self.browser.title)
		
		# Goes back to ingredients list
		ingredientsDetailToList = self.browser.find_element_by_id('ingredientsDetailToList')
		self.assertEqual(ingredientsDetailToList.get_attribute('innerHTML'), 'Back to Ingredients List')
		ingredientsDetailToList.click()
		time.sleep(1)
		
		# Goes to ingredients update
		ingredientsUpdate = self.browser.find_element_by_id('ingredientsUpdate')
		ingredientsUpdate.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/ingredients/update/', self.browser.current_url)
		self.assertIn('Ingredients - Update', self.browser.title)
		
		# Goes back to ingredients list
		ingredientsUpdateToList = self.browser.find_element_by_id('ingredientsUpdateToList')
		self.assertEqual(ingredientsUpdateToList.get_attribute('innerHTML'), 'Back to Ingredients List')
		ingredientsUpdateToList.click()
		time.sleep(1)
		
		# Goes to ingredients create
		ingredientsCreate = self.browser.find_element_by_id('ingredientsCreate')
		ingredientsCreate.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/ingredients/create/', self.browser.current_url)
		self.assertIn('Ingredients - Create', self.browser.title)
		
		# Goes back to ingredients list
		ingredientsCreateToList = self.browser.find_element_by_id('ingredientsCreateToList')
		self.assertEqual(ingredientsCreateToList.get_attribute('innerHTML'), 'Back to Ingredients List')
		ingredientsCreateToList.click()
		time.sleep(1)
		
		# Goes back to froyo home page
		ingredientsListToHome = self.browser.find_element_by_id('ingredientsListToHome')
		self.assertEqual(ingredientsListToHome.get_attribute('innerHTML'), 'Back to Froyo Home Page')
		ingredientsListToHome.click()
		time.sleep(1)
		
		# Goes to recipes list
		Recipes = self.browser.find_element_by_id('Recipes')
		Recipes.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/recipes/', self.browser.current_url)
		self.assertIn('Recipes - List', self.browser.title)
		
		# Goes to recipes detail
		recipesDetail = self.browser.find_element_by_id('recipesDetail')
		recipesDetail.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/recipes/detail/', self.browser.current_url)
		self.assertIn('Recipes - Detail', self.browser.title)
		
		# Goes back to recipes list
		recipesDetailToList = self.browser.find_element_by_id('recipesDetailToList')
		self.assertEqual(recipesDetailToList.get_attribute('innerHTML'), 'Back to Recipes List')
		recipesDetailToList.click()
		time.sleep(1)
		
		# Goes to recipes update
		recipesUpdate = self.browser.find_element_by_id('recipesUpdate')
		recipesUpdate.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/recipes/update/', self.browser.current_url)
		self.assertIn('Recipes - Update', self.browser.title)
		
		# Goes back to recipes list
		recipesUpdateToList = self.browser.find_element_by_id('recipesUpdateToList')
		self.assertEqual(recipesUpdateToList.get_attribute('innerHTML'), 'Back to Recipes List')
		recipesUpdateToList.click()
		time.sleep(1)
		
		# Goes to recipes create
		recipesCreate = self.browser.find_element_by_id('recipesCreate')
		recipesCreate.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/recipes/create/', self.browser.current_url)
		self.assertIn('Recipes - Create', self.browser.title)
		
		# Goes back to recipes list
		recipesCreateToList = self.browser.find_element_by_id('recipesCreateToList')
		self.assertEqual(recipesCreateToList.get_attribute('innerHTML'), 'Back to Recipes List')
		recipesCreateToList.click()
		time.sleep(1)
		
		# Goes back to froyo home page
		recipesListToHome = self.browser.find_element_by_id('recipesListToHome')
		self.assertEqual(recipesListToHome.get_attribute('innerHTML'), 'Back to Froyo Home Page')
		recipesListToHome.click()
		time.sleep(1)
		
		# Goes to orders list
		Orders = self.browser.find_element_by_id('Orders')
		Orders.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/orders/', self.browser.current_url)
		self.assertIn('Orders - List', self.browser.title)
		
		# Goes to orders detail
		ordersDetail = self.browser.find_element_by_id('ordersDetail')
		ordersDetail.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/orders/detail/', self.browser.current_url)
		self.assertIn('Orders - Detail', self.browser.title)
		
		# Goes back to orders list
		ordersDetailToList = self.browser.find_element_by_id('ordersDetailToList')
		self.assertEqual(ordersDetailToList.get_attribute('innerHTML'), 'Back to Orders List')
		ordersDetailToList.click()
		time.sleep(1)
		
		# Goes to orders update
		ordersUpdate = self.browser.find_element_by_id('ordersUpdate')
		ordersUpdate.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/orders/update/', self.browser.current_url)
		self.assertIn('Orders - Update', self.browser.title)
		
		# Goes back to orders list
		ordersUpdateToList = self.browser.find_element_by_id('ordersUpdateToList')
		self.assertEqual(ordersUpdateToList.get_attribute('innerHTML'), 'Back to Orders List')
		ordersUpdateToList.click()
		time.sleep(1)
		
		# Goes to orders create
		ordersCreate = self.browser.find_element_by_id('ordersCreate')
		ordersCreate.click()
		time.sleep(1)
		self.assertEqual('http://localhost:8000/home/orders/create/', self.browser.current_url)
		self.assertIn('Orders - Create', self.browser.title)
		
		# Goes back to orders list
		ordersCreateToList = self.browser.find_element_by_id('ordersCreateToList')
		self.assertEqual(ordersCreateToList.get_attribute('innerHTML'), 'Back to Orders List')
		ordersCreateToList.click()
		time.sleep(1)

		# Goes back to froyo home page
		ordersListToHome = self.browser.find_element_by_id('ordersListToHome')
		self.assertEqual(ordersListToHome.get_attribute('innerHTML'), 'Back to Froyo Home Page')
		ordersListToHome.click()
		time.sleep(1)
		
		self.fail('Finish the test!')
		
if __name__ == '__main__':
	unittest.main(warnings='ignore')