from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class NewVisitorTest(LiveServerTestCase):

    def setUp(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(3)

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

    def check_for_row_in_list_table(self, row_text):
        table = self.browser.find_element_by_id('id_list_table')
        rows = table.find_elements_by_tag_name('tr')
        self.assertIn(row_text, [row.text for row in rows])

    def test_can_start_a_list_and_retrieve_it_later(self):

        # User goes to homepage.
        self.browser.get(self.live_server_url)

        # Page title and header mention to-do lists.
        self.assertIn('To-Do', self.browser.title)
        header_text = self.browser.find_element_by_tag_name('h1').text
        self.assertIn('To-Do', header_text)

        # User is invited to enter a to-do item straight away.
        inputbox = self.browser.find_element_by_id('id_new_item')
        self.assertEqual(
            inputbox.get_attribute('placeholder'),
            'Enter a to-do item'
        )

        # User types "Buy peacock feathers" into a text box.
        inputbox.send_keys('Buy peacock feathers')

        # When user hits enter, the page updates, and now the page lists
        # "1. Buy peacock feather" as an item in a to-do list table.
        inputbox.send_keys(Keys.ENTER)
        self.check_for_row_in_list_table('1: Buy peacock feathers')

        # There is still a box inviting the user to add another item. The user
        # enters "Use peacock feathers to make a fly".
        inputbox = self.browser.find_element_by_id('id_new_item')
        inputbox.send_keys('Use peacock feathers to make a fly')
        inputbox.send_keys(Keys.ENTER)

        # The page updates again, and now shows both items on her list.
        self.check_for_row_in_list_table('1: Buy peacock feathers')
        self.check_for_row_in_list_table('2: Use peacock feathers to make ' +
                                         'a fly')

        # The site has generated a unique URL for the user -- there is some
        # text to explain this.
        self.fail('Finish the test!')

        # By visiting this unique URL, the user has access to her to-do list
        # again.

@@ -619,20 +619,21 @@
       # 
-User
+Alice
  goes to
@@ -921,20 +921,21 @@
       # 
-User
+Alice
  is invi
@@ -1176,12 +1176,13 @@
   # 
-User
+Alice
  typ
@@ -1291,20 +1291,21 @@
  # When 
-user
+Alice
  hits en
@@ -1458,16 +1458,120 @@
 .ENTER)%0A
+        alice_list_url = self.browser.current_url%0A        self.assertRegex(alice_list_url, '/lists/.+')%0A
         
@@ -1671,24 +1671,21 @@
 nviting 
-the user
+Alice
  to add 
@@ -2216,153 +2216,670 @@
   # 
-The site has generated a unique URL for the user -- there is some%0A        # text to explain this.%0A        self.fail('Finish the test!
+A new user, Bob, vists the site. We use a new browser session to make%0A        # sure that no information of Alice's is coming through from cookies%0A        # etc.%0A        self.browser.quit()%0A        self.browser = webdriver.Firefox()%0A%0A        # Bob visits the home page. There is no sign of Alice's list.%0A        self.browser.get(self.live_server_url)%0A        page_text = self.broser.find_element_by_tag_name('body').text%0A        self.assertNotIn('Buy peacock feathers', page_text)%0A        self.assertNotIn('make a fly', page_text)%0A%0A        # Bob starts a new list by entering a new item.%0A        inputbox = self.browser.find_element_by_id('id_new_item
 ')%0A
-%0A
         
 # By
@@ -2878,90 +2878,500 @@
     
-# By visiting this unique URL, the user has access to her to-do list%0A        # again.
+inputbox.send_keys('Buy milk')%0A        inputbox.send_keys(Keys.ENTER)%0A%0A        # Bob gets his own unique URL.%0A        bob_list_url = self.browser.current_url%0A        self.assertRegex(bob_list_url, '/lists/.+')%0A        self.assertNotEqual(bob_list_url, alice_list_url)%0A%0A        # Again, there is no trace of Alice's list%0A        page_text = self.browser.find_element_by_tag_name('body').text%0A        self.assertNotIn('Buy peacock feathers', page_text)%0A        self.assertIn('Buy milk', page_text)
 %0A

Extend user story in functional test to include unique URL for lists and a second user