284 lines
11 KiB
Python
284 lines
11 KiB
Python
# -*- coding: UTF-8 -*-
|
||
import os
|
||
import time
|
||
import platform
|
||
from seleniumwire import webdriver
|
||
from selenium.webdriver.support.wait import WebDriverWait
|
||
from selenium.webdriver.support import expected_conditions as ec
|
||
from selenium.webdriver.common.keys import Keys
|
||
from selenium.webdriver.support.select import Select
|
||
from base_framework.public_tools.read_config import get_current_config, ReadConfig, InitConfig
|
||
from base_framework.base_config.current_pth import env_choose_path
|
||
from copy import copy, deepcopy
|
||
from selenium.webdriver.common.action_chains import ActionChains
|
||
|
||
cull_path = os.path.dirname(os.path.abspath(__file__))
|
||
plugs = os.path.abspath(os.path.join(cull_path, '../{}'.format('/platform_tools/plugins/headerenv.crx')))
|
||
cc_driver = os.path.abspath(os.path.join(cull_path, '../{}'.format('/public_business/CC/webdriver/chromedriver.exe')))
|
||
DEFAULT_TIMEOUT = 10
|
||
|
||
|
||
class DriverInit(InitConfig):
|
||
def __init__(self, driver_path=None):
|
||
|
||
# super(DriverInit, self).__init__()
|
||
InitConfig.__init__(self, run_user_name=None, current_evn=None)
|
||
self.driver_path = cc_driver if not driver_path else driver_path
|
||
self.config = ReadConfig(env_choose_path)
|
||
self.jira_id = ReadConfig(env_choose_path).get_value('run_jira_id', 'huohua-podenv')
|
||
self.option = webdriver.ChromeOptions()
|
||
self.option.add_argument('--no-sandbox')
|
||
self.option.add_argument('--disable-gpu')
|
||
self.option.add_argument('--disable-dev-shm-usage')
|
||
# self.option.headless = True
|
||
self.option.add_extension(plugs)
|
||
# self._init_driver()
|
||
|
||
def _init_driver(self):
|
||
self.driver = webdriver.Chrome(executable_path=self.driver_path, options=self.option)
|
||
self.driver.scopes = [
|
||
'.*huohua.cn.*', '.*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+.*', '.*allschool.com', '.*sparkedu.com'
|
||
]
|
||
self.driver.implicitly_wait(30)
|
||
self.driver.maximize_window()
|
||
self.actions = ActionChains(self.driver)
|
||
self.wait = WebDriverWait(self.driver, timeout=60)
|
||
if self.jira_id:
|
||
self._set_independent_environment()
|
||
|
||
def _set_independent_environment(self):
|
||
backend = self.jira_id
|
||
frontend = 'feature/%s' % self.jira_id
|
||
new_loc = ('xpath', '/html/body/div/div[3]/button/div/div/i')
|
||
# 独立环境名称
|
||
rule_name_loc = ('id', 'rule-name')
|
||
# 规则类型
|
||
rule_type_loc = ('xpath', '//*[@id="edit-page"]/div[2]/div[1]/div/div[2]/div[2]/div[2]/div[3]/div')
|
||
# 头名称
|
||
rule_headerName_loc = ('id', 'rule-headerName')
|
||
# 头内容
|
||
rule_headerValue_loc = ('id', 'rule-headerValue')
|
||
# 保存
|
||
create_loc = ('xpath', '//*[@id="edit-page"]/div[2]/div[2]/div[2]/div[2]/button/div/div')
|
||
|
||
# js = 'window.open("chrome-extension://eningockdidmgiojffjmkdblpjocbhgh/options/options.html");'
|
||
# self.driver.execute_script(js)
|
||
self.driver.get("chrome-extension://eningockdidmgiojffjmkdblpjocbhgh/options/options.html")
|
||
rule_headerName = 'huohua-podenv'
|
||
rule_headerValue = backend
|
||
self.clickElement(new_loc)
|
||
self.sendKeysElement(rule_name_loc, "back")
|
||
self.clickElement(rule_type_loc)
|
||
self.sendKeysElement(rule_headerName_loc, rule_headerName)
|
||
self.sendKeysElement(rule_headerValue_loc, rule_headerValue)
|
||
self.clickElement(create_loc)
|
||
rule_headerName = 'huohua-feature'
|
||
rule_headerValue = frontend
|
||
self.clickElement(new_loc)
|
||
self.sendKeysElement(rule_name_loc, "front")
|
||
self.clickElement(rule_type_loc)
|
||
self.sendKeysElement(rule_headerName_loc, rule_headerName)
|
||
self.sendKeysElement(rule_headerValue_loc, rule_headerValue)
|
||
self.clickElement(create_loc)
|
||
|
||
def waitEleDisappear(self, locator, timeout=10):
|
||
count = 0
|
||
while count <= timeout:
|
||
try:
|
||
self.wait.until(ec.invisibility_of_element_located(locator=locator))
|
||
break
|
||
except Exception as error:
|
||
time.sleep(1)
|
||
count += 1
|
||
|
||
def elementScroolToView(self, locator):
|
||
ele = self.findElement(locator=locator, timeout=10)
|
||
self.driver.execute_script("arguments[0].focus()", ele)
|
||
self.driver.execute_script("arguments[0].scrollIntoView();", ele)
|
||
|
||
def findElement(self, locator, timeout=10):
|
||
"""
|
||
| 功能说明: | 传入元素定位器,定位到该元素,返回第一个元素|
|
||
| 传入参数: | locator |
|
||
举例说明:
|
||
"""
|
||
element = WebDriverWait(self.driver, timeout).until(ec.presence_of_element_located(locator))
|
||
return element
|
||
|
||
def findElements(self, locator, timeout=10):
|
||
"""
|
||
| 功能说明: | 传入元素定位器,定位到该元素,返回所有元素|
|
||
| 传入参数: | locator |
|
||
举例说明:
|
||
"""
|
||
element = WebDriverWait(self.driver, timeout).until(ec.presence_of_all_elements_located(locator))
|
||
return element
|
||
|
||
def clickElement(self, locator, timeout=10):
|
||
"""
|
||
| 功能说明: | 传入元素定位器,定位到该元素,普通方法点击|
|
||
| 传入参数: | locator |
|
||
举例说明:
|
||
"""
|
||
element = self.findElement(locator, timeout)
|
||
element.click()
|
||
|
||
def clickElement_by_JS(self, locator=None, element=None, timeout=10):
|
||
"""
|
||
| 功能说明: | 传入元素定位器,定位到该元素,以JS的方式点击|
|
||
| 传入参数: | locator | 元素定位器 |
|
||
| element | 页面对象 |
|
||
举例说明:
|
||
"""
|
||
if not element and locator:
|
||
obj = self.findElement(locator, timeout)
|
||
self.driver.execute_script("arguments[0].click();", obj)
|
||
elif element and not locator:
|
||
self.driver.execute_script("arguments[0].click();", element)
|
||
else:
|
||
raise Exception('不支持的传参方式,locator和element必须且只能传一个')
|
||
|
||
def sendKeysElement(self, locator, text, timeout=10):
|
||
"""
|
||
| 功能说明: | 传入元素定位器,定位到该元素,清空输入框,写入text|
|
||
| 传入参数: | locator,text |
|
||
举例说明:
|
||
"""
|
||
element = self.findElement(locator, timeout)
|
||
element.send_keys(Keys.CONTROL, 'a')
|
||
element.send_keys(text)
|
||
|
||
def getElementText(self, locator, timeout=10):
|
||
"""
|
||
| 功能说明: | 传入元素定位器,定位到该元素,返回该元素的文本值|
|
||
| 传入参数: | locator |
|
||
举例说明:
|
||
"""
|
||
element = self.findElement(locator, timeout)
|
||
return element.text
|
||
|
||
def clickSingleBox(self, locator, timeout=10):
|
||
"""
|
||
| 功能说明: | 传入单选框元素定位器,定位到该元素,依次点击单选框|
|
||
| 传入参数: | locator |
|
||
举例说明:
|
||
"""
|
||
elements = self.findElements(locator, timeout)
|
||
for element in elements:
|
||
self.driver.execute_script("arguments[0].click();", element)
|
||
time.sleep(1.5)
|
||
|
||
def selectDropDownBox(self, locator, index=0, timeout=10):
|
||
"""
|
||
| 功能说明: | 传入下拉框定位器,定位到该元素,选择下拉框的第index个选项|
|
||
| 传入参数: | locator,index |
|
||
举例说明:
|
||
"""
|
||
select = Select(self.findElement(locator, timeout))
|
||
select.select_by_index(index)
|
||
|
||
# 选择多选框-->全选
|
||
def clickCheckbox(self, locator, timeout=10):
|
||
"""
|
||
| 功能说明: | 传入多选框元素定位器,定位到该元素,全选|
|
||
| 传入参数: | locator |
|
||
举例说明:
|
||
"""
|
||
checkbox = self.findElements(locator, timeout)
|
||
for i in checkbox:
|
||
if not i.is_selected():
|
||
i.click()
|
||
|
||
def uploadFile(self, locator, path, timeout=10):
|
||
"""
|
||
| 功能说明: | 传入元素定位器,定位到该元素,传入文件路径,只适用于input标签|
|
||
| 传入参数: | locator |
|
||
举例说明:
|
||
"""
|
||
element = self.findElement(locator, timeout)
|
||
element.send_keys(path)
|
||
|
||
def roll_windows(self, x=0, y=0):
|
||
"""
|
||
滑动窗口
|
||
:param x: x轴距离
|
||
:param y: y轴距离
|
||
:return: 无
|
||
"""
|
||
self.driver.execute_script("window.scrollBy({},{})".format(x, y))
|
||
|
||
def back_window(self):
|
||
"""
|
||
返回上一页(浏览器工具栏向左箭头)
|
||
:return: 无
|
||
"""
|
||
self.driver.back()
|
||
|
||
def forward_window(self):
|
||
"""
|
||
前进一页(浏览器工具栏向右箭头)
|
||
:return: 无
|
||
"""
|
||
self.driver.forward()
|
||
|
||
def close_current_window(self):
|
||
"""
|
||
关闭当前窗口
|
||
:return: 无
|
||
"""
|
||
self.driver.close()
|
||
|
||
def get_window_title(self):
|
||
"""
|
||
关闭当前窗口
|
||
:return: 无
|
||
"""
|
||
current_title = self.driver.title
|
||
return current_title
|
||
|
||
def get_element_attribute_value(self, locator, attr: str, timeout=10):
|
||
"""
|
||
关闭当前窗口
|
||
:param locator 定位器
|
||
:param attr 属性名
|
||
:param timeout 元素查找超时时间
|
||
:return: 无
|
||
"""
|
||
obj = self.findElement(locator, timeout=timeout)
|
||
attr_value = obj.get_attribute(attr)
|
||
return attr_value
|
||
|
||
def modify_tag_attribution(self, locator, attr, value, timeout=10):
|
||
"""
|
||
修改页签属性
|
||
:param locator: 需要修改的元素定位器
|
||
:param attr: 修改属性名
|
||
:param value: 修改后的值
|
||
:param timeout: 超时时间
|
||
:return:
|
||
"""
|
||
obj = self.findElement(locator, timeout)
|
||
self.driver.execute_script("arguments[0].{attr}={value};".format(attr=attr, value=value), obj)
|
||
|
||
def input_to_readonly_tag(self, locator, text, timeout=10):
|
||
"""
|
||
带只读属性的标签输入
|
||
:param locator: 元素定位器
|
||
:param text: 输入内容
|
||
:param timeout: 超时时间
|
||
:return:无
|
||
"""
|
||
obj = self.findElement(locator, timeout)
|
||
self.driver.execute_script("arguments[0].removeAttribute('readonly');", obj)
|
||
self.sendKeysElement(locator=locator, text=text)
|
||
|
||
def takeScreenshot(self, savePath, pictureName):
|
||
"""
|
||
| 功能说明: | 截取浏览器当前页面|
|
||
| 传入参数: | savePath:保存地址 |
|
||
| | pictureName:图片保存名称
|
||
举例说明:
|
||
"""
|
||
picturePath = os.path.join(savePath, pictureName + '.png')
|
||
self.driver.get_screenshot_as_file(picturePath)
|