import time
import pytest
import uuid
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import UnexpectedAlertPresentException, NoAlertPresentException, NoSuchElementException, TimeoutException

def signup(URL, driver):
    username = 'TestUser' + str(uuid.uuid1()).split('-')[0].title()
    password = '[Abc+123]'

    driver.get(URL)
    try:
        elem = driver.find_element_by_link_text('注册')
        elem.click()
        elem = driver.find_element_by_id('username')
        elem.send_keys(username)
        elem = driver.find_element_by_id('password')
        elem.send_keys(password)
        elem = driver.find_element_by_id('password2')
        elem.send_keys(password)    
        elem = driver.find_element_by_class_name('btn')
        elem.click()

        WebDriverWait(driver, 10).until(EC.alert_is_present())
        driver.switch_to.alert.accept()
        print(f"Registration successful: {username}")
    except (UnexpectedAlertPresentException, NoAlertPresentException, TimeoutException, NoSuchElementException) as e:
        print(f"Error during signup: {e}")
        driver.quit()
        raise
    
    return username, password

def test_bug561():
    driver = webdriver.Edge()
    try:
        driver.maximize_window()
        base_url = "http://118.25.96.118:90/"
        username, password = signup(base_url, driver)

        article = driver.find_element(By.ID, 'article')
        actions = ActionChains(driver)
        actions.move_to_element(article)
        actions.click_and_hold()
        actions.move_by_offset(450, 200)
        actions.release()
        actions.perform()
        print("Performed actions on article.")

        next_button = driver.find_element(By.ID, 'load_next_article')
        next_button.click()
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'load_pre_article')))
        print("Clicked next article button.")

        prev_button = driver.find_element(By.ID, 'load_pre_article')
        prev_button.click()
        print("Clicked previous article button.")
    finally:
        driver.quit()
        print("Driver closed.")