dashboard_test.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 title(driver):
  47. return driver.find_element("xpath", "//div[@id='app']//h1[@class='header-title']")
  48. def wait_title_text(driver, text):
  49. return WebDriverWait(driver, 10).until(lambda x: title(x).text == text)
  50. def test_basic(driver, login, dashboard_url):
  51. driver.get(dashboard_url)
  52. wait_title_text(driver, "Cluster Overview")
  53. def test_log(driver, login, dashboard_url):
  54. dest_url = urljoin(dashboard_url, "/#/log")
  55. driver.get(dest_url)
  56. ensure_current_url(driver, dest_url)
  57. wait_title_text(driver, "Logging")
  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"))