Source code for django_webapps_fullstack.test.test_account_password_change
"""test program for the django template "password_change"based on the "unittest" and "selenium" functionality"""importosimportsysimportconfigparserimportunittestfromseleniumimportwebdriverfromselenium.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 email of the customer used as the logincustom_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_password_change(unittest.TestCase):""" Class for the test cases of the django template "password_change" """
[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 "password_change" """# find the user for whom the password should be updatedres_users_id=models.execute_kw(db,uid,pword,'res.users','search',[[['login','=',custom_login],['x_activate_complete','=',False],]])# if a user exist:# update of 'x_activate_complete' so that the password could be uptaded forxinres_users_id:id=xresult=models.execute_kw(db,uid,pword,'res.users','write',[[id],{'x_activate_complete':True}])# login of the user to create the request session# 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 email and 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 "dashboard" wait=WebDriverWait(self.driver,20)wait.until(EC.title_contains(custom_title_dashboard))# assertion to test that template "Dashboard" is called after submitassertcustom_title_dashboardinself.driver.title# user has been logged into, "password_change" procedure could be started# url of template "password_change" = '/account/password_change'self.driver.get(custom_url+'/account/password_change')# assertion to confirm that the title has the keyword in itassert'Change Password'inself.driver.title# search and enter email and submit button password_current=self.driver.find_element(By.ID,'id_password_current')password_new=self.driver.find_element(By.ID,'id_password_new')password_confirm=self.driver.find_element(By.ID,'id_password_confirm')button=self.driver.find_element(By.CLASS_NAME,'btn-primary')password_current.send_keys(custom_password)password_new.send_keys(custom_password)password_confirm.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("Password Change Done"))# assertion to test that template "password_recovery_done" is called after submitassert"Password Change Done"inself.driver.title