Source code for django_webapps_fullstack.test.test_account_login

"""
test program for the django template "login"
based on the "unittest" and "selenium" functionality
"""


import os
import sys
import configparser
import unittest
import time
from selenium                        import webdriver
from selenium.webdriver.common.by    import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support      import expected_conditions as EC


# ------- get the parameter to log onto odoo Webservice API

# Get the path to the parent directory
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
# Define the directory 'settings' under the parent directory that should be added
sub_dir = os.path.join(parent_dir, 'settings')
# Append this directory to sys.path
sys.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"
from get_settings_odoo import get_settings_odoo
result   = 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 directory
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
# Define the directory 'settings' under the parent directory that should be added
sub_dir = os.path.join(parent_dir, 'settings')
# file is in a subdirectory under the parent directory
pathname = sub_dir

# Read parameter_test.ini
filename_test  = "parameter_test.ini"
fullpath_test  = os.path.join(pathname, filename_test)
config_test = configparser.ConfigParser()
config_test.read(fullpath_test)

# Read parameter_global.ini
filename_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 tested
custom_url = config_global.get('settings_django_server', 'url')
#  get the dedicated login of the customer
custom_login = config_test.get('get_settings_customer', 'login')
#  get the dedicated password of the customer
custom_password = config_test.get('get_settings_customer', 'password')
#  get the title of the "login" template
custom_title_login = config_test.get('get_settings_title', 'title_login')
#  get the title of the "dashboard" template
custom_title_dashboard = config_test.get('get_settings_title', 'title_dashboard')
#  get the window size for the browser
custom_window_size = config_test.get('get_settings_browser', 'window_size')



[docs] class test_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] def setUp(self): options = webdriver.ChromeOptions() # runs the browser in headless mode options.add_argument('-headless') # pages render at a realistic size and elements are on-screen/clickable options.add_argument(f'--window-size={custom_window_size}') # set the browser preferred language to english to display web pages in english options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'}) self.driver = webdriver.Chrome(options=options)
[docs] def tearDown(self): # Close the browser window self.driver.quit() # Properly clean up test environment super().tearDown()
[docs] def test_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 issues res_users_id = models.execute_kw(db, uid, pword, 'res.users', 'search', [[['login', '=', custom_login]]]) # Always set activation to True to ensure consistent state if res_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 visible time.sleep(30) # Extra verification: confirm activation is visible in database for _ in range(3): check = models.execute_kw(db, uid, pword, 'res.users', 'search_read', [[['login', '=', custom_login]]], {'fields': ['x_activate_complete']}) if check and check[0].get('x_activate_complete'): break time.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 it assert custom_title_login in self.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 submit assert custom_title_dashboard in self.driver.title
if __name__ == "__main__": unittest.main()