#coding=utf8
'''
该模式功能是实现自动运行sql命令,命令执行成功后
保存成功截图并下载结果文件;运行出错,则保存错误截图;
开始运行保存,设置命令截图。
该模块初始化包含如下信息:
self.url:用来保存要访问的网站链接
self.driver:创建一个Chrome对象实例,并启动Chrome浏览器
self.driver.maximize_window():窗口最大化
self.driver.get(self.url):打开网站链接
self.driver.implicitly_wait(10):设置隐性等待时间10秒
'''
import time
import os
#导入读取配置文件库的Config
from readConfig import Config
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
#设置默认编码格式为utf8
import sys
reload(sys)
sys.setdefaultencoding("utf8")
class RunXQL(object):
def __init__(self,configObj=Config()):
self.config=configObj
self.url="http://xql.9nali.com/"
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.get(self.url)
self.driver.implicitly_wait(10)
self.run()
def openCmd(self):
try:
#设置命令文件的路径,使用RunAll.py脚本时,是.//CmdFile//CMD
#如果直接运行该脚本要更改为:..//CmdFile//CMD
cmd=self.config.getXQLCMD()
#把文件转换成string类型并解码
cmds=str(cmd).decode("utf8")
#根据换行符对命令文件进行拆分
#如果不进行该项处理,在命令行结尾会莫名添加一个括号)
cmd=cmds.split("\n")
#返回命令行列表
return cmd
except Exception,e:
print e
def SetCmdImage(self):
try:
#获取当前时间并转换为字符串
now=time.strftime("%Y%m%d%H%M%S")
#设置图片格式与路径
#以下涉及路径的于此处理方法一样
try:
if os.path.exists(self.config.SetCmdImage()):
pass
else:
os.mkdir(self.config.SetCmdImage())
fileName=self.config.SetCmdImage()+"\\"+now+"xpl.png"
except Exception,e:
print "SetCmdImage Error:",e
#保存屏幕截图到相应路径
self.driver.get_screenshot_as_file(fileName)
except Exception,e:
print e
def RunErrorImage(self):
try:
now=time.strftime("%Y%m%d%H%M%S")
try:
if os.path.exists(self.config.RunErrorImage()):
pass
else:
os.mkdir(self.config.RunErrorImage())
fileName=self.config.RunErrorImage()+"\\"+now+"error.png"
except Exception,e:
print "RunErrorImage Error:",e
self.driver.get_screenshot_as_file(fileName)
except Exception,e:
print e
def SuccessImage(self):
try:
now=time.strftime("%Y%m%d%H%M%S")
try:
if os.path.exists(self.config.ExecResutlImage()):
pass
else:
os.mkdir(self.config.ExecResutlImage())
fileName=self.config.ExecResutlImage()+"\\"+now+"success.png"
except Exception,e:
print "SetCmdImage Error:",e
self.driver.get_screenshot_as_file(fileName)
except Exception,e:
print e
def inputCMD(self,cmd):
try:
#获取输入命令行元素对象
cmdArea=self.driver.find_element_by_class_name("ace_text-input")
#如果元素存在
if cmdArea:
#把命令输入到文本区域中
cmdArea.send_keys(cmd)
#获取屏幕截图
self.SetCmdImage()
else:
print "The element %s not exists!" %("ace_text-input")
except Exception,e:
print e
def runCmd(self):
try:
#获取执行命令的元素对象
runEle=self.driver.find_element_by_xpath("//*[@id=\"new-tab-cont\"]/div/div[1]/ul[1]/li[1]/a")
#如果元素存在
if runEle:
#点击该元素运行命令
runEle.click()
else:
print "The element %s not exists!" %("exec")
except Exception,e:
print e
def clickDown(self):
try:
#获取下载元素对象
download=self.driver.find_element_by_xpath("//*[@id=\"new-tab-cont\"]/div/div[3]/ul/li[1]/a[1]")
#如果元素存在
if download:
#点击该元素执行下载操作
download.click()
else:
print "The element %s not exists!" %("fa fa-download j-dload")
except Exception,e:
print e
def GetResutl(self):
try:
#获取命令出错或执行异常,显示异常信息的元素对象
#超过60秒如果找不到该元素,失败
text_danger=WebDriverWait(self.driver, 60).until(lambda driver: driver.find_element_by_xpath("//*[@id=\"new-tab-cont\"]/div/div[3]/div/div[1]/pre"))
#如果元素存在
if text_danger:
#获取元素的文本信息
errInfro=text_danger.text
#获取屏幕截图
self.RunErrorImage()
print errInfro
except Exception,e:
pass
try:
#获取成功时显示的文本信息,超过300秒元素不存在则失败
text_success=WebDriverWait(self.driver, 300).until(lambda driver: driver.find_element_by_class_name("text-success"))
#如果元素存在
if text_success:
#获取屏幕截图
self.SuccessImage()
#点击下载按钮下载运行结果文件
time.sleep(10)
self.clickDown()
print "sql run successs......"
except Exception,e:
print e
def run(self):
try:
cmd=self.openCmd()
self.inputCMD(cmd)
self.runCmd()
self.GetResutl()
time.sleep(10)
except Exception,e:
print e
finally:
#最后关闭浏览器
self.driver.close()
def test():
RunXQL()
if __name__=="__main__":
test()
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
XQLSendMail_V2.2.rar (22个子文件)
XQLSendMail_V2.1
.project 387B
RunAll.py 1KB
RunErrorImage
20170308110636error.png 57KB
readme 512B
Report
20170308112512.csv 233B
.settings
org.eclipse.core.resources.prefs 225B
ExecResutlImage
20170308112259success.png 62KB
20170308112450success.png 61KB
.pydevproject 431B
config.xml 451B
Lib
mangerResultFile.pyc 3KB
AutoSendMail.pyc 4KB
readConfig.py 4KB
__init__.pyc 412B
readConfig.pyc 4KB
__init__.py 236B
AutoSendMail.py 3KB
XQL.pyc 6KB
XQL.py 7KB
mangerResultFile.py 5KB
SetCmdImage
20170308112156xpl.png 49KB
20170308112346xpl.png 49KB
共 22 条
- 1
资源评论
- rebecca_g2017-11-09感谢分享!
- 自沉于海2018-01-17感谢分享!正需要
WEL测试
- 粉丝: 3701
- 资源: 47
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功