dashboard_test.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import os
  2. import time
  3. import unittest
  4. import pytest
  5. from urllib.parse import urljoin
  6. from selenium import webdriver
  7. from selenium.webdriver.common.by import By
  8. from selenium.webdriver.common.keys import Keys
  9. from selenium.webdriver.chrome.options import Options
  10. from selenium.webdriver.support.wait import WebDriverWait
  11. from selenium.webdriver.common import utils
  12. @pytest.fixture
  13. def driver():
  14. options = Options()
  15. options.add_argument("--headless")
  16. options.add_argument("--no-sandbox")
  17. _driver = webdriver.Chrome(options=options)
  18. yield _driver
  19. _driver.quit()
  20. @pytest.fixture(autouse=True)
  21. def dashboard_url(dashboard_host, dashboard_port):
  22. count = 0
  23. while utils.is_connectable(port=dashboard_port, host=dashboard_host) is False:
  24. if count == 30:
  25. raise Exception("Dashboard is not ready")
  26. count += 1
  27. time.sleep(1)
  28. return f"http://{dashboard_host}:{dashboard_port}"
  29. @pytest.fixture
  30. def login(driver, dashboard_url):
  31. driver.get(dashboard_url)
  32. assert "EMQX Dashboard" == driver.title
  33. assert f"{dashboard_url}/#/login?to=/dashboard/overview" == driver.current_url
  34. driver.find_element(By.XPATH, "//div[@class='login']//form[1]//input[@type='text']").send_keys("admin")
  35. driver.find_element(By.XPATH, "//div[@class='login']//form[1]//input[@type='password']").send_keys("admin")
  36. driver.find_element(By.XPATH, "//div[@class='login']//form[1]//button[1]").click()
  37. dest_url = urljoin(dashboard_url, "/#/dashboard/overview")
  38. driver.get(dest_url)
  39. ensure_current_url(driver, dest_url)
  40. def ensure_current_url(driver, url):
  41. count = 0
  42. while url != driver.current_url:
  43. if count == 10:
  44. raise Exception(f"Failed to load {url}")
  45. count += 1
  46. time.sleep(1)
  47. def title(driver):
  48. return driver.find_element("xpath", "//div[@id='app']//h1[@class='header-title']")
  49. def wait_title_text(driver, text):
  50. return WebDriverWait(driver, 10).until(lambda x: title(x).text == text)
  51. def test_basic(driver, login, dashboard_url):
  52. driver.get(dashboard_url)
  53. wait_title_text(driver, "Cluster Overview")
  54. def test_log(driver, login, dashboard_url):
  55. dest_url = urljoin(dashboard_url, "/#/log")
  56. driver.get(dest_url)
  57. ensure_current_url(driver, dest_url)
  58. wait_title_text(driver, "Logging")
  59. label = driver.find_element(By.XPATH, "//div[@id='app']//form//label[contains(., 'Enable Log Handler')]")
  60. assert driver.find_elements(By.ID, label.get_attribute("for"))
  61. label = driver.find_element(By.XPATH, "//div[@id='app']//form//label[contains(., 'Log Level')]")
  62. assert driver.find_elements(By.ID, label.get_attribute("for"))
  63. label = driver.find_element(By.XPATH, "//div[@id='app']//form//label[contains(., 'Log Formatter')]")
  64. assert driver.find_elements(By.ID, label.get_attribute("for"))
  65. label = driver.find_element(By.XPATH, "//div[@id='app']//form//label[contains(., 'Time Offset')]")
  66. assert driver.find_elements(By.ID, label.get_attribute("for"))
  67. def test_docs_link(driver, login, dashboard_url):
  68. dest_url = urljoin(dashboard_url, "/#/dashboard/overview")
  69. driver.get(dest_url)
  70. ensure_current_url(driver, dest_url)
  71. xpath_link_help = "//div[@id='app']//div[@class='nav-header']//a[contains(@class, 'link-help')]"
  72. link_help = driver.find_element(By.XPATH, xpath_link_help)
  73. driver.execute_script("arguments[0].click();", link_help)
  74. emqx_name = os.getenv("EMQX_NAME")
  75. emqx_community_version = os.getenv("EMQX_COMMUNITY_VERSION")
  76. emqx_enterprise_version = os.getenv("EMQX_ENTERPRISE_VERSION")
  77. if emqx_name == 'emqx-enterprise':
  78. emqx_version = f"v{emqx_enterprise_version}"
  79. docs_base_url = "https://docs.emqx.com/en/enterprise"
  80. else:
  81. emqx_version = f"v{emqx_community_version}"
  82. docs_base_url = "https://www.emqx.io/docs/en"
  83. emqx_version = ".".join(emqx_version.split(".")[:2])
  84. docs_url = f"{docs_base_url}/{emqx_version}"
  85. xpath = f"//div[@id='app']//div[@class='nav-header']//a[@href[starts-with(.,'{docs_url}')]]"
  86. assert driver.find_element(By.XPATH, xpath)