Functional testing - python selenium django - Source Code :
from selenium import webdriver
from selenium.webdriver.common.by import By
from django.test import LiveServerTestCase
class Browser(): #(Run more than one testcases on one browser)
browser = None
def __new__(cls):
Browser.browser = Browser.browser or webdriver.Firefox()
return Browser.browser
class Base(LiveServerTestCase):
def setUp(self):
self.browser = Browser()
self.browser.maximize_window()
self.browser.implicitly_wait(10)
self.flag_close_browser = None
self.browser.get(self.live_server_url)
def tearDown(self):
if self.flag_close_browser == 1:
self.browser.quit()
def login(self, user_name, password):
self.find_element(By.NAME, ‘user_name‘).send_keys(user_name)
self.find_element(By.NAME, ‘pwd‘).send_keys(password)
self.find_element(By.TAG_NAME, ‘button‘).submit()
def find_element(self, find_type, element):
obj = self.browser.find_element(find_type, element)
if obj.get_attribute(‘type‘) == ‘text‘ and obj.is_displayed():
obj.clear()
return obj
def find_elements(self, find_type, element):
obj_list = self.browser.find_elements(find_type, element)
for obj in obj_list:
if obj.get_attribute(‘type‘) == ‘text‘ and obj.is_displayed():
obj.clear()
return obj_list