Commit 5f7e7d1d authored by JunHyung An's avatar JunHyung An
Browse files

automation check

parent c91e27ca
Loading
Loading
Loading
Loading

load_check.py

0 → 100644
+386 −0
Original line number Diff line number Diff line
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException, TimeoutException
import unittest, time
import threading  # Import the threading module

# Import for Explicit Waits
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class MultipleTest(unittest.TestCase):
    def setUp(self):
        options = webdriver.ChromeOptions()
        options.add_experimental_option("excludeSwitches", ["enable-logging"])
        options.add_argument("--ignore-certificate-errors")
        options.add_argument('--start-maximized')
        options.add_argument('--guest')
        options.add_argument("--window-size=1920,1080")

        # print("webdriver 위치: ")
        #self.webdriver_path = input()
        self.webdriver_path = 'C:\\Users\\watchtek\\Documents\\chromedriver-win64\\chromedriver.exe'

        print("[필수 입력] 테스트 대상 웹 (e.g., https://example.com:8443/): ")
        self.DC_address = input()

        # Get user input for account type, credentials, and duplication count
        print("[필수 입력] 계정유형(1-어드민,2-(지역)관리자,3-사용자) / 계정 접속정보 / 커넥션 비중 (e.g., 1 / monitor, monitor / 4)")
        user_input = input("")

        try:
            parts = user_input.split(' / ')
            self.account_type = int(parts[0].strip())
            credentials = parts[1].split(', ')
            self.username = credentials[0].strip()
            self.password = credentials[1].strip()
            self.duplicate_count = int(parts[2].strip()) if len(parts) > 2 else 1 # Default to 1 if no duplication count is provided
        except (ValueError, IndexError) as e:
            print(f"Invalid input format: {e}. Please use 'type / username, password / duplicate_count'. Exiting.")
            exit(1)

        # Initialize a list to hold driver instances for each thread
        self.drivers = []
        self.verificationErrors = []
        self.accept_next_alert = True

        #

    # ... (rest of your _interact_with_time_filters, _login_action, etc. methods remain unchanged)

    def _interact_with_time_filters(self, driver):
        """
        Automates the interaction with time filter buttons on a webpage using Selenium WebDriver.
        This function specifically targets the time filter dropdown and options found in
        sections like 'Performance/Operation' (성능/운영).

        This function performs the following steps:
        1. Clicks the main dropdown button to reveal time filter options.
        2. Selects either 'Recent 3 Hours' option.
        3. Clicks the main dropdown button again to re-open the options.
        4. Selects the 'Recent 7 Days' option.
        5. Clicks the main dropdown button one more time.
        6. Selects the 'Recent 60 Days' option.

        """
        driver.find_element_by_xpath("//div[@id='page-wrapper']/div/div/div[2]/div/span[2]/div/button[2]/span").click()
        driver.find_element_by_xpath(
            "//div[contains(@class, 'el-popover')]//button[span[contains(text(), '최근 3 시간')]]").click()
        driver.find_element_by_xpath("//div[@id='page-wrapper']/div/div/div[2]/div/span[2]/div/button[2]/span").click()
        recent_7_days_button_xpath = "//div[contains(@class, 'el-popover')]//button[span[contains(text(), '최근 7 일')]]"
        driver.find_element_by_xpath(recent_7_days_button_xpath).click()
        driver.find_element_by_xpath("//div[@id='page-wrapper']/div/div/div[2]/div/span[2]/div/button[2]/span").click()
        recent_60_days_button_xpath = "//div[contains(@class, 'el-popover')]//button[span[contains(text(), '최근 60 일')]]"
        driver.find_element_by_xpath(recent_60_days_button_xpath).click()

    def _interact_with_time_filters_for_la(self, driver):
        """
        Automates the interaction with time filter buttons on a webpage using Selenium WebDriver.
        This function specifically targets the time filter dropdown and options found in
        sections like 'Performance/Operation' (성능/운영).

        This function performs the following steps:
        1. Clicks the main dropdown button to reveal time filter options.
        2. Selects either 'Recent 3 Hours' option.
        3. Clicks the main dropdown button again to re-open the options.
        4. Selects the 'Recent 7 Days' option.
        5. Clicks the main dropdown button one more time.
        6. Selects the 'Recent 60 Days' option.

        """
        wait = WebDriverWait(driver, 5)  # Initialize WebDriverWait

        try:
            dropdown_button = wait.until(EC.element_to_be_clickable(
                (By.XPATH, "//div[@id='page-wrapper']/div/div/div/div/span/div/button[2]/span")))
            dropdown_button.click()
        except TimeoutException:
            print("Timeout waiting for the dropdown button in _interact_with_time_filters_for_la.")

        try:
            recent_6_hours_button = wait.until(EC.element_to_be_clickable(
                (By.XPATH, "//div[contains(@class, 'el-popover')]//button[span[contains(text(), '최근 6 시간')]]")))
            recent_6_hours_button.click()
        except TimeoutException:
            print("Timeout waiting for '최근 6 시간' button in _interact_with_time_filters_for_la.")

        try:
            dropdown_button_reopen = wait.until(EC.element_to_be_clickable(
                (By.XPATH, "//div[@id='page-wrapper']/div/div/div/div/span/div/button[2]/span")))
            dropdown_button_reopen.click()
        except TimeoutException:
            print("Timeout waiting for the dropdown button to re-open in _interact_with_time_filters_for_la.")

        recent_7_days_button_xpath = "//div[contains(@class, 'el-popover')]//button[span[contains(text(), '최근 7 일')]]"
        try:
            recent_7_days_button = wait.until(EC.element_to_be_clickable((By.XPATH, recent_7_days_button_xpath)))
            recent_7_days_button.click()
        except TimeoutException:
            print("Timeout waiting for '최근 7 일' button in _interact_with_time_filters_for_la.")

        try:
            dropdown_button_reopen_2 = wait.until(EC.element_to_be_clickable(
                (By.XPATH, "//div[@id='page-wrapper']/div/div/div/div/span/div/button[2]/span")))
            dropdown_button_reopen_2.click()
        except TimeoutException:
            print(
                "Timeout waiting for the dropdown button to re-open (second time) in _interact_with_time_filters_for_la.")

        recent_60_days_button_xpath = "//div[contains(@class, 'el-popover')]//button[span[contains(text(), '최근 60 일')]]"
        try:
            recent_60_days_button = wait.until(EC.element_to_be_clickable((By.XPATH, recent_60_days_button_xpath)))
            recent_60_days_button.click()
        except TimeoutException:
            print("Timeout waiting for '최근 60 일' button in _interact_with_time_filters_for_la.")

    def _login_action(self, driver, address, id, pw, wait):
        """Performs the login action."""
        driver.get(address)
        time.sleep(0.5)

        wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@type='text']"))).click()
        driver.find_element(By.XPATH, "//input[@type='text']").clear()
        driver.find_element(By.XPATH, "//input[@type='text']").send_keys(id)
        driver.find_element(By.XPATH, "//input[@type='password']").clear()
        driver.find_element(By.XPATH, "//input[@type='password']").send_keys(pw)
        driver.find_element(By.XPATH, "//button[@type='button']").click()
        time.sleep(0.5)

    def _dashboard_actions(self, driver):
        """Performs actions on the Dashboard."""
        driver.find_element_by_xpath("//div[@id='wrapper']/nav/ul/li[1]/a/div").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div/div/article/div/div/div/div/div/div/div/div[2]/div/div/span[2]/span/span").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div/div/article/div/div/div/div/div/div/div/div[2]/div[2]/div/span[2]/span/span").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div/div/article/div/div/div/div/div/div/div/div[2]/div[3]/div/span[2]/span/span").click()

        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div/div/div[2]/label[2]/span/span").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div/div/article/div/div[2]/div/div/div/div/div/div[2]/div/div/span[2]/span/span").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div/div/article/div/div[2]/div/div/div/div/div/div[2]/div[2]/div/span[2]/span/span").click()

    def _topology_map_actions(self, driver):
        """Performs actions on the Topology Map."""
        time.sleep(0.5)
        driver.find_element_by_xpath("//div[@id='wrapper']/nav/ul/li[2]/a/div").click()

        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div/article/div/div/div/div/div/div[2]/div/div/span[2]/span/span[2]").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div/article/div/div/div/div/div/div[2]/div/div[2]/div/div/span[2]/span/span[2]").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div/article/div/div/div/div/div/div[2]/div/div[2]/div/div[2]/div/div/span[2]/span/span[2]").click()

    def _performance_operation_actions(self, driver):
        """Performs actions on Performance/Operation."""
        time.sleep(0.5)
        driver.find_element_by_xpath("//div[@id='wrapper']/nav/ul/li[3]/a/div").click()
        driver.find_element_by_xpath("//div[@id='wrapper']/div/div/aside/div/div/div[2]/label[2]/span/span").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div/span[2]/span").click()
        driver.find_element_by_id("tab-performance").click()

        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div[2]/div/div").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div[2]/div/div[2]/div/div/span[2]/span/span[2]").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div[2]/div/div[2]/div/div[2]/div/div/span[2]/span/span/span/span/span[3]").click()
        analysis_tab = driver.find_element(By.ID, "tab-analysis")
        driver.execute_script("arguments[0].click();", analysis_tab)
        self._interact_with_time_filters(driver)

        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div[2]/div[2]/div/span[2]/span/span[2]").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div[2]/div[2]/div[2]/div/div/span[2]/span/span[2]").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div[2]/div[2]/div[2]/div/div[2]/div/div/span[2]/span/span/span/span/span[3]").click()
        driver.find_element_by_id("tab-analysis").click()
        self._interact_with_time_filters(driver)

        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div[2]/div[3]/div/span[2]/span/span[2]").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div[2]/div[3]/div[2]/div/div/span[2]/span/span[2]").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div[2]/div[3]/div[2]/div/div[2]/div/div/span[2]/span/span/span/span/span[3]").click()
        driver.find_element_by_id("tab-analysis").click()
        self._interact_with_time_filters(driver)

    def _log_analysis_actions(self, driver):
        """Performs actions on Log Analysis."""
        time.sleep(0.5)
        driver.find_element_by_xpath("//div[@id='wrapper']/nav/ul/li[5]/a/div").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div/div/article/div/div/div/div/div/div/span[2]/span/span").click()
        self._interact_with_time_filters_for_la(driver)

    def _event_status_actions(self, driver):
        """Performs actions on Event Status."""
        time.sleep(0.5)
        driver.find_element_by_xpath("//div[@id='wrapper']/nav/ul/li[6]/a/div").click()
        driver.find_element_by_id("tab-event-status").click()
        driver.find_element_by_xpath("//div[@id='wrapper']/div/div/aside/div/div/div[2]/label[2]/span/span").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div/span[2]/span/span[2]").click()

        driver.find_element_by_id("tab-event-hist").click()
        driver.find_element_by_xpath("//div[@id='wrapper']/div/div/aside/div/div/div[2]/label[2]/span/span").click()
        driver.find_element_by_xpath(
            "//div[@id='wrapper']/div/div/aside/div/div/div[3]/div[2]/div/div/div/div/div/div/span[2]/span/span[2]").click()
        self._interact_with_time_filters(driver)

    def _work_management_actions(self, driver, wait):
        """Performs actions on Work Management."""
        time.sleep(0.5)
        wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@id='wrapper']/nav/ul/li[7]/a/div"))).click()
        driver.find_element_by_xpath(
            "//div[@id='page-wrapper']/div/div[2]/div/div/div/div/div/div/div/label/span").click()
        driver.find_element_by_xpath(
            "//div[@id='page-wrapper']/div/div[2]/div/div/div/div/div/div/div/label[2]/span").click()
        driver.find_element_by_xpath(
            "//div[@id='page-wrapper']/div/div[2]/div/div/div/div/div/div/div/label[3]/span").click()

    def _admin_settings_actions(self, driver):
        """Performs admin-specific settings actions."""
        time.sleep(0.5)
        driver.find_element_by_xpath("//div[@id='wrapper']/nav/span/a/div").click()
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='신규 장비 등록 및 관리 여부 설정이 가능합니다.'])[1]/following::button[1]").click()

        time.sleep(0.5)
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='네트워크'])[1]/following::li[1]").click()
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='서버'])[1]/following::li[1]").click()
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='시설장비'])[1]/following::li[1]").click()

        time.sleep(0.5)
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='초기 설정'])[1]/following::span[1]").click()
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize(.)='기준 정보 편집'])[1]/following::span[1]").click()
        driver.find_element_by_xpath(
            u"(.//*[normalize-space(text()) and normalize-space(.)='수집/판단 기준'])[1]/following::li[1]").click()

    def _run_driver1_actions(self, driver, address, id, pw, account_type):
        wait = WebDriverWait(driver, 10)

        self._login_action(driver, address, id, pw, wait)
        self._dashboard_actions(driver)
        self._topology_map_actions(driver)
        self._performance_operation_actions(driver)
        self._log_analysis_actions(driver)
        self._event_status_actions(driver)
        self._work_management_actions(driver, wait)

        # Only run admin actions if the account_type is 1 (admin)
        if account_type == 1:
            # self._admin_settings_actions(driver)
            print("Admin actions executed.")
        elif account_type == 2:
            print("Manager account - no specific admin actions.")
        elif account_type == 3:
            print("User account - no specific admin actions.")

        time.sleep(1)

    def _run_single_driver_actions(self, address, id, pw, account_type, driver_instance_number):
        # Create a new driver instance for this thread
        options = webdriver.ChromeOptions()
        options.add_experimental_option("excludeSwitches", ["enable-logging"])
        options.add_argument("--ignore-certificate-errors")
        options.add_argument('--start-maximized')
        options.add_argument('--guest')
        options.add_argument("--window-size=1920,1080")

        driver = webdriver.Chrome(executable_path=self.webdriver_path, options=options)
        driver.implicitly_wait(10)  # Common implicit wait for this driver
        self.drivers.append(driver)  # Add this driver to the list for tearDown

        wait = WebDriverWait(driver, 10)

        print(f"Starting actions for session {driver_instance_number} with user: {id}")
        self._login_action(driver, address, id, pw, wait)
        self._dashboard_actions(driver)
        self._topology_map_actions(driver)
        self._performance_operation_actions(driver)
        self._log_analysis_actions(driver)
        self._event_status_actions(driver)
        self._work_management_actions(driver, wait)

        if account_type == 1:
            self._admin_settings_actions(driver)
            print(f"Admin actions executed for session {driver_instance_number}.")
        elif account_type == 2:
            print(f"Manager account actions for session {driver_instance_number}.")
        elif account_type == 3:
            print(f"User account actions for session {driver_instance_number}.")

        time.sleep(1)  # Keep the browser open for a short duration after actions

    def test_multiple(self):
        address = self.DC_address
        username = self.username
        password = self.password
        account_type = self.account_type
        duplicate_count = self.duplicate_count

        threads = []
        for i in range(duplicate_count):
            # Pass unique driver instance to each thread
            thread = threading.Thread(target=self._run_single_driver_actions, args=(address, username, password, account_type, i + 1))
            threads.append(thread)
            thread.start()
            # Add a small delay between launching threads to prevent immediate resource contention
            time.sleep(0.5)

        for thread in threads:
            thread.join() # Wait for all threads to complete

    def is_element_present(self, how, what):
        try:
            self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e:
            return False
        return True

    def is_alert_present(self):
        try:
            self.driver.switch_to.alert
        except NoAlertPresentException as e:
            return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to.alert
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally:
            self.accept_next_alert = True

    def tearDown(self):
        # Quit all driver instances that were created
        for driver in self.drivers:
            try:
                driver.quit()
            except Exception as e:
                print(f"Error quitting driver: {e}")
        self.assertEqual([], self.verificationErrors)


if __name__ == "__main__":
    unittest.main()
 No newline at end of file