EnglishPal/app/test/helper.py

48 lines
1.6 KiB
Python

import uuid
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import UnexpectedAlertPresentException, NoAlertPresentException
from selenium.webdriver.common.by import By
def signup(URL, driver):
username = 'TestUser' + str(uuid.uuid1()).split('-')[0].title()
password = '[Abc+123]'
driver.get(URL)
try:
# Click the register link
register_link = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.LINK_TEXT, '注册'))
)
register_link.click()
# Fill the registration form
username_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'username'))
)
username_field.send_keys(username)
password_field = driver.find_element(By.ID, 'password')
password_field.send_keys(password)
confirm_password_field = driver.find_element(By.ID, 'password2')
confirm_password_field.send_keys(password)
# Click the register button
register_button = driver.find_element(By.CLASS_NAME, 'btn')
register_button.click()
# Handle possible alert
try:
WebDriverWait(driver, 1).until(EC.alert_is_present())
alert = driver.switch_to.alert
alert.accept()
except (UnexpectedAlertPresentException, NoAlertPresentException):
pass
return username, password
except Exception as e:
print(f"An error occurred: {e}")
return None, None