summaryrefslogtreecommitdiff
path: root/support/ui_utils/profiles/create_js_file.py
blob: b8021ece72811eac5347b588d6e956f7807b3870 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import sys
import time
from selenium.webdriver.common.by import By
from support.ui_utils.element_position.profile_element_position import *
from datetime import datetime 

class CreateJSFile:
    def __init__(self, driver):
        self.driver = driver

    def create_js_file(self, data):
        profile_name = data["name"]
        if "css" in data["script_type"]:
            temp_insertScripts_input_Name_posXpath = insertScripts_input_Name_posXpath
            temp_insertScripts_button_oK_posXpath = insertScripts_button_oK_posXpath
        else:
            temp_insertScripts_input_Name_posXpath = insertScripts_js_input_Name_posXpath
            temp_insertScripts_button_oK_posXpath = insertScripts_js_button_oK_posXpath
        self.driver.find_element(By.XPATH, temp_insertScripts_input_Name_posXpath).clear()
        self.driver.find_element(By.XPATH, temp_insertScripts_input_Name_posXpath).send_keys(profile_name)
        #页面其它选项操作
        result_code = self._operate_page(data, operation_type="create")
        #点击OK
        self.driver.find_element(By.XPATH, temp_insertScripts_button_oK_posXpath).click()
        if self.driver.is_element_exist_by_value(By.XPATH, insertScripts_button_warningSaveYes_posXpath):
            self.driver.find_element(By.XPATH, insertScripts_button_warningSaveYes_posXpath).click()

    def _operate_page(self, data, operation_type="create"):
        """
        除name的其它选项操作,共创建、修改逻辑调用
        :param data:
        :param operation_type: create modify。创建 修改时调用
        :return:
        """
        if operation_type == "create":
            data_index = 0   #新增数据索引
        else:
            data_index = -1   #编辑数据索引
        no_modify = "不修改"
        #script type脚本类型操作 --先操作文件类型

        #self._operate_scriptType(data, data_index=data_index)
        #File文件上传操作
        file = self._get_value_from_data(data, key="script", data_index=data_index)   #解析file文件名称
        if file != no_modify:      #判断修改时不修改时跳过操作。
            if file != "":
                create_file = data["script"].split("->")[data_index]  # 提取上传文件绝对路径
                profile_path = os.path.dirname(os.path.abspath(__file__))  # ...support\ui_utils\profile
                file_abs = os.path.join(profile_path, "profile_file", "insert_scripts", create_file)
                #上传文件
                if "css" in data["script_type"]:
                    temp_insertScripts_input_scriptUpLoad_posXpath = insertScripts_input_scriptUpLoad_posXpath
                else:
                    temp_insertScripts_input_scriptUpLoad_posXpath = insertScripts_js_input_scriptUpLoad_posXpath
                self.driver.find_element(By.XPATH, temp_insertScripts_input_scriptUpLoad_posXpath).send_keys(file_abs)

    def _operate_scriptType(self, data, data_index=0, no_modify="不修改"):
        """
        脚本类型操作,。
        :param data:
        :param data_index: 0 -1来自上层
        :param no_modify:"不修改" 来自上次调用
        :param operation_type: create modify两个状态,
        :return:
        """
        #解析输入data数据的数据,脚本类型
        script_type = self._get_value_from_data(data, key="script_type", data_index=data_index)
        if script_type != no_modify:     #编辑不修改时执行
            if script_type != "":
                self.driver.find_element(By.XPATH, insertScripts_input_scriptType_posXpath).click()   #点击输入列表框
                if "js" == script_type:   #js类型点击
                    self.driver.find_element(By.XPATH, insertScripts_dropdown_jsItem_posXpath).click()
                    #insert position操作
                    insert_position = self._get_value_from_data(data, key="insert_position", data_index=data_index)     #获取insert position等操作After Page Load
                    if insert_position != no_modify:
                        self.driver.find_element(By.XPATH, insertScripts_input_insertPosition_posXpath).click()
                        if ("after" in insert_position) and ("page" in insert_position) and ("load" in insert_position):      #点击 after Page Load选项
                            #点击下拉列表选项
                            self.driver.find_element(By.XPATH, insertScripts_dropdown_afterPageLoadItem_posXpath).click()
                        else:                    #点击 before Page Load选项
                            self.driver.find_element(By.XPATH, insertScripts_dropdown_beforePageLoadItem_posXpath).click()
                else:          #css类型点击列表
                    self.driver.find_element(By.XPATH, insertScripts_dropdown_cssItem_posXpath).click()

    def _get_value_from_data(self, data, key="", data_index=0):
        """
        从data数据中,根据key提取值
        :param data:
        :param key:
        :param data_index: 0 -1
        :return:
        """
        try:
            switch_value: str = data[key].split("->")[data_index].strip().lower()    #用例数据
            return switch_value
        except Exception as e:
            print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"Exception: {e}", flush=True)
            raise