summaryrefslogtreecommitdiff
path: root/common/driver_common
diff options
context:
space:
mode:
Diffstat (limited to 'common/driver_common')
-rw-r--r--common/driver_common/__init__.py0
-rw-r--r--common/driver_common/__pycache__/__init__.cpython-38.pycbin0 -> 144 bytes
-rw-r--r--common/driver_common/__pycache__/mywebdriver.cpython-38.pycbin0 -> 1879 bytes
-rw-r--r--common/driver_common/__pycache__/random_name.cpython-38.pycbin0 -> 1699 bytes
-rw-r--r--common/driver_common/__pycache__/screenshot.cpython-38.pycbin0 -> 1564 bytes
-rw-r--r--common/driver_common/mywebdriver.py58
-rw-r--r--common/driver_common/random_name.py48
-rw-r--r--common/driver_common/recovery.py25
-rw-r--r--common/driver_common/screenshot.py39
9 files changed, 170 insertions, 0 deletions
diff --git a/common/driver_common/__init__.py b/common/driver_common/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/common/driver_common/__init__.py
diff --git a/common/driver_common/__pycache__/__init__.cpython-38.pyc b/common/driver_common/__pycache__/__init__.cpython-38.pyc
new file mode 100644
index 00000000..38bc323b
--- /dev/null
+++ b/common/driver_common/__pycache__/__init__.cpython-38.pyc
Binary files differ
diff --git a/common/driver_common/__pycache__/mywebdriver.cpython-38.pyc b/common/driver_common/__pycache__/mywebdriver.cpython-38.pyc
new file mode 100644
index 00000000..8e258f8b
--- /dev/null
+++ b/common/driver_common/__pycache__/mywebdriver.cpython-38.pyc
Binary files differ
diff --git a/common/driver_common/__pycache__/random_name.cpython-38.pyc b/common/driver_common/__pycache__/random_name.cpython-38.pyc
new file mode 100644
index 00000000..ce517551
--- /dev/null
+++ b/common/driver_common/__pycache__/random_name.cpython-38.pyc
Binary files differ
diff --git a/common/driver_common/__pycache__/screenshot.cpython-38.pyc b/common/driver_common/__pycache__/screenshot.cpython-38.pyc
new file mode 100644
index 00000000..1c1f63c7
--- /dev/null
+++ b/common/driver_common/__pycache__/screenshot.cpython-38.pyc
Binary files differ
diff --git a/common/driver_common/mywebdriver.py b/common/driver_common/mywebdriver.py
new file mode 100644
index 00000000..cb8dadaf
--- /dev/null
+++ b/common/driver_common/mywebdriver.py
@@ -0,0 +1,58 @@
+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
+
+
+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):
+ 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:
+ caller_info = inspect.stack()[1]
+ caller_filename = caller_info.filename
+ caller_line_number = caller_info.frame
+ caller_code_context = caller_info.code_context
+ print(caller_filename, caller_line_number, caller_code_context)
+ print(f"查找方法:{by};-->>", f"查找元素:{value}\n")
+ throw_assert = 1
+ assert_info = traceback.format_exc()
+ continue
+ else:
+ time.sleep(find_after_wait_time)
+ throw_assert = 0
+ break #找到则退出循环
+ if throw_assert == 1:
+ print(assert_info)
+ raise
+ return self.element
+
+
+
+if __name__ == '__main__':
+ chrome_option = webdriver.ChromeOptions()
+ driver = MyWebDriver(
+ command_executor="http://192.168.64.34:4444",
+ options=chrome_option
+ )
+ driver.implicitly_wait(5)
+ driver.get("http://192.168.44.72")
+ driver.find_element(By.NAME, "username1").send_keys("zcw3")
+ driver.find_element(By.NAME, "username").send_keys("zcw3")
+ driver.find_element(By.NAME, "username").send_keys("zcw3")
+ driver.find_element(By.NAME, "username").send_keys("zcw3")
+ driver.find_element(By.NAME, "password").send_keys("111111")
+ driver.find_element(By.NAME, "password").send_keys("111111")
+ driver.find_element(By.NAME, "password").send_keys("111111")
+ driver.find_element(By.NAME, "password").send_keys("111111")
+
+ driver.quit()
diff --git a/common/driver_common/random_name.py b/common/driver_common/random_name.py
new file mode 100644
index 00000000..8f5327f0
--- /dev/null
+++ b/common/driver_common/random_name.py
@@ -0,0 +1,48 @@
+import random
+import string
+import time
+import datetime
+
+class RandomName:
+ """
+ 生成随机使用名称
+ """
+ def __init__(self, pro_name="test_uitemp", r_letter_count=8, r_number_count=8):
+ self.pro_name = pro_name
+ self.r_letter_count = r_letter_count
+ self.r_number_count = r_number_count
+
+ def random_name(self):
+ letters = self.random_letter(self.r_letter_count)
+ numbers = self.random_number(self.r_number_count)
+ format_time = self.current_time()
+ r_name = f"{self.pro_name}_{letters}_{numbers}_{format_time}"
+ return r_name
+
+ def random_letter(self, count=8):
+ temp_list = []
+ for i in range(count):
+ temp_list.append(random.choice(string.ascii_letters))
+ r_letter = "".join(temp_list)
+ #print(r_letter)
+ return r_letter
+
+ def random_number(self, count=8):
+ temp_list = []
+ for i in range(count):
+ temp_list.append(random.choice(string.digits))
+ r_number = "".join(temp_list)
+ #print(r_number)
+ return r_number
+
+ def current_time(self):
+ c_time = time.time()
+ formatted_time = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d-%H-%M-%S')
+ return formatted_time
+
+# r = RandomName()
+# c = r.random_name()
+# print(c)
+# print(c)
+# print(c)
+
diff --git a/common/driver_common/recovery.py b/common/driver_common/recovery.py
new file mode 100644
index 00000000..b982faa9
--- /dev/null
+++ b/common/driver_common/recovery.py
@@ -0,0 +1,25 @@
+import configparser
+from config.workpath import *
+import requests
+
+
+class Recovery:
+ """
+ 自动回收异常时,产生的创建对象
+ 从接口来删除对象
+ """
+ def __init__(self):
+ #初始化,负载远程用户登录
+ self.loginout_parse = configparser.ConfigParser()
+ loginout_parse_dir = os.path.join(workdir, "config", "ui_conf", "loginout.ini")
+ self.loginout_parse.read(loginout_parse_dir, encoding="utf-8")
+ self.username = self.loginout_parse.get("ui_account_1", "username")
+ self.passwd = self.loginout_parse.get("ui_account_1", "passwd")
+
+ def login_by_interface(self):
+ #todo
+ pass
+
+ def del_profile(self):
+ # todo
+ pass
diff --git a/common/driver_common/screenshot.py b/common/driver_common/screenshot.py
new file mode 100644
index 00000000..84c00968
--- /dev/null
+++ b/common/driver_common/screenshot.py
@@ -0,0 +1,39 @@
+from selenium import webdriver
+import shutil
+import time, datetime
+import os
+import functools
+from config.workpath import workdir
+
+#截图装饰器
+def screenshot_on_failure(func):
+ @functools.wraps(func)
+ def warpper(self, *args, **kwargs):
+ try:
+ return func(self, *args, **kwargs)
+ except:
+ driver = self.driver
+ if driver:
+ formatted_time = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d-%H-%M-%S')
+ file = "{}.png".format(formatted_time)
+ file_path = os.path.join(workdir, "results", "screenshot", file)
+ print(f"截图保存于:{file_path}")
+ driver.save_screenshot(file_path)
+ #删除历史7天前数据
+ try:
+ clear_history(file_path)
+ except:
+ print("删除历史数据失败...")
+ raise
+ return warpper
+
+def clear_history(file_path, retain_time=3600*24*1):
+ time_now = time.time()
+ file_work = os.path.dirname(file_path)
+ data_list = [os.path.join(file_work, i) for i in os.listdir(file_work)]
+ for datapath in data_list:
+ if time_now - os.path.getctime(datapath) > retain_time:
+ try:
+ os.remove(datapath)
+ except:
+ shutil.rmtree(datapath)