summaryrefslogtreecommitdiff
path: root/support/ui_utils/my_web_driver.py
blob: 6b362fcdd2999d6128794a9ac0414650b9f43707 (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
105
106
107
108
109
110
111
import time
import traceback
import inspect
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from typing import Optional
from selenium.webdriver.support import expected_conditions as EC
from datetime import datetime 

class MyWebDriver(webdriver.Remote):
    def find_element(self, by=By.ID, value: Optional[str] = None, find_before_wait_time=0.3, find_after_wait_time=0.3, wait_timeout=10):
        """
        封装find_element方法:
        :param by:
        :param value:
        :param find_before_wait_time:   开始查找元素前等待时间
        :param find_after_wait_time:    元素查找后等待时间
        :param wait_timeout:        元素等待超时时间
        :return:
        """
        # 获取当前调用栈
        current_stack = inspect.stack()
        # 调用栈的第一个元素是当前的函数调用(my_function),
        # 第二个元素是调用my_function的地方
        caller_frame_record = current_stack[1]
        # 获取调用者的文件名、行号、函数名等信息
        caller_filename = caller_frame_record.filename
        caller_lineno = caller_frame_record.lineno
        time_1 = time.time()
        if wait_timeout > 60:
            wait_timeout = 60
        while wait_timeout > 0:
            time.sleep(find_before_wait_time)
            wait_timeout = wait_timeout - (find_before_wait_time if find_before_wait_time > 0 else 2.5)
            try:
                self.element = super().find_element(by, value)
            except:
                #print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"{caller_filename}. {caller_lineno} line. find element: {value}. find method: {by}.", flush=True)
                throw_assert = 1
                continue
            else:
                time.sleep(find_after_wait_time)
                throw_assert = 0
                break   #找到则退出循环
        time_2 = time.time()
        search_time = time_2 - time_1
        search_time = round(search_time, 1)
        #print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f" {caller_filename}. {caller_lineno} line. find element:{value}. find method: {by}. find take time:{search_time} second.", flush=True)
        if throw_assert == 1:
            method_info = '{{"method":"{}","path":"{}"}}'.format(by, value)
            find_element_error = "Element not found.The element on line {} of {} took {} seconds and was not found.The element search method is {}".format(caller_lineno, caller_filename, search_time, method_info)
            print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], find_element_error, flush=True)
            raise ValueError(find_element_error)
        return self.element

    def find_elements(self, by=By.ID, value: Optional[str] = None, find_before_wait_time=0.3, find_after_wait_time=0.3, wait_timeout=10):
        # 获取当前调用栈
        current_stack = inspect.stack()
        # 调用栈的第一个元素是当前的函数调用(my_function),
        # 第二个元素是调用my_function的地方
        caller_frame_record = current_stack[1]
        # 获取调用者的文件名、行号、函数名等信息
        caller_filename = caller_frame_record.filename
        caller_lineno = caller_frame_record.lineno
        time_1 = time.time()
        if wait_timeout > 60:
            wait_timeout = 60
        while wait_timeout > 0:
            time.sleep(find_before_wait_time)
            wait_timeout = wait_timeout - (find_before_wait_time if find_before_wait_time > 0 else 2.5)
            try:
                self.element = super().find_elements(by, value)
            except:
                #print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f"{caller_filename}. {caller_lineno} line. find element: {value}. find method: {by}.")
                throw_assert = 1
                continue
            else:
                time.sleep(find_after_wait_time)
                throw_assert = 0
                break   #找到则退出循环
        time_2 = time.time()
        search_time = time_2 - time_1
        search_time = round(search_time, 1)
        #print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], f" {caller_filename}. {caller_lineno} line. find element:{value}. find method: {by}. find take time:{search_time} second.", flush=True)
        if throw_assert == 1:
            method_info = '{{"method":"{}","path":"{}"}}'.format(by, value)
            find_element_error = "Element not found.The element on line {} of {} took {} seconds and was not found.The element search method is {}".format(caller_lineno, caller_filename, search_time, method_info)
            print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], find_element_error, flush=True)
            raise ValueError(find_element_error)
        return self.element

    def is_element_exist(self, Element, timeout=3):
        # xpath定位元素是否存在
        try:
            WebDriverWait(super(), timeout).until(EC.presence_of_element_located((By.XPATH, Element)))
            self.is_existence = True                # 找到==》True
        except:
            self.is_existence = False               # 找不到===》Flase
        return self.is_existence

    def is_element_exist_by_value(self, by=By.ID, value: Optional[str] = None, timeout=3):
        try:
            WebDriverWait(super(), timeout).until(EC.presence_of_element_located((by, value)))
            is_existence = True                # 找到==》True
        except:
            is_existence = False               # 找不到===》Flase
        return is_existence

if __name__ == '__main__':
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "test")