class FunctionalTest(LiveServerTestCase): def setUp(self): self.browser = WebDriver() self.browser.implicitly_wait(5) def tearDown(self): self.browser.quit() def wait_for(self, function_with_assertion, timeout=DEFAULT_WAIT): start_time = time.time() while time.time() - start_time < timeout: try: return function_with_assertion() except (AssertionError, WebDriverException): time.sleep(0.1) return function_with_assertion() def create_pre_authenticated_session(self, email, password, *args, **kwargs): session_key = create_pre_authenticated_session(email, password, *args, **kwargs) # To set a cookie we need to first visit the domain... # 404 pages load the fastest... self.browser.get(self.live_server_url + '/404_no_such_url/') self.browser.add_cookie({ 'name': settings.SESSION_COOKIE_NAME, 'value': session_key, 'path': '/' })
class WebObject(StaticLiveServerTestCase): """Base class for page objects.""" @classmethod def setUpClass(self): super(WebObject, self).setUpClass() self.driver = WebDriver() self.driver.set_window_size(1024, 768) self.driver.maximize_window() self.driver.implicitly_wait(15) @classmethod def tearDownClass(self): self.driver.quit() super(WebObject, self).tearDownClass() def create_login_session(self, username): session_cookie = create_session_for_given_user(username) self.driver.get(self.live_server_url) self.driver.add_cookie(session_cookie)
class TransactionTypeAddTest(StaticLiveServerTestCase): def setUp(self): self.selenium = WebDriver() self.user = User.objects.create_superuser(username="******", email="*****@*****.**", password="******", first_name="kossar", last_name="najafi", phone_number="09147898557", account_number="1234567812345678", notification_preference="S") self.selenium.implicitly_wait(10) self.selenium.get('%s%s' % (self.live_server_url, '/admin_panel/transaction_types/add')) def tearDown(self): self.selenium.quit() def __get_page(self): class TransactionTypeAddPage(object): def __init__(self, selenium): self.selenium = selenium self.name = self.selenium.find_element_by_id('id_name') self.description = self.selenium.find_element_by_id('id_description') self.fixed_price = self.selenium.find_element_by_id('id_fixed_price') self.currency_select = Select(self.selenium.find_element_by_id('id_currency')) self.price = self.selenium.find_element_by_id('id_price') self.min_price = self.selenium.find_element_by_id('id_min_amount') self.max_price = self.selenium.find_element_by_id('id_max_amount') self.wage = self.selenium.find_element_by_id('id_wage_percentage') # self.group = self.selenium.find_element_by_id('id_transaction_group') self.general_info = self.selenium.find_element_by_id('id_required_fields_0') self.personal_info = self.selenium.find_element_by_id('id_required_fields_1') self.exam_info = self.selenium.find_element_by_id('id_required_fields_2') self.university_info = self.selenium.find_element_by_id('id_required_fields_3') self.button = self.selenium.find_element_by_xpath("//button[@type='submit']") return TransactionTypeAddPage(self.selenium) def login(self): user = User.objects.get(username="******") SessionStore = import_module(settings.SESSION_ENGINE).SessionStore session = SessionStore() session[SESSION_KEY] = User.objects.get(username="******").id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session[HASH_SESSION_KEY] = user.get_session_auth_hash() session.save() cookie = { 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'path': '/', } self.selenium.add_cookie(cookie) self.selenium.refresh() self.selenium.get('%s%s' % (self.live_server_url, '/admin_panel/transaction_types/add')) @staticmethod def __get_text(element): return element.get_attribute('textContent') @staticmethod def __get_checked(element): return element.get_attribute('checked') @staticmethod def __send_keys_scrolling(input_element, keys): _ = input_element.location_once_scrolled_into_view time.sleep(1) input_element.send_keys(keys) def check_transaction_type_creation(self): toefl = PaymentType.objects.get(name="toefl") self.assertIn("english", toefl.description) def _fill(self, page): self.__send_keys_scrolling(page.name, 'toefl') self.__send_keys_scrolling(page.description, 'english exam') page.fixed_price.click() page.currency_select.select_by_visible_text("$") page.min_price.clear() page.price.clear() page.max_price.clear() page.wage.clear() self.__send_keys_scrolling(page.price, '200') self.__send_keys_scrolling(page.min_price, '1') self.__send_keys_scrolling(page.max_price, '1000000') self.__send_keys_scrolling(page.wage, '7') page.personal_info.click() page.general_info.click() page.exam_info.click() def test_successful_ttadd(self): self.login() page = self.__get_page() self._fill(page) page.button.click() self.check_transaction_type_creation() self.assertNotIn("add", self.selenium.current_url) def test_empty_parts_add(self): self.login() page = self.__get_page() self._fill(page) page.name.clear() page.button.click() self.assertIn("add", self.selenium.current_url)
class ExchangeCreditTest(StaticLiveServerTestCase): def setUp(self): self.selenium = WebDriver() self.user = User.objects.create_user(username="******", email="*****@*****.**", password="******", first_name="kossar", last_name="najafi", phone_number="09147898557", account_number="1234432112344321", notification_preference="S") self.selenium.implicitly_wait(10) self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/exchange/')) def tearDown(self): self.selenium.quit() def __get_page_1(self): class ExchangePage(object): def __init__(self, selenium): self.selenium = selenium self.from_curr = Select( self.selenium.find_element_by_id('id_currency')) self.to_curr = Select( self.selenium.find_element_by_id('id_final_currency')) self.amount = self.selenium.find_element_by_id( 'id_final_amount') self.button = self.selenium.find_element_by_xpath( "//button[@type='submit']") return ExchangePage(self.selenium) def __get_page_2(self): class ExchangePreviewPage(object): def __init__(self, selenium): self.selenium = selenium self.confirm_exchange = self.selenium.find_element_by_xpath( "//button[@type='submit']") return ExchangePreviewPage(self.selenium) @staticmethod def _fill_1(page): page.amount.send_keys('1') page.from_curr.select_by_visible_text('﷼') page.to_curr.select_by_visible_text('$') def login(self): user = User.objects.get(username="******") SessionStore = import_module(settings.SESSION_ENGINE).SessionStore session = SessionStore() session[SESSION_KEY] = User.objects.get(username="******").id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session[HASH_SESSION_KEY] = user.get_session_auth_hash() session.save() cookie = { 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'path': '/', } self.selenium.add_cookie(cookie) self.selenium.refresh() self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/exchange/')) @staticmethod def __get_text(element): return element.get_attribute('textContent') def charge_wallet(self, amount): self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/charge/')) self.selenium.find_element_by_id('id_amount').send_keys(amount) self.selenium.find_element_by_id('charge-button').click() self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/exchange/')) def test_preview_exchange_not_empty(self): self.login() page = self.__get_page_1() self._fill_1(page) page.amount.clear() page.button.click() success_set = self.selenium.find_elements_by_xpath( '//*[@id="right-panel"]/div[2]/div/div[1]/div/div') self.assertEqual(len(success_set), 0) page.amount.send_keys('1') page.from_curr.select_by_visible_text('---------') page.button.click() success_set = self.selenium.find_elements_by_xpath( '//*[@id="right-panel"]/div[2]/div/div[1]/div/div') self.assertEqual(len(success_set), 0) page.from_curr.select_by_visible_text('﷼') page.to_curr.select_by_visible_text('---------') page.button.click() success_set = self.selenium.find_elements_by_xpath( '//*[@id="right-panel"]/div[2]/div/div[1]/div/div') self.assertEqual(len(success_set), 0) page.to_curr.select_by_visible_text('$') def test_preview_exchange(self): self.login() self.charge_wallet(550000) page = self.__get_page_1() self._fill_1(page) page.button.click() self.selenium.implicitly_wait(10) self.assertIn("accept", self.selenium.current_url) def test_preview_accept(self): self.login() self.charge_wallet(550000) page = self.__get_page_1() self._fill_1(page) page.button.click() self.selenium.implicitly_wait(10) page2 = self.__get_page_2() page2.confirm_exchange.click() time.sleep(3) credit = self.selenium.find_element_by_id("wallet_balance_$") self.assertEqual(float(1), float(self.__get_text(credit))) self.assertNotIn("accept", self.selenium.current_url)
class ReportTest(StaticLiveServerTestCase): def setUp(self): self.selenium = WebDriver() self.user = User.objects.create_superuser( username="******", email="*****@*****.**", password="******", first_name="kossar", last_name="najafi", phone_number="09147898557", account_number="1234567812345678", notification_preference="S") self.user = User.objects.create_user(username="******", email="*****@*****.**", password="******", first_name="kossar", last_name="najafi", phone_number="09147898557", account_number="1234432112344321", notification_preference="S") self.__create_transactions() self.selenium.implicitly_wait(10) self.selenium.get( '%s%s' % (self.live_server_url, '/admin_panel/transactions/1')) def tearDown(self): self.selenium.quit() @staticmethod def __create_transactions(): user_k = User.objects.get(username="******") transaction1 = Transaction(owner=user_k, amount="300", currency="D") transaction1.save() def login(self): user = User.objects.get(username="******") SessionStore = import_module(settings.SESSION_ENGINE).SessionStore session = SessionStore() session[SESSION_KEY] = User.objects.get(username="******").id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session[HASH_SESSION_KEY] = user.get_session_auth_hash() session.save() cookie = { 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'path': '/', } self.selenium.add_cookie(cookie) self.selenium.refresh() self.selenium.get( '%s%s' % (self.live_server_url, '/admin_panel/transactions/1')) def __get_page(self): class ReportPage(object): def __init__(self, selenium): self.selenium = selenium self.comment = self.selenium.find_element_by_id('id_comment') self.selenium.execute_script( "window.scrollTo(0, document.body.scrollHeight);") self.button = self.selenium.find_element_by_xpath( "//button[@type='submit']") return ReportPage(self.selenium) @staticmethod def __send_keys_scrolling(input_element, keys): _ = input_element.location_once_scrolled_into_view time.sleep(1) input_element.send_keys(keys) def _fill(self, page): self.__send_keys_scrolling(page.comment, 'suspicious') def test_successful_report(self): self.login() page = self.__get_page() self._fill(page) page.button.click() self.selenium.get( '%s%s' % (self.live_server_url, '/admin_panel/transactions/1')) p = self.selenium.find_elements_by_tag_name('p') self.assertEqual(len(p), 0) @staticmethod def __get_text(element): return element.get_attribute('textContent')
class EditTest(StaticLiveServerTestCase): fixtures = ['user_panel_data.json'] def setUp(self): self.selenium = WebDriver() self.user = User.objects.create_user(username="******", email="*****@*****.**", password="******", first_name="kossar", last_name="najafi", phone_number="09147898557", account_number="1234432112344321", notification_preference="S") self.selenium.implicitly_wait(10) self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/profile/')) def tearDown(self): self.selenium.quit() def login(self): user = User.objects.get(username="******") SessionStore = import_module(settings.SESSION_ENGINE).SessionStore session = SessionStore() session[SESSION_KEY] = User.objects.get(username="******").id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session[HASH_SESSION_KEY] = user.get_session_auth_hash() session.save() cookie = { 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'path': '/', } self.selenium.add_cookie(cookie) self.selenium.refresh() self.selenium.get('%s%s' % (self.live_server_url, '/user_panel/profile/')) def __get_page(self): class EditPage(object): def __init__(self, selenium): self.selenium = selenium self.username = self.selenium.find_element_by_id('id_username') self.email = self.selenium.find_element_by_id('id_email') self.fname = self.selenium.find_element_by_id('id_first_name') self.lname = self.selenium.find_element_by_id('id_last_name') self.phone = self.selenium.find_element_by_id( 'id_phone_number') self.notification = Select( self.selenium.find_element_by_id( 'id_notification_preference')) self.button = self.selenium.find_element_by_xpath( "//button[@type='submit']") return EditPage(self.selenium) @staticmethod def _fill(page): page.username.clear() page.email.clear() page.fname.clear() page.lname.clear() page.phone.clear() page.username.send_keys('parand') page.email.send_keys('*****@*****.**') page.fname.send_keys('Parand') page.lname.send_keys('Alizadeh') page.phone.send_keys('02347898557') page.notification.select_by_visible_text('sms') @staticmethod def __get_text(element): return element.get_attribute('textContent') @staticmethod def __send_keys_scrolling(input_element, keys): _ = input_element.location_once_scrolled_into_view time.sleep(1) input_element.send_keys(keys) def test_empty_parts_edit(self): self.login() page = self.__get_page() username = page.username username.clear() button = page.button button.click() success_set = self.selenium.find_elements_by_xpath( '//*[@id="right-panel"]/div[2]/div/div[1]/div/div') self.assertEqual(len(success_set), 0) def test_successful_edit(self): self.login() page = self.__get_page() self._fill(page) page.button.click() success = self.selenium.find_element_by_xpath( '//*[@id="right-panel"]/div[2]/div/div[1]/div/div') self.assertIn("Profile successfully updated!", self.__get_text(success))
class RegistrationTest(StaticLiveServerTestCase): def setUp(self): self.selenium = WebDriver() self.selenium.implicitly_wait(10) self.__create_users() self.selenium.get('%s%s' % (self.live_server_url, '/admin_panel/reviewers/add')) def tearDown(self): self.selenium.quit() pass def __create_users(self): self.user = User.objects.create_superuser( username="******", email="*****@*****.**", password="******", first_name="kossar", last_name="najafi", phone_number="09147898557", account_number="1234567812345678", notification_preference="S") self.user = User.objects.create_user(username="******", email="*****@*****.**", password="******", first_name="kossar", last_name="najafi", phone_number="09147898557", account_number="1234432112344321", notification_preference="S") self.user = User.objects.create_user(username="******", email="*****@*****.**", password="******", first_name="soroush", last_name="ebadian", phone_number="09147898557", account_number="1234567887654321", notification_preference="S") def login(self): user = User.objects.get(username="******") SessionStore = import_module(settings.SESSION_ENGINE).SessionStore session = SessionStore() session[SESSION_KEY] = User.objects.get(username="******").id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session[HASH_SESSION_KEY] = user.get_session_auth_hash() session.save() cookie = { 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'path': '/', } self.selenium.add_cookie(cookie) self.selenium.refresh() self.selenium.get('%s%s' % (self.live_server_url, '/admin_panel/reviewers/add')) def __get_page(self): class AddReviewerPage(object): def __init__(self, selenium): self.selenium = selenium self.username = Select( self.selenium.find_element_by_id('id_user')) self.salary = self.selenium.find_element_by_id('id_salary') self.employee = self.selenium.find_element_by_id( 'id_is_employee') self.button = self.selenium.find_element_by_xpath( "//button[@type='submit']") return AddReviewerPage(self.selenium) @staticmethod def __get_text(element): return element.get_attribute('textContent') @staticmethod def _fill(page): page.username.select_by_visible_text('kossar') page.salary.clear() page.salary.send_keys("2000") def test_successful_radd(self): self.login() page = self.__get_page() self._fill(page) page.button.click() self.assertNotIn("add", self.selenium.current_url) def test_empty_parts_add(self): self.login() page = self.__get_page() self._fill(page) page.username.select_by_visible_text("---------") page.button.click() self.assertIn("add", self.selenium.current_url) def test_already_existing_reviewer(self): self.login() page = self.__get_page() self._fill(page) page.button.click() self.selenium.get('%s%s' % (self.live_server_url, '/admin_panel/reviewers/add')) page = self.__get_page() self._fill(page) page.button.click() self.assertIn("add", self.selenium.current_url)
class NotifTest(StaticLiveServerTestCase): def setUp(self): self.selenium = WebDriver() self.user = User.objects.create_superuser(username="******", email="*****@*****.**", password="******", first_name="kossar", last_name="najafi", phone_number="09147898557", account_number="1234567812345678", notification_preference="S") self.selenium.implicitly_wait(10) self.selenium.get('%s%s' % (self.live_server_url, '/admin_panel/send_notification/')) def tearDown(self): self.selenium.quit() def __get_page(self): class NotifPage(object): def __init__(self, selenium): self.selenium = selenium self.subject = self.selenium.find_element_by_id('id_subject') self.text = self.selenium.find_element_by_id("id_notification_text") buttons = self.selenium.find_elements_by_xpath("//button[@type='submit']") self.send_button = buttons[0] self.cancel_button = buttons[1] return NotifPage(self.selenium) def login(self): user = User.objects.get(username="******") SessionStore = import_module(settings.SESSION_ENGINE).SessionStore session = SessionStore() session[SESSION_KEY] = User.objects.get(username="******").id session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0] session[HASH_SESSION_KEY] = user.get_session_auth_hash() session.save() cookie = { 'name': settings.SESSION_COOKIE_NAME, 'value': session.session_key, 'path': '/', } self.selenium.add_cookie(cookie) self.selenium.refresh() self.selenium.get('%s%s' % (self.live_server_url, '/admin_panel/send_notification/')) @staticmethod def _fill(page): page.subject.send_keys('HEllO') page.text.send_keys('Hi! Welcome to Dollarial') def test_successful_notif(self): self.login() page = self.__get_page() self._fill(page) page.send_button.click() self.assertNotIn("send_notification", self.selenium.current_url) def test_cancel_notif(self): self.login() page = self.__get_page() page.cancel_button.click() self.assertNotIn("send_notification", self.selenium.current_url) @staticmethod def __get_text(element): return element.get_attribute('textContent') def test_empty_parts_add(self): self.login() page = self.__get_page() self._fill(page) page.subject.clear() page.send_button.click() self.assertIn("send_notification", self.selenium.current_url)