def test_index_blog_shows_posts_and_can_be_consulted(self): # Nato goes to the blog and creates a posts and keeps the url first_post = PostFactory.build(title='First post title') create_post_page = CreatePostPage(self).create_post(first_post) first_post_url = self.browser.current_url # He can see the post in the index page of the posts index_blog_page = IndexBlogPage(self).go_to_index_blog_page() index_blog_page.wait_for_title_post_in_the_posts(first_post.title) # He clicks the Read More link and can see that he is in the page # for that post post = index_blog_page.get_post_from_this_page(first_post.title) post.find_element_by_link_text('READ MORE').click() self.wait_for( lambda: self.assertEqual(self.browser.current_url, first_post_url) ) # He creates another post and again saves the url second_post = PostFactory.build(title='Second post title') create_post_page.create_post(second_post) second_post_url = self.browser.current_url # And now he sees the new created post in the index index_blog_page = IndexBlogPage(self).go_to_index_blog_page() index_blog_page.wait_for_title_post_in_the_posts(second_post.title) # And he checks is the page that shows him the post post = index_blog_page.get_post_from_this_page(second_post.title) post.find_element_by_link_text('READ MORE').click() self.wait_for( lambda: self.assertEqual(self.browser.current_url, second_post_url) )
def test_can_edit_a_post(self): # Nato goes to the blog and creates a post post_object = PostFactory.build() CreatePostPage(self).create_post(post_object) # Then he proceeds to edit it # He first goes to the page of the post index_post_page = IndexPostPage(self).go_to_index_post_page() post_html = index_post_page.get_post_from_this_page(post_object.title) post_html.find_element_by_link_text('EDIT POST').click() # Then he starts to edit the post field by field edit_post_page = EditPostPage(self) edit_post_page.write_in_title_input_box('New title') edit_post_page.write_in_content_input_box('New content') # He puts the image for the post ## TODO: today_plus_three_days = datetime.date.today() + datetime.timedelta(3) edit_post_page.write_expiring_date(today_plus_three_days.strftime('%Y-%m-%d')) # Then he presses the save button edit_post_page.click_save_post() edit_post_page.check_message_in_messages('The blog post has been updated') # And he can see the post has changed body_text = self.browser.find_element_by_tag_name('body').text self.assertIn('New title', body_text) self.assertIn('New content', body_text) self.assertIn(post_object.publication_date.strftime('%b. %d, %Y'), body_text) self.assertIn(today_plus_three_days.strftime('%b. %d, %Y'), body_text)
def test_index_blog_does_not_show_non_published_post(self): # Nato goes to the blog and creates an expired post non_published_post = PostFactory.build(title='Expired post') non_published_post.publication_date = datetime.date.today( ) + datetime.timedelta(1) create_post_page = CreatePostPage(self).create_post(non_published_post) # And creates a non expired post published_post = PostFactory.build(title='Not expired post') published_post.publication_date = datetime.date.today() published_post.expiring_date = datetime.date.today() create_post_page.create_post(published_post) # He can see the post in the index page of the posts index_post_page = IndexBlogPage(self).go_to_index_blog_page() index_post_page.wait_for_title_post_in_the_posts(published_post.title) # And as expected he can't see the expired post body = self.browser.find_element_by_tag_name('body') self.assertNotIn(non_published_post.title, body.text)
def test_can_delete_a_post(self): # Nato goes to the blog and creates two post first_post = PostFactory.build(title='First post title') second_post = PostFactory.build(title='Second post title') create_post_page = CreatePostPage(self).create_post(first_post) create_post_page.create_post(second_post) # He can see the posts in the index index_post_page = IndexPostPage(self).go_to_index_post_page() index_post_page.wait_for_title_post_in_the_posts(first_post.title) index_post_page.wait_for_title_post_in_the_posts(second_post.title) # He clicks the Delete button of one post post = index_post_page.get_post_from_this_page(second_post.title) post.find_element_by_name('delete post').click() index_post_page.check_message_in_messages( 'The blog post has been succesfully deleted') # and he can see the post has been deleted body = self.browser.find_element_by_tag_name('body') self.assertNotIn(second_post.title, body.text) index_post_page.wait_for_title_post_in_the_posts(first_post.title)
def test_index_post_shows_truncated_content(self): # Nato goes to the blog and creates a post with a long content post = PostFactory.build(title='First post title') fake = Faker() post.content = fake.sentence(nb_words=100) CreatePostPage(self).create_post(post) # He can see the post in the index page of the posts index_post_page = IndexPostPage(self).go_to_index_post_page() post_html = index_post_page.get_post_from_this_page(post.title) # And he can see the content has been trimmed trimmed_content = post.content[:97] + '...' trimmed_content = trimmed_content.replace('\n', ' ') self.assertIn(trimmed_content, post_html.text)
def test_publication_date_updates_asynchronous(self): # Nato goes to the blog and creates a post post_object = PostFactory.build() CreatePostPage(self).create_post(post_object) first_browser = self.browser self.addCleanup(lambda: quit_if_possible(first_browser)) # And he can see it index_blog_page = IndexBlogPage(self).go_to_index_blog_page() index_blog_page.wait_for_title_post_in_the_posts(post_object.title) # Then he proceeds to edit it in another browser second_browser = webdriver.Firefox() self.addCleanup(lambda: quit_if_possible(second_browser)) self.browser = second_browser # He first goes to the page of the post index_post_page = IndexPostPage(self).go_to_index_post_page() post_html = index_post_page.get_post_from_this_page(post_object.title) post_html.find_element_by_link_text('EDIT POST').click() # Then he starts to edit the post publication date edit_post_page = EditPostPage(self) today_plus_three_days = datetime.date.today() + datetime.timedelta(3) edit_post_page.write_publication_date( today_plus_three_days.strftime('%Y-%m-%d')) ## Change the focus so the event is complete edit_post_page.write_in_title_input_box("") # And he sees a message telling him the publication date has changed whitout saving # the post edit_post_page.check_message_in_messages( "The publication date has been saved.") # And he can see the post has dissapeared from the index blog page self.browser = first_browser index_blog_page = IndexBlogPage(self).go_to_index_blog_page() body_text = self.browser.find_element_by_tag_name('body').text self.assertNotIn(post_object.title, body_text)
def test_cannot_add_post_with_bad_expiration_date(self): # Nato goes blog and starts a new post create_post_page = CreatePostPage(self).go_to_create_post_page() create_post_page.write_in_title_input_box('Awesome blog post') create_post_page.write_in_content_input_box('Content of the post') today = datetime.date.today() create_post_page.write_publication_date(today.strftime('%Y-%m-%d')) # He creates an expiring_date that does not make sense today_minus_a_day = today - datetime.timedelta(days=1) expiring_date = today_minus_a_day.strftime('%Y-%m-%d') create_post_page.write_expiring_date(expiring_date) create_post_page.click_create_post() self.wait_for( lambda: self.assertEqual(create_post_page.get_error_element().text, EXPIRATION_DATE_IS_WRONG))