dashboard_test.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import time
  2. import unittest
  3. import pytest
  4. from urllib.parse import urljoin
  5. from selenium import webdriver
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.common.keys import Keys
  8. from selenium.webdriver.chrome.options import Options
  9. from selenium.webdriver.support.wait import WebDriverWait
  10. from selenium.webdriver.common import utils
  11. @pytest.fixture
  12. def driver():
  13. options = Options()
  14. options.add_argument("--headless")
  15. options.add_argument("--no-sandbox")
  16. _driver = webdriver.Chrome(options=options)
  17. yield _driver
  18. _driver.quit()
  19. @pytest.fixture(autouse=True)
  20. def dashboard_url(dashboard_host, dashboard_port):
  21. count = 0
  22. while utils.is_connectable(port=dashboard_port, host=dashboard_host) is False:
  23. if count == 30:
  24. raise Exception("Dashboard is not ready")
  25. count += 1
  26. time.sleep(1)
  27. return f"http://{dashboard_host}:{dashboard_port}"
  28. @pytest.fixture
  29. def login(driver, dashboard_url):
  30. driver.get(dashboard_url)
  31. assert "EMQX Dashboard" == driver.title
  32. assert f"{dashboard_url}/#/login?to=/dashboard/overview" == driver.current_url
  33. driver.find_element(By.XPATH, "//div[@class='login']//form[1]//input[@type='text']").send_keys("admin")
  34. driver.find_element(By.XPATH, "//div[@class='login']//form[1]//input[@type='password']").send_keys("admin")
  35. driver.find_element(By.XPATH, "//div[@class='login']//form[1]//button[1]").click()
  36. dest_url = urljoin(dashboard_url, "/#/dashboard/overview")
  37. driver.get(dest_url)
  38. ensure_current_url(driver, dest_url)
  39. def ensure_current_url(driver, url):
  40. count = 0
  41. while url != driver.current_url:
  42. if count == 10:
  43. raise Exception(f"Failed to load {url}")
  44. count += 1
  45. time.sleep(1)
  46. def wait_title(driver):
  47. return WebDriverWait(driver, 10).until(lambda x: x.find_element("xpath", "//div[@id='app']//h1[@class='header-title']"))
  48. def test_basic(driver, login, dashboard_url):
  49. driver.get(dashboard_url)
  50. title = wait_title(driver)
  51. assert "Cluster Overview" == title.text
  52. def test_log(driver, login, dashboard_url):
  53. dest_url = urljoin(dashboard_url, "/#/log")
  54. driver.get(dest_url)
  55. ensure_current_url(driver, dest_url)
  56. title = wait_title(driver)
  57. assert "Logging" == title.text
  58. label = driver.find_element(By.XPATH, "//div[@id='app']//form//label[contains(., 'Enable Log Handler')]")
  59. assert driver.find_elements(By.ID, label.get_attribute("for"))
  60. label = driver.find_element(By.XPATH, "//div[@id='app']//form//label[contains(., 'Log Level')]")
  61. assert driver.find_elements(By.ID, label.get_attribute("for"))
  62. label = driver.find_element(By.XPATH, "//div[@id='app']//form//label[contains(., 'Log Formatter')]")
  63. assert driver.find_elements(By.ID, label.get_attribute("for"))
  64. label = driver.find_element(By.XPATH, "//div[@id='app']//form//label[contains(., 'Time Offset')]")
  65. assert driver.find_elements(By.ID, label.get_attribute("for"))