From f9a5ed6e83ba884fe6eb8b0c4dde62496ae436d3 Mon Sep 17 00:00:00 2001 From: Monal5031 Date: Wed, 20 Jun 2018 14:54:13 +0530 Subject: [PATCH 1/3] Testing Home app --- vms/home/tests/test_functional.py | 243 ++++++++++++++------------- vms/home/tests/test_services.py | 3 - vms/home/tests/test_unit.py | 2 - vms/pom/locators/homePageLocators.py | 2 + vms/pom/pages/homePage.py | 10 +- 5 files changed, 136 insertions(+), 124 deletions(-) delete mode 100644 vms/home/tests/test_services.py delete mode 100644 vms/home/tests/test_unit.py diff --git a/vms/home/tests/test_functional.py b/vms/home/tests/test_functional.py index 95c0c0e..33812a8 100644 --- a/vms/home/tests/test_functional.py +++ b/vms/home/tests/test_functional.py @@ -1,6 +1,9 @@ # third party from selenium import webdriver from selenium.common.exceptions import NoSuchElementException +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.common.by import By # Django from django.contrib.staticfiles.testing import LiveServerTestCase @@ -12,23 +15,23 @@ from shift.utils import (create_admin, create_volunteer) -# Class contains failing test cases which have been documented -# Test class commented out to prevent travis build failure -""" + class CheckURLAccess(LiveServerTestCase): - ''' + """ CheckURLAccess contains methods to browse(via URL) a volunteer page view after logging in from an admin account and vice-versa. Tests included: - Admin cannot access volunteer URL's - Volunteer cannot access admin URL's - ''' - + """ + @classmethod def setUpClass(cls): cls.driver = webdriver.Firefox() + cls.driver.implicitly_wait(5) cls.driver.maximize_window() cls.home_page = HomePage(cls.driver) cls.authentication_page = AuthenticationPage(cls.driver) + cls.wait = WebDriverWait(cls.driver, 10) super(CheckURLAccess, cls).setUpClass() def setUp(self): @@ -43,73 +46,77 @@ def tearDownClass(cls): cls.driver.quit() super(CheckURLAccess, cls).tearDownClass() - def find_volunteer_page_error(self, volunteer_url): - home_page = self.home_page - home_page.get_page(self.live_server_url, volunteer_url) - page_source = self.driver.page_source - error = re.search('403', page_source) - return error - def verify_admin_page_error(self, admin_url): home_page = self.home_page home_page.get_page(self.live_server_url, admin_url) heading = home_page.get_no_admin_right() body = home_page.get_no_admin_right_content() - self.assertNotEqual(heading,None) - self.assertNotEqual(body,None) - self.assertEqual(heading.text,'No Access') - self.assertEqual(body.text,"You don't have administrator rights") - - def test_admin_cannot_access_volunteer_urls(self): - ''' - Method logins an admin user and tries to surf volunteer pages through - url. The volunteer views should return a 403 error to deny access. - ''' - - authentication_page = self.authentication_page - authentication_page.server_url = self.live_server_url - authentication_page.login({ 'username' : 'admin', 'password' : 'admin'}) + self.assertNotEqual(heading, None) + self.assertNotEqual(body, None) + self.assertEqual(heading.text, 'No Access') + self.assertEqual(body.text, 'You don\'t have administrator rights') - error = self.find_volunteer_page_error(PageUrls.upcoming_shifts_page+'1') - self.assertNotEqual(error, None) + def verify_volunteer_page_error(self, volunteer_url): + home_page = self.home_page + home_page.get_page(self.live_server_url, volunteer_url) + head = home_page.get_no_volunteer_right() + body = home_page.get_no_volunteer_right_content() + self.assertNotEqual(head, None) + self.assertNotEqual(body, None) + self.assertEqual(head.text, 'No Access') + self.assertEqual(body.text, 'You don\'t have the required rights') + + def login(self, username, password): + self.authentication_page.login({ + 'username': username, + 'password': password + }) - error = self.find_volunteer_page_error(PageUrls.completed_shifts_page+'1') - self.assertNotEqual(error, None) + def wait_for_home_page(self): + self.wait.until( + EC.presence_of_element_located( + (By.XPATH, + "//h1[contains(text(), 'Volunteer Management System')]") + ) + ) - error = self.find_volunteer_page_error(PageUrls.shift_sign_up_page+'1') - self.assertNotEqual(error, None) + def test_admin_cannot_access_volunteer_urls(self): + """ + Method will login as admin user and tries to surf volunteer pages through url. + The volunteer views should return a no rights page. + """ - error = self.find_volunteer_page_error(PageUrls.volunteer_report_page+'1') - self.assertNotEqual(error, None) + authentication_page = self.authentication_page + authentication_page.server_url = self.live_server_url + self.login(username='admin', password='admin') + self.wait_for_home_page() - error = self.find_volunteer_page_error(PageUrls.volunteer_profile_page+'1') - self.assertNotEqual(error, None) + self.verify_volunteer_page_error(PageUrls.upcoming_shifts_page + '1') + self.verify_volunteer_page_error(PageUrls.completed_shifts_page + '1') + self.verify_volunteer_page_error(PageUrls.volunteer_report_page + '1') + self.verify_volunteer_page_error(PageUrls.volunteer_profile_page + '1') def test_volunteer_cannot_access_admin_urls(self): - ''' - Method logins a volunteer and tries to surf admin page views through url. + """ + Method will login as volunteer and tries to surf admin page views through url. The admin views should return a no admin rights page. - ''' + """ authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url - authentication_page.login({ 'username' : 'volunteer', 'password' : 'volunteer'}) + self.login(username='volunteer', password='volunteer') + self.wait_for_home_page() self.verify_admin_page_error(PageUrls.manage_volunteer_shift_page) - self.verify_admin_page_error(PageUrls.admin_settings) + self.verify_admin_page_error(PageUrls.admin_settings_page) self.verify_admin_page_error(PageUrls.volunteer_search_page) self.verify_admin_page_error(PageUrls.administrator_report_page) -""" - -# Class contains failing test cases which have been documented -# Test class commented out to prevent travis build failure -""" class CheckContentAndRedirection(LiveServerTestCase): - ''' - This Class contains methods to check if + """ + This Class contains methods to check if - - an administrator or a volunteer are provided their respective views + - an administrator or a volunteer are provided their respective views links on their dashboard. - all links in the nav-bar for admin and volunteer page redirect to desired views. @@ -133,14 +140,16 @@ class CheckContentAndRedirection(LiveServerTestCase): - Report - Profile - Logout - ''' + """ @classmethod def setUpClass(cls): cls.driver = webdriver.Firefox() + cls.driver.implicitly_wait(5) cls.driver.maximize_window() cls.home_page = HomePage(cls.driver) cls.authentication_page = AuthenticationPage(cls.driver) + cls.wait = WebDriverWait(cls.driver, 10) super(CheckContentAndRedirection, cls).setUpClass() def setUp(self): @@ -156,24 +165,39 @@ def tearDownClass(cls): cls.driver.quit() super(CheckContentAndRedirection, cls).tearDownClass() + def login(self, username, password): + self.authentication_page.login({ + 'username': username, + 'password': password + }) + + def wait_for_home_page(self): + self.wait.until( + EC.presence_of_element_located( + (By.XPATH, + "//h1[contains(text(), 'Volunteer Management System')]") + ) + ) + def test_check_admin_page_content(self): - ''' + """ Check if an admin user has following functionalities on its home page. - Volunteer Search - Manage Volunteer Shift - Report - Settings - Create Admin Account - ''' + """ authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url home_page = self.home_page - authentication_page.login({'username': 'admin', 'password': 'admin'}) - - with self.assertRaises(NoSuchElementException): - home_page.get_login_link() + self.login(username='admin', password='admin') + self.wait_for_home_page() + self.assertRaisesRegexp(NoSuchElementException, + 'Unable to locate element: Log In', + home_page.get_login_link) self.assertNotEqual(home_page.get_volunteer_search_link(), None) self.assertNotEqual(home_page.get_manage_shifts_link(), None) self.assertNotEqual(home_page.get_admin_report_link(), None) @@ -182,7 +206,7 @@ def test_check_admin_page_content(self): self.assertNotEqual(home_page.get_logout_link(), None) def test_check_volunteer_page_content(self): - ''' + """ Check if a volunteer user has following functionalities on its home page. - UpComing Shift @@ -190,18 +214,16 @@ def test_check_volunteer_page_content(self): - Shift Sign Up - Report - Profile - ''' + """ home_page = self.home_page authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url - authentication_page.login({ - 'username': 'volunteer', - 'password': 'volunteer' - }) - - with self.assertRaises(NoSuchElementException): - home_page.get_login_link() + self.login(username='volunteer', password='volunteer') + self.wait_for_home_page() + self.assertRaisesRegexp(NoSuchElementException, + 'Unable to locate element: Log In', + home_page.get_login_link) self.assertNotEqual(home_page.get_upcoming_shifts_link(), None) self.assertNotEqual(home_page.get_completed_shifts_link(), None) self.assertNotEqual(home_page.get_shift_signup_link(), None) @@ -213,85 +235,72 @@ def test_admin_page_redirection(self): home_page = self.home_page authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url - authentication_page.login({'username': 'admin', 'password': 'admin'}) + self.login(username='admin', password='admin') + self.wait_for_home_page() - self.assertEqual(self.driver.current_url, + self.assertEqual(authentication_page.remove_i18n(self.driver.current_url), self.live_server_url + PageUrls.homepage) + self.assertRaisesRegexp(NoSuchElementException, + 'Unable to locate element: Log In', + home_page.get_login_link) - with self.assertRaises(NoSuchElementException): - home_page.get_login_link() - - volunteer_search_link = home_page.get_volunteer_search_link( - ).get_attribute('href') - self.assertEqual(volunteer_search_link, + volunteer_search_link = home_page.get_volunteer_search_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(volunteer_search_link), self.live_server_url + PageUrls.volunteer_search_page) - manage_volunteer_shift_link = home_page.get_manage_shifts_link( - ).get_attribute('href') - self.assertEqual( - manage_volunteer_shift_link, - self.live_server_url + PageUrls.manage_volunteer_shift_page) + manage_volunteer_shift_link = home_page.get_manage_shifts_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(manage_volunteer_shift_link), + self.live_server_url + PageUrls.manage_volunteer_shift_page) report_link = home_page.get_admin_report_link().get_attribute('href') - self.assertEqual( - report_link, - self.live_server_url + PageUrls.administrator_report_page) + self.assertEqual(home_page.remove_i18n(report_link), + self.live_server_url + PageUrls.administrator_report_page) settings_link = home_page.get_events_link().get_attribute('href') - self.assertEqual(settings_link, + self.assertEqual(home_page.remove_i18n(settings_link), self.live_server_url + PageUrls.admin_settings_page) - creat_account_link = home_page.get_create_admin_link().get_attribute( - 'href') - self.assertEqual( - creat_account_link, - self.live_server_url + PageUrls.admin_registration_page) + create_account_link = home_page.get_create_admin_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(create_account_link), + self.live_server_url + PageUrls.admin_registration_page) logout_link = home_page.get_logout_link().get_attribute('href') - self.assertEqual(logout_link, + self.assertEqual(home_page.remove_i18n(logout_link), self.live_server_url + PageUrls.logout_page) def test_volunteer_page_redirection(self): home_page = self.home_page authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url - authentication_page.login({ - 'username': 'volunteer', - 'password': 'volunteer' - }) + self.login(username='volunteer', password='volunteer') + self.wait_for_home_page() - self.assertEqual(self.driver.current_url, + self.assertEqual(home_page.remove_i18n(self.driver.current_url), self.live_server_url + PageUrls.homepage) + self.assertRaisesRegexp(NoSuchElementException, + 'Unable to locate element: Log In', + home_page.get_login_link) - with self.assertRaises(NoSuchElementException): - home_page.get_login_link() - - upcoming_shift_link = home_page.get_upcoming_shifts_link( - ).get_attribute('href') - self.assertEqual(upcoming_shift_link, self.live_server_url + + upcoming_shift_link = home_page.get_upcoming_shifts_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(upcoming_shift_link), self.live_server_url + PageUrls.upcoming_shifts_page + self.volunteer_id) - shift_hours_link = home_page.get_completed_shifts_link().get_attribute( - 'href') - self.assertEqual(shift_hours_link, self.live_server_url + + shift_hours_link = home_page.get_completed_shifts_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(shift_hours_link), self.live_server_url + PageUrls.completed_shifts_page + self.volunteer_id) - shift_signup_link = home_page.get_shift_signup_link().get_attribute( - 'href') - self.assertEqual(shift_signup_link, self.live_server_url + + shift_signup_link = home_page.get_shift_signup_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(shift_signup_link), self.live_server_url + PageUrls.shift_sign_up_page + self.volunteer_id) - report_link = home_page.get_volunteer_report_link().get_attribute( - 'href') - self.assertEqual(report_link, self.live_server_url + - PageUrls.volunteer_report_page + self.volunteer_id) + report_link = home_page.get_volunteer_report_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(report_link), + self.live_server_url + PageUrls.volunteer_report_page + self.volunteer_id) - profile_link = home_page.get_volunteer_profile_link().get_attribute( - 'href') - self.assertEqual(profile_link, self.live_server_url + - PageUrls.volunteer_profile_page + self.volunteer_id) + profile_link = home_page.get_volunteer_profile_link().get_attribute('href') + self.assertEqual(home_page.remove_i18n(profile_link), + self.live_server_url + PageUrls.volunteer_profile_page + self.volunteer_id) logout_link = home_page.get_logout_link().get_attribute('href') - - self.assertEqual(logout_link, self.live_server_url + - PageUrls.logout_page)""" + self.assertEqual(home_page.remove_i18n(logout_link), + self.live_server_url + PageUrls.logout_page) diff --git a/vms/home/tests/test_services.py b/vms/home/tests/test_services.py deleted file mode 100644 index f9e883a..0000000 --- a/vms/home/tests/test_services.py +++ /dev/null @@ -1,3 +0,0 @@ -# Django - -# Create your tests here. diff --git a/vms/home/tests/test_unit.py b/vms/home/tests/test_unit.py deleted file mode 100644 index a2629a5..0000000 --- a/vms/home/tests/test_unit.py +++ /dev/null @@ -1,2 +0,0 @@ -# Django - diff --git a/vms/pom/locators/homePageLocators.py b/vms/pom/locators/homePageLocators.py index 10f778c..bb0159a 100644 --- a/vms/pom/locators/homePageLocators.py +++ b/vms/pom/locators/homePageLocators.py @@ -15,3 +15,5 @@ class HomePageLocators(object): NO_ADMIN_RIGHT_HEAD = 'panel-heading' NO_ADMIN_RIGHT_CONTENT = 'panel-body' + NO_VOLUNTEER_RIGHT_HEAD = 'panel-title' + NO_VOLUNTEER_RIGHT_CONTENT = 'panel-body' diff --git a/vms/pom/pages/homePage.py b/vms/pom/pages/homePage.py index 0b7a6cf..676c21c 100644 --- a/vms/pom/pages/homePage.py +++ b/vms/pom/pages/homePage.py @@ -45,7 +45,13 @@ def get_volunteer_profile_link(self): return self.find_link(self.elements.VOLUNTEER_PROFILE_TEXT) def get_no_admin_right(self): - return self.elements_by_class_name(self.elements.NO_ADMIN_RIGHT_HEAD) + return self.element_by_class_name(self.elements.NO_ADMIN_RIGHT_HEAD) def get_no_admin_right_content(self): - return self.elements_by_class_name(self.elements.NO_ADMIN_RIGHT_CONTENT) + return self.element_by_class_name(self.elements.NO_ADMIN_RIGHT_CONTENT) + + def get_no_volunteer_right(self): + return self.element_by_class_name(self.elements.NO_VOLUNTEER_RIGHT_HEAD) + + def get_no_volunteer_right_content(self): + return self.element_by_class_name(self.elements.NO_VOLUNTEER_RIGHT_CONTENT) From 1265e14759d1e1f40d9e4a3a793e40339a17308b Mon Sep 17 00:00:00 2001 From: Monal5031 Date: Mon, 9 Jul 2018 07:01:10 +0530 Subject: [PATCH 2/3] Add docstrings to home tests --- vms/home/tests/test_functional.py | 80 +++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/vms/home/tests/test_functional.py b/vms/home/tests/test_functional.py index 33812a8..a87b47a 100644 --- a/vms/home/tests/test_functional.py +++ b/vms/home/tests/test_functional.py @@ -26,6 +26,11 @@ class CheckURLAccess(LiveServerTestCase): @classmethod def setUpClass(cls): + """Method to initiate class level objects. + + This method initiates Firefox WebDriver, WebDriverWait and + the corresponding POM objects for this Test Class + """ cls.driver = webdriver.Firefox() cls.driver.implicitly_wait(5) cls.driver.maximize_window() @@ -35,18 +40,36 @@ def setUpClass(cls): super(CheckURLAccess, cls).setUpClass() def setUp(self): + """ + Method consists of statements to be executed before + start of each test. + """ create_admin() create_volunteer() def tearDown(self): + """ + Method consists of statements to be executed at + end of each test. + """ pass @classmethod def tearDownClass(cls): + """ + Class method to quit the Firefox WebDriver session after + execution of all tests in class. + """ cls.driver.quit() super(CheckURLAccess, cls).tearDownClass() def verify_admin_page_error(self, admin_url): + """ + Utility function to verify errors raised on the + pages when user tries to admin pages provided as + admin_url in param. + :param admin_url: URL of admin page to check errors on. + """ home_page = self.home_page home_page.get_page(self.live_server_url, admin_url) heading = home_page.get_no_admin_right() @@ -57,6 +80,12 @@ def verify_admin_page_error(self, admin_url): self.assertEqual(body.text, 'You don\'t have administrator rights') def verify_volunteer_page_error(self, volunteer_url): + """ + Utility function to verify errors raised on the + pages when user tries to volunteer pages provided as + volunteer_url in param. + :param volunteer_url: URL of volunteer page to check errors on. + """ home_page = self.home_page home_page.get_page(self.live_server_url, volunteer_url) head = home_page.get_no_volunteer_right() @@ -67,12 +96,20 @@ def verify_volunteer_page_error(self, volunteer_url): self.assertEqual(body.text, 'You don\'t have the required rights') def login(self, username, password): + """ + Utility function to login with credentials received as parameters. + :param username: Username of the user + :param password: Password of the user + """ self.authentication_page.login({ 'username': username, 'password': password }) def wait_for_home_page(self): + """ + Utility function to perform a explicit wait for home page. + """ self.wait.until( EC.presence_of_element_located( (By.XPATH, @@ -82,8 +119,8 @@ def wait_for_home_page(self): def test_admin_cannot_access_volunteer_urls(self): """ - Method will login as admin user and tries to surf volunteer pages through url. - The volunteer views should return a no rights page. + Test admin will be shown errors when they try to access + volunteers URLs. """ authentication_page = self.authentication_page @@ -98,8 +135,8 @@ def test_admin_cannot_access_volunteer_urls(self): def test_volunteer_cannot_access_admin_urls(self): """ - Method will login as volunteer and tries to surf admin page views through url. - The admin views should return a no admin rights page. + Test volunteer will be shown errors when they try to access + admin URLs. """ authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url @@ -144,6 +181,11 @@ class CheckContentAndRedirection(LiveServerTestCase): @classmethod def setUpClass(cls): + """Method to initiate class level objects. + + This method initiates Firefox WebDriver, WebDriverWait and + the corresponding POM objects for this Test Class + """ cls.driver = webdriver.Firefox() cls.driver.implicitly_wait(5) cls.driver.maximize_window() @@ -153,25 +195,45 @@ def setUpClass(cls): super(CheckContentAndRedirection, cls).setUpClass() def setUp(self): + """ + Method consists of statements to be executed before + start of each test. + """ self.admin = create_admin() self.volunteer = create_volunteer() self.volunteer_id = str(self.volunteer.id) def tearDown(self): + """ + Method consists of statements to be executed at + end of each test. + """ pass @classmethod def tearDownClass(cls): + """ + Class method to quit the Firefox WebDriver session after + execution of all tests in class. + """ cls.driver.quit() super(CheckContentAndRedirection, cls).tearDownClass() def login(self, username, password): + """ + Utility function to login with credentials received as parameters. + :param username: Username of the user + :param password: Password of the user + """ self.authentication_page.login({ 'username': username, 'password': password }) def wait_for_home_page(self): + """ + Utility function to perform a explicit wait for home page. + """ self.wait.until( EC.presence_of_element_located( (By.XPATH, @@ -232,6 +294,11 @@ def test_check_volunteer_page_content(self): self.assertNotEqual(home_page.get_logout_link(), None) def test_admin_page_redirection(self): + """ + Test admin is redirected corrected to home page after + successful authorization by checking different elements + on home page. + """ home_page = self.home_page authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url @@ -269,6 +336,11 @@ def test_admin_page_redirection(self): self.live_server_url + PageUrls.logout_page) def test_volunteer_page_redirection(self): + """ + Test volunteer is redirected corrected to home page after + successful authorization by checking different elements + on home page. + """ home_page = self.home_page authentication_page = self.authentication_page authentication_page.server_url = self.live_server_url From 19f290ca7ab4546d2b04101d1ff89aa2e01a7350 Mon Sep 17 00:00:00 2001 From: Monal5031 Date: Thu, 19 Jul 2018 02:33:45 +0530 Subject: [PATCH 3/3] Increase value of id for home app --- vms/home/tests/test_functional.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vms/home/tests/test_functional.py b/vms/home/tests/test_functional.py index a87b47a..afcd4f6 100644 --- a/vms/home/tests/test_functional.py +++ b/vms/home/tests/test_functional.py @@ -128,10 +128,10 @@ def test_admin_cannot_access_volunteer_urls(self): self.login(username='admin', password='admin') self.wait_for_home_page() - self.verify_volunteer_page_error(PageUrls.upcoming_shifts_page + '1') - self.verify_volunteer_page_error(PageUrls.completed_shifts_page + '1') - self.verify_volunteer_page_error(PageUrls.volunteer_report_page + '1') - self.verify_volunteer_page_error(PageUrls.volunteer_profile_page + '1') + self.verify_volunteer_page_error(PageUrls.upcoming_shifts_page + '1000') + self.verify_volunteer_page_error(PageUrls.completed_shifts_page + '1000') + self.verify_volunteer_page_error(PageUrls.volunteer_report_page + '1000') + self.verify_volunteer_page_error(PageUrls.volunteer_profile_page + '1000') def test_volunteer_cannot_access_admin_urls(self): """