LRR/test/conftest.py

63 lines
2.3 KiB
Python
Raw Normal View History

import os
import pytest
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
@pytest.fixture
def restore_database():
''' Restore the database.
It is useful for making sure that each end-to-end test
starts with the same database.
Benefit: we can reproduce the same test result.
'''
PASSWORD = 'lrr@123' # root password
DB_NAME = 'lrr' # database name used for LRR
HOST = 'localhost'
PORT = 3909 # XAMPP MySQL default port
SOCKET = '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' # XAMPP MySQL socket (optional)
# commands used to import data to DB_NAME
cmds = [
f'mysql -u root -p{PASSWORD} -h {HOST} -P {PORT} --socket={SOCKET} -e "DROP DATABASE IF EXISTS {DB_NAME};"',
f'mysql -u root -p{PASSWORD} -h {HOST} -P {PORT} --socket={SOCKET} -e "CREATE DATABASE {DB_NAME};"',
f'mysql -u root -p{PASSWORD} -h {HOST} -P {PORT} --socket={SOCKET} -e "CREATE USER IF NOT EXISTS \'lrr\'@\'localhost\' IDENTIFIED BY \'lrr@123\';"',
f'mysql -u root -p{PASSWORD} -h {HOST} -P {PORT} --socket={SOCKET} -e "GRANT ALL PRIVILEGES ON {DB_NAME}.* TO lrr@localhost WITH GRANT OPTION;"',
f'mysql -u root -p{PASSWORD} -h {HOST} -P {PORT} --socket={SOCKET} {DB_NAME} < ../../lrr_database_hui.sql'
]
try:
for command in cmds:
print(f"Executing command: {command}")
result = os.system(command)
if result != 0:
raise Exception(f"Command failed with exit code {result}: {command}")
print("Database restored successfully.")
except Exception as e:
print(f"An error occurred while restoring the database: {e}")
return None
@pytest.fixture
def url():
# return 'http://localhost/LRR/' # URL of LRR
return 'http://localhost:8081/LRR-Hui-Organize/' # local URL of LRR-Hui-Organize
@pytest.fixture
def driver():
# Use the locally downloaded ChromeDriver
chrome_driver_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../chromedriver-mac-x64 2/chromedriver'))
print(f"Using ChromeDriver at: {chrome_driver_path}")
driver = webdriver.Chrome(executable_path=chrome_driver_path)
return driver
@pytest.fixture
def admin_username():
return 'admin@qq.com'
@pytest.fixture
def admin_password():
return '123'