Testing_Unioil_Web/notifications_test.py

165 lines
5.8 KiB
Python

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import sys
import os
# Debug: Print the current working directory and list files
print(f"Current working directory: {os.getcwd()}")
print(f"Files in directory: {os.listdir(os.getcwd())}")
try:
from login import login_to_cms
print("✅ Successfully imported login_to_cms from login.py")
except ImportError as e:
print(f"❌ Failed to import login_to_cms: {e}")
sys.exit(1)
def test_notifications():
driver = None
try:
driver = login_to_cms()
print("Logged in, proceeding to Notifications test")
WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "li.ant-menu-item"))
)
print("Sidebar loaded")
notifications_link = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//li[contains(@class, 'ant-menu-item') and normalize-space()='Notifications']"))
)
driver.execute_script("arguments[0].scrollIntoView(true);", notifications_link)
notifications_link.click()
print("Clicked Notifications tab")
time.sleep(1)
WebDriverWait(driver, 20).until(
EC.visibility_of_element_located((By.XPATH, "//h1[contains(text(), 'Notifications')]"))
)
print("Successfully navigated to Notifications page")
time.sleep(1)
# Click Add Notification button
add_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Add Notification']]"))
)
add_button.click()
print("Clicked Add Notification button")
time.sleep(1)
# Fill in Subject
subject_field = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.NAME, "subject"))
)
subject_field.send_keys("Test")
print("Entered Subject: Test")
time.sleep(1)
# Fill in Content
content_field = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.NAME, "content"))
)
content_field.send_keys("This is a testing notification")
print("Entered Content: This is a testing notification")
time.sleep(1)
# Click "Yes" radio button
yes_radio = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//input[@name='isScheduled' and @value='true']/.."))
)
yes_radio.click()
print("Selected 'Yes' for scheduling")
time.sleep(1)
# Click Schedule Date input
schedule_input = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Schedule']"))
)
schedule_input.click()
print("Clicked Schedule Date input")
time.sleep(1)
# Click "Now" button
now_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(@class, 'ant-calendar-today-btn')]"))
)
now_button.click()
print("Selected 'Now' as schedule")
time.sleep(1)
# Click Expiration Date input
expiration_input = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//input[@placeholder='Expiration']"))
)
expiration_input.click()
print("Clicked Expiration Date input")
time.sleep(1)
# Select day 13
day_13 = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//div[@class='ant-calendar-date' and text()='13']"))
)
day_13.click()
print("Selected expiration date: 13")
time.sleep(1)
# Click Ok
ok_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[@class='ant-calendar-ok-btn']"))
)
ok_button.click()
print("Clicked OK to confirm expiration date")
time.sleep(1)
# Click Submit
submit_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[.//span[text()='Submit']]"))
)
submit_button.click()
print("Clicked Submit button")
time.sleep(3) # Wait a bit for potential error/toast to appear
# Try to detect error or toast message
try:
# Example: Ant Design error/toast uses ant-message or ant-notification
error_element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.XPATH, "//*[contains(@class, 'ant-message') or contains(@class, 'ant-notification')]"))
)
print("⚠️ Error/Toast message detected:")
print(error_element.text)
except:
print("✅ No visible error/toast message detected after submission.")
# Take screenshot after submission attempt
driver.save_screenshot("notification_submitted.png")
print("📸 Screenshot saved as notification_submitted.png")
# Wait 10 seconds before closing
print("Waiting 10 seconds before closing browser...")
time.sleep(10)
return driver
except Exception as e:
print(f"Test failed: {e}")
if driver is not None:
driver.save_screenshot("error.png")
print("Screenshot saved as error.png for debugging")
if driver is not None:
driver.quit()
raise
except KeyboardInterrupt:
print("\nUser interrupted the script. Closing the browser.")
if driver is not None:
driver.quit()
if __name__ == "__main__":
test_notifications()