|
@@ -1,3 +1,7 @@
|
|
|
|
+import shutil
|
|
|
|
+import time
|
|
|
|
+from datetime import timedelta
|
|
|
|
+import datetime
|
|
from selenium import webdriver
|
|
from selenium import webdriver
|
|
from hytest import *
|
|
from hytest import *
|
|
from selenium.webdriver.common.action_chains import ActionChains
|
|
from selenium.webdriver.common.action_chains import ActionChains
|
|
@@ -16,25 +20,33 @@ class BasePage:
|
|
options = webdriver.ChromeOptions()
|
|
options = webdriver.ChromeOptions()
|
|
# options = Options()
|
|
# options = Options()
|
|
# ---无图形界面模式,浏览器采用无头模式
|
|
# ---无图形界面模式,浏览器采用无头模式
|
|
- options.add_argument("--no-sandbox")
|
|
|
|
- options.add_argument("--window-size=1920,1080")
|
|
|
|
- options.add_argument("--start-maximized")
|
|
|
|
- options.add_argument('--disable-dev-shm-usage')
|
|
|
|
- options.add_argument("--headless")
|
|
|
|
|
|
+ # options.add_argument("--no-sandbox")
|
|
|
|
+ # options.add_argument("--window-size=1920,1080")
|
|
|
|
+ # options.add_argument("--start-maximized")
|
|
|
|
+ # options.add_argument('--disable-dev-shm-usage')
|
|
|
|
+ # options.add_argument("--headless")
|
|
# -----------------------------------------
|
|
# -----------------------------------------
|
|
options.add_experimental_option('excludeSwitches', ['enable-logging'])
|
|
options.add_experimental_option('excludeSwitches', ['enable-logging'])
|
|
|
|
+ prefs = {
|
|
|
|
+ 'profile.default_content_setting_values':
|
|
|
|
+ {
|
|
|
|
+ 'notifications': 2
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ options.add_experimental_option('prefs', prefs)#关闭浏览器通知
|
|
# 本地环境
|
|
# 本地环境
|
|
- # wd = webdriver.Chrome(options=options)
|
|
|
|
|
|
+ wd = webdriver.Chrome(options=options)
|
|
|
|
+ #-------------------------------
|
|
# 3.204 linux环境
|
|
# 3.204 linux环境
|
|
- wd = webdriver.Chrome(executable_path='/var/jenkins_home/workspace/jianyu_auto/chromedriver', options=options)
|
|
|
|
|
|
+ # wd = webdriver.Chrome(executable_path='/var/jenkins_home/workspace/jianyu_auto/chromedriver', options=options)
|
|
|
|
+ #-------------------------------
|
|
wd.implicitly_wait(10)
|
|
wd.implicitly_wait(10)
|
|
wd.maximize_window()
|
|
wd.maximize_window()
|
|
# 把driver对象存入全局变量中
|
|
# 把driver对象存入全局变量中
|
|
GSTORE['wd'] = wd
|
|
GSTORE['wd'] = wd
|
|
# 打开网站
|
|
# 打开网站
|
|
wd.get('https://www.jianyu360.com/')
|
|
wd.get('https://www.jianyu360.com/')
|
|
- # wd.get('https://web2-jytest.jydev.jianyu360.com/')
|
|
|
|
-
|
|
|
|
|
|
+ # wd.get('https://jybx2-webtest.jydev.jianyu360.com/')
|
|
# 定位单个元素的关键字
|
|
# 定位单个元素的关键字
|
|
def locator_element(self, loc):
|
|
def locator_element(self, loc):
|
|
# loc为一个元祖
|
|
# loc为一个元祖
|
|
@@ -64,26 +76,31 @@ class BasePage:
|
|
select_loc=self.locator_element(loc)
|
|
select_loc=self.locator_element(loc)
|
|
select=Select(select_loc)
|
|
select=Select(select_loc)
|
|
select.select_by_visible_text(info)
|
|
select.select_by_visible_text(info)
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+ #浏览器上滑300像素
|
|
|
|
+ def slide_upward(self):
|
|
|
|
+ wd = GSTORE['wd']
|
|
|
|
+ js = "var q=document.documentElement.scrollTop=30" # documentElement表示获取根节点元素
|
|
|
|
+ wd.execute_script(js)
|
|
|
|
+ #返回浏览器上一页
|
|
|
|
+ def back_to_previous_page(self):
|
|
|
|
+ wd = GSTORE['wd']
|
|
|
|
+ wd.back()
|
|
|
|
+ time.sleep(3)
|
|
# 设置值的关键字
|
|
# 设置值的关键字
|
|
def send_keys(self, loc, value):
|
|
def send_keys(self, loc, value):
|
|
self.locator_element(loc).send_keys(value)
|
|
self.locator_element(loc).send_keys(value)
|
|
|
|
|
|
# 单击的关键字
|
|
# 单击的关键字
|
|
def click(self, loc):
|
|
def click(self, loc):
|
|
- print(loc)
|
|
|
|
self.locator_element(loc).click()
|
|
self.locator_element(loc).click()
|
|
|
|
|
|
# 列表中单击的关键字
|
|
# 列表中单击的关键字
|
|
def click_list(self, list, loc):
|
|
def click_list(self, list, loc):
|
|
- print(loc)
|
|
|
|
self.locator_list_element(list, loc).click()
|
|
self.locator_list_element(list, loc).click()
|
|
|
|
|
|
# 列表中单击的关键字2
|
|
# 列表中单击的关键字2
|
|
def click_direct_list(self, list):
|
|
def click_direct_list(self, list):
|
|
list.click()
|
|
list.click()
|
|
-
|
|
|
|
# # 列表中单击的关键字3
|
|
# # 列表中单击的关键字3
|
|
# def click_direct_lists(self, lists):
|
|
# def click_direct_lists(self, lists):
|
|
# for list in lists:
|
|
# for list in lists:
|
|
@@ -98,7 +115,6 @@ class BasePage:
|
|
|
|
|
|
# 获取元素的值的关键字
|
|
# 获取元素的值的关键字
|
|
def obtain_key(self, loc, element):
|
|
def obtain_key(self, loc, element):
|
|
- print(loc, element)
|
|
|
|
return self.locator_element(loc).get_attribute(element)
|
|
return self.locator_element(loc).get_attribute(element)
|
|
|
|
|
|
# 获取input输入框的值的关键字
|
|
# 获取input输入框的值的关键字
|
|
@@ -107,12 +123,10 @@ class BasePage:
|
|
|
|
|
|
# 获取元素的文本
|
|
# 获取元素的文本
|
|
def obtain_text(self, loc):
|
|
def obtain_text(self, loc):
|
|
- print(loc)
|
|
|
|
return self.locator_element(loc).text
|
|
return self.locator_element(loc).text
|
|
|
|
|
|
# 获取列表中的单个元素的值
|
|
# 获取列表中的单个元素的值
|
|
def obtain_list_key(self, list, loc):
|
|
def obtain_list_key(self, list, loc):
|
|
- print(list, loc)
|
|
|
|
return self.locator_list_element(list, loc).get_attribute('textContent')
|
|
return self.locator_list_element(list, loc).get_attribute('textContent')
|
|
|
|
|
|
# 获取列表中的单个元素的文本
|
|
# 获取列表中的单个元素的文本
|
|
@@ -179,7 +193,14 @@ class BasePage:
|
|
def close_window(self):
|
|
def close_window(self):
|
|
wd = GSTORE['wd']
|
|
wd = GSTORE['wd']
|
|
wd.close() # 关闭当前
|
|
wd.close() # 关闭当前
|
|
-
|
|
|
|
|
|
+ #切换到当前最新的打开的窗口
|
|
|
|
+ def switch_to_firstwindow (self):
|
|
|
|
+ wd = GSTORE['wd']
|
|
|
|
+ wd.switch_to_window(wd.window_handles[0])
|
|
|
|
+ #刷新当前页面
|
|
|
|
+ def refresh_page(self):
|
|
|
|
+ wd = GSTORE['wd']
|
|
|
|
+ wd.refresh()
|
|
# 鼠标悬停关键字
|
|
# 鼠标悬停关键字
|
|
def mouse_over(self, loc):
|
|
def mouse_over(self, loc):
|
|
wd = GSTORE['wd']
|
|
wd = GSTORE['wd']
|
|
@@ -208,6 +229,22 @@ class BasePage:
|
|
return True
|
|
return True
|
|
else:
|
|
else:
|
|
return False
|
|
return False
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+ #清空目录下文件(不包括文件夹)
|
|
|
|
+ def del_file(self,path):
|
|
|
|
+ ls = os.listdir(path)
|
|
|
|
+ for i in ls:
|
|
|
|
+ c_path = os.path.join(path, i)
|
|
|
|
+ if os.path.isdir(c_path):
|
|
|
|
+ self.del_file(c_path)
|
|
|
|
+ else:
|
|
|
|
+ os.remove(c_path)
|
|
|
|
+ #获取目录下文件数量
|
|
|
|
+ def obtain_file_num(self,filepath):
|
|
|
|
+ file_nums = sum([len(files) for root, dirs, files in os.walk(filepath)])
|
|
|
|
+ return file_nums
|
|
|
|
+ #获取当前窗口url
|
|
|
|
+ def get_current_url(self):
|
|
|
|
+ wd = GSTORE['wd']
|
|
|
|
+ url=wd.current_url
|
|
|
|
+ return url
|
|
bp = BasePage()
|
|
bp = BasePage()
|