uploadstorageconfig.py 3.9 KB
# -*- coding: utf-8 -*-
# @Time    :  2020/12/22 14:33
# @Author  :  Young Lee
# @Email   :  young_lee2017@163.com

from jumppage import jump_page
from selenium.webdriver.common.keys import Keys
import time


def upload_storage_config(driver, config_name, config_path):
    """上传存储配置

    :param config_name: 存储配置名称
    :param config_path: 存储配置路径
    :return: first_sto_name 已上传的第一个存储配置名称 或者失败信息
    """
    existing = False
    jump_page(driver, "资源管理", "存储配置")

    # 输入存储配置名称进行查询过滤
    search_ele = driver.find_element_by_css_selector("div[class='content']>div input[placeholder='请输入存储配置名称']")
    search_ele.send_keys(Keys.CONTROL, 'a')
    search_ele.send_keys(config_name)
    driver.find_element_by_xpath("//div[@class='content']/div[1]//button/span[text()='查询']").click()
    time.sleep(1)

    res_class = driver.find_element_by_css_selector(".el-table__body-wrapper>*:nth-last-child(1)").get_attribute('class')
    if res_class != 'el-table__empty-block':
        # 获取页数
        page_common_xpath = "//div[@class='content']/div[2]//ul[@class='el-pager']"
        page_ele = driver.find_element_by_xpath(f"{page_common_xpath}/li[last()]")
        page_nums = int(page_ele.get_attribute("textContent"))

        for page_num in range(1, page_nums+1):
            if page_num != 1:
                driver.find_element_by_xpath(f"{page_common_xpath}/li[{page_num}]").click()
                time.sleep(0.5)
            sto_eles = driver.find_elements_by_css_selector("tbody>tr>td:nth-child(1)>div")
            storage_list = [sto_ele.get_attribute("textContent") for sto_ele in sto_eles]
            print(storage_list)
            if config_name in storage_list:
                print("存在同名存储配置")
                existing = True

    # 添加存储配置
    driver.find_element_by_xpath("//div[@class='content']/div[1]//button/span[text()='添加']").click()

    # 清除已存在的存储配置上传列表
    while True:
        child_nodes_num = driver.execute_script("return document.getElementsByClassName('el-upload-list')[0].childNodes.length")
        if child_nodes_num == 0:
            break
        # 若存在存储配置文件,则清除
        driver.find_element_by_class_name("el-upload-list__item-name").click()
        driver.find_element_by_class_name("el-upload-list__item").send_keys(Keys.DELETE)
        time.sleep(0.5)


    # 输入存储配置名称
    name_input_ele = driver.find_element_by_css_selector("div[aria-label='添加'] input[type='text']")
    name_input_ele.send_keys(Keys.CONTROL, 'a')
    name_input_ele.send_keys(config_name)
    # 输入存储配置文件路径
    driver.find_element_by_css_selector("div[aria-label='添加'] input[type='file']").send_keys(config_path)
    time.sleep(0.5)

    # 点击上传
    driver.find_element_by_xpath("//div[@class='el-dialog__footer']//span[text()='上 传']").click()

    # 捕捉响应消息
    upload_msg_class = driver.find_element_by_css_selector("div[role='alert']>i").get_attribute('class')
    if 'el-icon-error' in upload_msg_class:
        # 关闭上传窗口,返回错误信息
        driver.find_element_by_css_selector("div[aria-label='添加']>.el-dialog__header>button[aria-label='Close']").click()
        return driver.find_element_by_css_selector('.el-message__content').get_attribute('textContent') if existing else '上传失败'

    first_sto_name = driver.find_element_by_css_selector(
        "tbody>tr>td:nth-child(1)>div:nth-child(1)"
    ).get_attribute("textContent")

    return first_sto_name


if __name__ == "__main__":
    from unitinit import *
    from login import login_fx

    login_fx(driver, login_url, user, passwd)
    for i in range(100):
        first_sto_name = upload_storage_config(driver, "测试MIwue", storage_config_path)
        print(first_sto_name)
    driver.quit()