def test_can_start_a_list_for_one_user(self): # Emily has heard about a cool new online to-do app. She goes to check out its homepage self.browser.get(self.live_server_url) # She notices the 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) # She is invited to enter a to-do item straight away list_page = ListPage(self) input_box = list_page.get_item_input_box() self.assertEqual(input_box.get_attribute('placeholder'), 'Enter a to-do item') # She types "Buy cat toys" into a text box (She has multiple cats that love to play) input_box.send_keys(E_ITEM_1) # When she hits enter, the page updates, and now the page lists # "1: Buy cat toys" as an item in a to-do list input_box.send_keys(Keys.ENTER) self.wait_for_row_in_list_table(f'1: {E_ITEM_1}') # There is still a text box inviting her to add another item. She enters "Surprise cats with toys" list_page.enter_list_item(E_ITEM_2) # The page updates again, and now shows both items on her list self.wait_for_row_in_list_table(f'1: {E_ITEM_1}') self.wait_for_row_in_list_table(f'2: {E_ITEM_2}')
def test_can_share_a_list_with_another_user(self): # Emily is a logged in user self.create_pre_authenticated_session(TEST_EMAIL_1) emily_browser = self.browser self.addCleanup(lambda: quit_if_possible(emily_browser)) # Her friend Oniciferous is also hanging out on the lists site oni_browser = get_webdriver() self.addCleanup(lambda: quit_if_possible(oni_browser)) self.browser = oni_browser self.create_pre_authenticated_session(TEST_EMAIL_2) # Emily goes to the home page and starts a list self.browser = emily_browser self.browser.get(self.live_server_url) list_page = ListPage(self).add_list_item('Get help') # She notices a "Share this list" option share_box = list_page.get_share_box() self.assertEqual(share_box.get_attribute('placeholder'), '*****@*****.**') # She shares her list. The page updates to say that it's shared with Oniciferous list_page.share_list_with(TEST_EMAIL_2) # Oniciferous now goes to the lists page with his browser self.browser = oni_browser MyListsPage(self).go_to_my_lists_page() # He sees Edith's list in there! self.browser.find_element_by_link_text('Get help').click() # On the list page, Oniciferous can see it says that it's Edit's list self.wait_for( lambda: self.assertEqual(list_page.get_list_owner(), TEST_EMAIL_1)) # He adds an item to the list list_page.add_list_item(TEST_ITEM) # When Edith refreshes the page, she sees Oniciferou's addition self.browser = emily_browser self.browser.refresh() list_page.wait_for_row_in_list_table(TEST_ITEM, 2)
def test_cannot_add_duplicate_items(self): # Emily goes to the new home page and starts a new list self.browser.get(self.live_server_url) list_page = ListPage(self).add_list_item(E_ITEM_1) # She accidentally tries to enter a duplicate item list_page.enter_list_item(E_ITEM_1) # She sees a helpful error message self.wait_for(lambda: self.assertEqual( self.get_error_element().text, "You've already got this in your list" ))
def test_error_messages_are_cleared_on_input(self): # Emily starts a list and causes a validation error: self.browser.get(self.live_server_url) list_page = ListPage(self).add_list_item(E_ITEM_1) list_page.enter_list_item(E_ITEM_1) self.wait_for(lambda: self.assertTrue( self.get_error_element().is_displayed() )) # She starts typing in the input box to clear the error input_box = list_page.get_item_input_box() input_box.send_keys(E_ITEM_2) # She is pleased to see that the error message disappears self.wait_for(lambda: self.assertFalse( self.get_error_element().is_displayed() ))
def test_aesthetics(self): # Emily goes to the home page self.browser.get(self.live_server_url) self.browser.set_window_size(1024, 768) list_page = ListPage(self) # She notices the input box is nicely centered input_box = list_page.get_item_input_box() self.assertAlmostEqual(input_box.location['x'] + input_box.size['width'] / 2, 512, delta=20) # She starts a new list and sees the input is nicely centered there too new_text = 'random item' list_page.add_list_item(new_text) input_box = list_page.get_item_input_box() self.assertAlmostEqual(input_box.location['x'] + input_box.size['width'] / 2, 512, delta=20)
def test_multiple_users_can_start_lists_at_different_urls(self): # Emily starts a new to-do list self.browser.get(self.live_server_url) list_page = ListPage(self).add_list_item(E_ITEM_1) # She notices that her list has a unique URL. emily_list_url = self.browser.current_url self.assertRegex(emily_list_url, '/lists/.+') # Now a new user, Felipe, comes along to the site. # # We use a new browser session to make sure that no information of Emily's is coming through from cookies, self.browser.refresh() self.browser.quit() self.browser = get_webdriver() # Felipe visits the home page. There is no sign of Emily's list self.browser.get(self.live_server_url) page_text = self.browser.find_element_by_tag_name('body').text self.assertNotIn(E_ITEM_1, page_text) self.assertNotIn(E_ITEM_2, page_text) # Felipe starts a new list by entering a new item. He is into techy stuff... list_page.add_list_item(F_ITEM_1) # Felipe gets his own unique URL felipe_list_url = self.browser.current_url self.assertRegex(felipe_list_url, '/lists/.+') self.assertNotEqual(felipe_list_url, emily_list_url) # Again, there is no trace of Emily's list page_text = self.browser.find_element_by_tag_name('body').text self.assertNotIn(E_ITEM_1, page_text) self.assertNotIn(E_ITEM_2, page_text)
def test_can_get_email_link_to_log_in(self): # Emily goes to the awesome superlists site and notices a "Log in" section in the navbar for the first time. # It's telling her to enter her email address, so she does. self.browser.get(self.live_server_url) list_page = ListPage(self) list_page.enter_email(TEST_EMAIL) # A message appears telling her an email has been sent self.wait_for(lambda: self.assertIn( 'Check your email', self.browser.find_element_by_tag_name('body').text)) # She checks her email and finds a message body = self.wait_for_email(TEST_EMAIL, SUBJECT) # It has a url link in it self.assertIn('Use this link to log in', body) url_search = re.search(r'http://.+/.+$', body) if not url_search: self.fail(f'Could not find url in email body:\n{body}') url = url_search.group(0) self.assertIn(self.live_server_url, url) # She clicks it self.browser.get(url) # She is logged in! list_page.wait_to_be_logged_in(email=TEST_EMAIL) # Now she logs out list_page.click_log_out_link() # She is logged out list_page.wait_to_be_logged_out(email=TEST_EMAIL)
def test_cannot_add_empty_list_items(self): # Emily goes to the home page and accidentally tries to submit an empty list item. She hits enter on the empty # input box. self.browser.get(self.live_server_url) list_page = ListPage(self) list_page.enter_list_item('') # The browser intercepts the request, and does not load the list page self.wait_for(lambda: self.browser.find_element_by_css_selector('#id_text:invalid')) # She starts typing some text for the new item and the error disappears input_box = list_page.get_item_input_box() input_box.send_keys(E_ITEM_1) self.wait_for(lambda: self.browser.find_element_by_css_selector('#id_text:valid')) # And she can submit it successfully input_box.send_keys(Keys.ENTER) self.wait_for_row_in_list_table(f'1: {E_ITEM_1}') # Perversely, she now decides to submit a second blank list item list_page.enter_list_item('') # Again the browser will not comply self.wait_for_row_in_list_table(f'1: {E_ITEM_1}') self.wait_for(lambda: self.browser.find_element_by_css_selector('#id_text:invalid')) # And she can correct it filling some text in input_box = list_page.get_item_input_box() input_box.send_keys(E_ITEM_2) self.wait_for(lambda: self.browser.find_element_by_css_selector('#id_text:valid')) input_box.send_keys(Keys.ENTER) self.wait_for_row_in_list_table(f'1: {E_ITEM_1}') self.wait_for_row_in_list_table(f'2: {E_ITEM_2}')