Source code for django_webapps_fullstack.test.test_account_login
"""test program for the django template "login"based on the "unittest" and "selenium" functionality"""importosimportsysimportconfigparserimportunittestimporttimefromseleniumimportwebdriverfromselenium.webdriver.common.byimportByfromselenium.webdriver.support.waitimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasEC# ------- get the parameter to log onto odoo Webservice API# Get the path to the parent directoryparent_dir=os.path.abspath(os.path.join(os.path.dirname(__file__),'..'))# Define the directory 'settings' under the parent directory that should be addedsub_dir=os.path.join(parent_dir,'settings')# Append this directory to sys.pathsys.path.append(sub_dir)# call the function "get_settings_odoo" to get the parameters# for the logging of the model on the "odoo Webservice API"fromget_settings_odooimportget_settings_odooresult=get_settings_odoo()db=result[0]uid=result[1]pword=result[3]models=result[4]# ------- get some parameter for the test# Name and full path of the "ini" file# Get the path to the parent directoryparent_dir=os.path.abspath(os.path.join(os.path.dirname(__file__),'..'))# Define the directory 'settings' under the parent directory that should be addedsub_dir=os.path.join(parent_dir,'settings')# file is in a subdirectory under the parent directorypathname=sub_dir# Read parameter_test.inifilename_test="parameter_test.ini"fullpath_test=os.path.join(pathname,filename_test)config_test=configparser.ConfigParser()config_test.read(fullpath_test)# Read parameter_global.inifilename_global="parameter_global.ini"fullpath_global=os.path.join(pathname,filename_global)config_global=configparser.ConfigParser()config_global.read(fullpath_global)# get the url of the webserver to be testedcustom_url=config_global.get('settings_django_server','url')# get the dedicated login of the customercustom_login=config_test.get('get_settings_customer','login')# get the dedicated password of the customercustom_password=config_test.get('get_settings_customer','password')# get the title of the "login" templatecustom_title_login=config_test.get('get_settings_title','title_login')# get the title of the "dashboard" templatecustom_title_dashboard=config_test.get('get_settings_title','title_dashboard')# get the window size for the browsercustom_window_size=config_test.get('get_settings_browser','window_size')
[docs]classtest_account_login(unittest.TestCase):""" Class for the test cases of the django template "login". Test if a login of the account is successful If successful, the "dashboard" template should be shown afterwards. """
[docs]defsetUp(self):options=webdriver.ChromeOptions()# runs the browser in headless modeoptions.add_argument('-headless')# pages render at a realistic size and elements are on-screen/clickableoptions.add_argument(f'--window-size={custom_window_size}')# set the browser preferred language to english to display web pages in englishoptions.add_experimental_option('prefs',{'intl.accept_languages':'en,en_US'})self.driver=webdriver.Chrome(options=options)
[docs]deftearDown(self):# Close the browser windowself.driver.quit()# Properly clean up test environmentsuper().tearDown()
[docs]deftest_cases(self):""" test cases for the template "login" """# Ensure the test user is activated (always, not just if x_activate_complete=False)# This avoids Django/Odoo sync timing issuesres_users_id=models.execute_kw(db,uid,pword,'res.users','search',[[['login','=',custom_login]]])# Always set activation to True to ensure consistent stateifres_users_id:models.execute_kw(db,uid,pword,'res.users','write',[[res_users_id[0]],{'x_activate_complete':True}])# Give Django/Odoo time to sync the activation change# Production system requires significantly more time than development# Wait with verification loop to ensure change is visibletime.sleep(30)# Extra verification: confirm activation is visible in databasefor_inrange(3):check=models.execute_kw(db,uid,pword,'res.users','search_read',[[['login','=',custom_login]]],{'fields':['x_activate_complete']})ifcheckandcheck[0].get('x_activate_complete'):breaktime.sleep(2)# url of template "login" = '/account/login'self.driver.get(custom_url+'/account/login')# assertion to confirm that the title has the keyword in itassertcustom_title_logininself.driver.title# search and enter login and password and push the submit button login=self.driver.find_element(By.ID,'id_login')password=self.driver.find_element(By.ID,'id_password')button=self.driver.find_element(By.CLASS_NAME,'btn-primary')login.send_keys(custom_login)password.send_keys(custom_password)button.click()# wait for the change to the expected title of the new template wait=WebDriverWait(self.driver,10)wait.until(EC.title_contains(custom_title_dashboard))# assertion to test that template "dashboard" is called after submitassertcustom_title_dashboardinself.driver.title