2019-06-17阶段性总结(Python3)

这篇是用来总结这段时间所学的所用的关于python3小tips,比较细微。主要是为了防止以后会遗忘掉这些而去重复搜索解决方法。
这次主要对于自动化提供一个之前没有想到的新思路。

原:
  原本对于自动化测试用例管理以及任务执行沿用了老东家的思路,即在前台输入脚本代码后,生成独立的用例脚本存放在测试机(电脑)上,后期如果要调用或者重跑的话直接利用dos命令执行。好处是复用率高,重跑不用重新生成文件,速度比较快。但存在着致命缺点,即无法传递socket实例(可能是还没找到),在执行任务时无法将每一步的执行日志输出发送至服务器。故采用新方法。
新:
  在前台输入脚本代码后,控制程序读取代码片段,并经过多次分割获得每一步操作经过自己封装过后的函数以及参数,然后执行。这样做socket实例没有跨越文件调用,不存在传递不了的问题。中间有碰到过如何获取日志信息的问题,考虑后也是直接将日志信息做成函数返回,每一步返回发送至服务器。代码如下(可能仍存在问题,当前版本当前用例可用),另变量名起的比较混乱,因为不是一次性写完的,这个后期再维护:

#解析核心
#encoding: utf-8
'''
@project = 
@file = 
@author = 
@create_time = 2019/6/12 14:38
'''

from configure.dbop import *
from appium import webdriver
from time import sleep
from configure.dingtalk import *
from configure.login import *
import random
from pathlib import Path
import time
from PIL import Image
import operator
from functools import reduce
import math

global dc
dc = {}



#截屏操作预处理,创建路径和准备截图文件名
i = 1

def getnum():
    a = random.randint(1,99999999)
    return a

#设备参数
def machine(name):
    if name == 1:
        dc['deviceName'] = 'ONEPLUS-A5010-41db022b'
        dc['platformName'] = 'Android'
        dc['platformVersion'] = '9'
    elif name == 2:
        dc['deviceName'] = 'Letv-X500-LJJJMBIJ9PR4DYAE'
        dc['platformName'] = 'Android'
        dc['platformVersion'] = '6.0'
    elif name == 'yg6T':
        dc['deviceName'] = 'ONEPLUS-6T-b5157c81'
        dc['platformName'] = 'Android'
        dc['platformVersion'] = '9'

#APP参数
def testapp(name):
    if name == 1:
        dc['appPackage'] = 'com.yr.mmpic'
        dc['appActivity'] = '.ui.activity.SplashActivity'
    elif name == 2:
        dc['appPackage'] = 'com.ym.ym'
        dc['appActivity'] = '.ui.activity.SplashActivity'
    elif name == 'Mixiu':
        dc['appPackage'] = 'com.ym.mixiu'
        dc['appActivity'] = '.ui.activity.SplashActivity'

#是否新安装状态,true为否
def reset(boolean):
    boolean = repr(boolean)
    if boolean == 1:
        dc['noReset'] = 'true'
    else:
        dc['noReset'] = 'false'

#启动控制驱动
def startdriver():
    global driver
    try:
        driver = webdriver.Remote('http://localhost:4723/wd/hub', dc)
        message = "启动中"
        return message
    except Exception as e:
        maincontrol('quit')
        return e


#将绝对位置坐标转换为相对位置坐标并点击
def findxy():
    x = driver.get_window_size()['width']
    y = driver.get_window_size()['height']
    sleep(5)
    #width = float(width)
    #height = float(height)
    driver.tap([(540, 1080)], 200)

#转换参数
def repalcenew(data):
    data = str(data)
    data = data.replace(")", "")
    return data

#检查是否为登录状态,app参数("尤蜜""蜜秀""MM131")
def checklogin(app):
    if app == "蜜秀":
        return loginMX()
    elif app == "尤蜜":
        print("还没做")
    elif app == "MM131":
        print("还没做")
    else:
        msg = "检查登录参数有问题"
        return msg

#向下滑跳转至随机某一页
def swipeDown(cycle=None):
    l = driver.get_window_size()
    x = l['width']
    y1 = l["height"] * 0.5
    y2 = l["height"] * 0.75
    if cycle == None:
        for i in range(random.randint(1,50)):
            driver.swipe(x,y1,x,y2,500)
    else:
        for i in range(cycle):
            driver.swipe(x,y1,x,y2,500)

def dealcodecount(code):
    for i in range(len(code)):
        codes = str(code[i - 1][4]).split('\r\n')
        print(len(codes))
        for i in range(len(codes)):
            try:
                a = maincontrol(codes[i])
                print(a)
            except Exception:
                message = "预处理还有问题"
                return message

#分割处理图片
def cutpic(x,y,w,h,old,new):
    img = Image.open(old)
    img_size = img.size
    height = img_size[1]
    width = img_size[0]
    x1 = int(float(x) * int(width))
    y1 = int(float(y) * int(height))
    w1 = int(float(w) * int(width))
    h1 = int(float(h) * int(height))
    region = img.crop((x1,y1,x1+w1,y1+h1))
    region.save(new)


#解析核心
def maincontrol(code):
    code = str(code)
    code = code.split("(")
    func = str(code[0])
    #print(func)
    #print(code, code[0], code[1])
    if func[0] == '#':
        print()
        message="注释:%s" %func
        return message
        print(message)
    elif func == 'setdelay':
        param = repalcenew(code[1].replace(")", "").replace("'", "").replace("\"", ""))
        #changedatainDB("t_ui_script", "delay=%s" %param, "group_id=%s" %)
    elif func == 'sleep':
        try:
            sleep(int(code[1].replace(")","")))
            message = "等待%s秒" %(code[1].replace(")",""))
            return message
        except Exception as e:
            return e

    elif func == 'machine':
        #print(code[1].replace(")",""))
        try:
            param= repalcenew(code[1].replace(")","").replace("'","").replace("\"",""))
            machine(param)
            message="设备参数载入成功"
            return message
        except Exception:
            message = "设备参数载入有问题,请确认"
            return message
    elif func == 'testapp':
        try:
            param = repalcenew(code[1].replace(")","").replace("'","").replace("\"",""))
            testapp(param)
            message="APP参数载入成功"
            return message
        except Exception:
            message = "APP参数载入有问题,请确认"
            return message

    elif func == 'reset':
        try:
            param = repalcenew(code[1].replace(")","").replace("'","").replace("\"",""))
            reset(param)
            message="是否设定设备不进行初始化:%s" %param
            return message
        except Exception:
            message = "设定重启有问题,请确认"
            return message

    elif func == 'startdriver':
        try:
            startdriver()
            message="启动中..."
            return message
        except Exception as e:
            #message = "启动驱动有问题:"
            return e

    elif func == 'findxy':
        #paramcount = repalcenew(code[1].replace(")","").replace("'","").replace("\"",""))
        #param = paramcount.split(",")
        #print(param[0],param[1])
        #findxy(param[0], param[1])
        findxy()

    elif func == 'clickxpath':
        #获取元素SQL结果的第一条数据第五个字段,即元素属性
        elementid = finddatainDB("t_ui_element", "eid=%s" %code[1].replace(")","").replace("\"","").replace("'",""))[0][4]
        try:
            driver.implicitly_wait(10)
            driver.find_element_by_xpath(elementid).click()
            message="点击元素(%s)成功" %elementid
            return message
        except Exception:
            print('there is some problems in the system or the element you choose')
            message="点击元素(%s)失败" %elementid
            return message

    elif func == 'clickid':
        elementid = finddatainDB("t_ui_element", "eid=%s" % code[1].replace(")","").replace("\"","").replace("'",""))[0][4]
        try:
            driver.implicitly_wait(10)
            driver.find_element_by_id(elementid).click()
            message="点击元素(%s)成功" %elementid
            return message
        except Exception:
            print('there is some problems in the system or the element you choose')
            message="点击元素(%s)失败" %elementid
            return message

    elif func == 'clickcont':
        elementid = finddatainDB("t_ui_element", "eid=%s" % code[1].replace(")","").replace("\"","").replace("'",""))[0][4]
        try:
            driver.implicitly_wait(10)
            driver.find_element_by_accessibility_id(elementid)
            driver.find_element_by_id(elementid).click()
            message="点击元素(%s)成功" %elementid
            return message
        except Exception as e:
            print(e)
            message="点击元素(%s)失败" %elementid
            return message

    elif func == 'checkxpath':
        elementcount = finddatainDB("t_ui_element", "eid=%s" % code[1].replace(")","").replace("\"","").replace("'",""))[0]
        elementid = elementcount[4]
        try:
            driver.implicitly_wait(10)
            a = driver.find_element_by_xpath(elementid).is_enabled()
            if a:
                print('找到元素')
                message="找到元素(%s)成功,无需重跑" %elementid
                return message
            else:
                print('未找到元素')
                sendMessageToDD('未找到元素')
                message="没有找到(%s)元素" %elementid
                return message
        except Exception as e:
            print("%s" %e)
            sendMessageToDD("%s" %e)
            message=e
            return message

    elif func == 'checkid':
        elementcount = finddatainDB("t_ui_element", "eid=%s" % code[1].replace(")","").replace("\"","").replace("'",""))[0]
        elementid = elementcount[4]
        try:
            driver.implicitly_wait(10)
            a = driver.find_element_by_id(elementid).is_enabled()
            if a:
                print('找到元素')
                message="找到元素(%s)成功,无需重跑" %elementid
                return message
            else:
                print('未找到元素')
                sendMessageToDD('未找到元素')
                message="没有找到(%s)元素" %elementid
                return message
                #return elementcount[3]
        except Exception as e:
            print("%s" %e)
            sendMessageToDD("%s" %e)
            message=e
            return message

    elif func == "screenshot":
        try:
            picname  = repalcenew(code[1].replace(")","").replace("'","").replace("\"",""))
            scrpath = "C:\\Users\\admin\\Desktop\\screenShot"
            capturename = "\\" + picname + ".png"
            wholepath = scrpath + capturename
            sleep(2)
            driver.get_screenshot_as_file(wholepath)
            message = "截屏成功"
            return message
        except Exception as e:
            return e

    elif func == "cutpic":
        try:
            paramcount = repalcenew(code[1].replace(")","").replace("'","").replace("\"",""))
            count = (paramcount.split(" ")[0]).split(",")
            scrpath = "C:\\Users\\admin\\Desktop\\screenShot"
            old = scrpath+"\\"+ count[4] +".png"
            new = scrpath+"\\"+ count[5] +".png"
            cutpic(float(count[0]),float(count[1]),float(count[2]),float(count[3]),old,new)
            message = "图片处理成功"
            return message
        except Exception as e:
            return e

    elif func == "longPress":
        sleep(3)
        try:
            param1 = code[1].replace(")", "").split(",")
            width = float(param1[0])
            height = float(param1[1])
            time = param1[2]
            realwidth = driver.get_window_size()["width"]
            realheight = driver.get_window_size()["height"]
            x1 = width*realwidth
            y1 = height*realheight
            print(width*realwidth)
            driver.tap([(x1,y1)], time)
            sleep(5)
            message = "根据坐标(%s,%s)点击成功" %(x1,y1)
            return message
        except Exception as e:
            message = "根据坐标(%s,%s)点击失败" %(x1,y1)
            return message

    elif func == "getText":
        try:
            code[1] = code[1].replace(")", "").split(",")
            type = code[1][0]
            id = code[1][1].replace("\"", "").replace("'", "")
            id = repr(id)
            elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" % (type, id))
            element = elementacc[0][4]
            driver.implicitly_wait(10)
            message = driver.find_element_by_xpath(element).text
            #message = "点击元素(%s)成功" % elementacc[0][3]
            return message
        except Exception:
            print('there are some problems in the system or the element you choose')
            message = "获取文本(%s)失败" % elementacc[0][3]
            return message

    elif func == "checkText":
        try:
            code[1] = code[1].replace(")", "").split(",")
            type = code[1][0]
            id = code[1][1].replace("\"", "").replace("'", "")
            id = repr(id)
            text = code[1][2].replace("\"", "").replace("'","").replace("私照","")
            elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" % (type, id))
            element = elementacc[0][4]
            driver.implicitly_wait(10)
            realtext = driver.find_element_by_xpath(element).text
            realtext = str(realtext).replace("私照","").replace("\"","")
            if text == realtext:
                message = "比较成功,文本控件%s存在,无需重跑" %text
            else:
                message = "比较失败,文本控件%s不存在" %text
            # message = "点击元素(%s)成功" % elementacc[0][3]
            return message,text,realtext
        except Exception as e:
            #print('there are some problems in the system or the element you choose')
            message = "比较文本(%s)失败" % elementacc[0][3]
            return message,e

    elif func == 'clickCont':
        try:
            code[1] = code[1].replace(")", "").split(",")
            type = code[1][0]
            id = code[1][1].replace("\"", "").replace("'","")
            id = repr(id)
            elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" %(type, id))
            element = elementacc[0][4]
            driver.implicitly_wait(10)
            driver.find_element_by_accessibility_id(element).click()
            message ="点击元素(%s)成功" %elementacc[0][3]
            return message
        except Exception as e:
            print(e)
            message = "点击元素(%s)失败" % elementacc[0][3]
            return message

    elif func == 'clickXpath':
        try:
            code[1] = code[1].replace(")", "").split(",")
            type = code[1][0]
            id = code[1][1].replace("\"", "").replace("'","")
            id = repr(id)
            elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" %(type, id))
            element = elementacc[0][4]
            driver.implicitly_wait(10)
            driver.find_element_by_xpath(element).click()
            message = "点击元素(%s)成功" %elementacc[0][3]
            return message
        except Exception:
            print('there are some problems in the system or the element you choose')
            message = "点击元素xpath(%s)失败" %elementacc[0][3]
            return message

    elif func == 'clickId':
        try:
            code[1] = code[1].replace(")", "").split(",")
            type = code[1][0]
            id = code[1][1].replace("\"", "").replace("'","")
            id = repr(id)
            elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" %(type, id))
            element = elementacc[0][4]
            driver.implicitly_wait(10)
            driver.find_element_by_id(element).click()
            message = "点击元素(%s)成功" %elementacc[0][3]
            return message
        except Exception:
            print('there are some problems in the system or the element you choose')
            message = "点击元素id(%s)失败" %elementacc[0][3]
            return message
        driver.long_press_keycode()

    # elif func == "checkText":
    #     code[1] = code[1].replace(")","").split(",")
    #     try:
    #         a = driver.find_element_by_link_text(str(u"热门".encode('utf-8'))).is_enabled()
    #         if a:
    #             print("找到元素")
    #             message = "找到元素(%s)成功,无需重跑" %code[1]
    #             return message
    #         else:
    #             print("未找到元素")
    #             sendMessageToDD("未找到元素(%s)" %code[1])
    #             message = "没有找到(%s)元素" %code[1]
    #             return message
    #     except Exception as e:
    #         return e

    elif func == 'checkXpath':
        code[1] = code[1].replace(")", "").split(",")
        type = code[1][0]
        id = code[1][1].replace("\"", "").replace("'", "")
        id = repr(id)
        elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" % (type, id))
        element = elementacc[0][4]
        driver.implicitly_wait(10)
        try:
            a = driver.find_element_by_xpath(element).is_enabled()
            if a:
                print("找到元素")
                message = "找到元素(%s)成功,无需重跑" %elementacc[0][3]
                return message
            else:
                print("未找到元素")
                sendMessageToDD("未找到元素(%s)" %elementacc[0][3])
                message = "没有找到(%s)元素" %elementacc[0][3]
        except Exception as e:
            print("%s" %e)
            sendMessageToDD("%s" %e)
            message = "%s" %e
            return message

    elif func == 'checkId':
        code[1] = code[1].replace(")", "").split(",")
        type = code[1][0]
        id = code[1][1].replace("\"", "").replace("'", "")
        id = repr(id)
        elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" % (type, id))
        element = elementacc[0][4]
        driver.implicitly_wait(10)
        try:
            a = driver.find_element_by_id(element).is_enabled()
            if a:
                print("找到元素")
                message = "找到元素(%s)成功,无需重跑" %elementacc[0][3]
                return message
            else:
                print("未找到元素")
                sendMessageToDD("未找到元素(%s)" %elementacc[0][3])
                message = "没有找到(%s)元素" %elementacc[0][3]
        except Exception as e:
            print("%s" %e)
            sendMessageToDD("%s" %e)
            message = e
            return message

    elif func == "checkscreen":
        try:
            pic = code[1].replace(")","").split(",")
            pic1 = pic[0].replace("\"","").replace("'","")
            pic2 = pic[1].replace("\"","").replace("'","")
            picName = pic[2].replace("\"","").replace("'","")
            type = pic[3].replace("\"","").replace("'","")
            path1 = "C:\\Users\\admin\\Desktop\\screenShot\\%s.png" %pic1
            path2 = "C:\\Users\\admin\\Desktop\\screenShot\\%s.png" %pic2
            imag1 = Image.open(path1)
            imag2 = Image.open(path2)
            h1 = imag1.histogram()
            h2 = imag2.histogram()
            result = math.sqrt(reduce(operator.add, list(map(lambda a,b: (a-b)**2, h1, h2)))/len(h1))

            if type == "True":
                if result <= 0.1:
                    message = "%s图片对比结果:相似。用例通过,无需重跑。差异度为:%s" % (picName, result)
                    return message
                else:
                    message = "%s图片对比结果:不同。用例不通过。差异度为:%s" % (picName, result)
                    return message
            elif type == "False":
                if result <= 0.1:
                    message = "%s图片对比结果:相似。用例不通过。差异度为:%s" % (picName, result)
                    return message
                else:
                    message = "%s图片对比结果:不同。用例通过,无需重跑。差异度为:%s" % (picName, result)
                    return message
            else:
                message = "结果不明确,请确认(结果应为True或者False)"
        except Exception as e:
            return e

    elif func == "chooseSmallpicIfnot":
        try:
            now = "C:\\Users\\admin\\Desktop\\screenShot\\smallpic.png"
            driver.get_screenshot_as_file(now)
            cutpic(0.848, 0.249, 0.152, 0.036, now, now)
            imageNow = Image.open(now)
            imageClose = Image.open("C:\\Users\\admin\\Desktop\\screenShot\\小图模式状态图.png")
            h3 = imageNow.histogram()
            h4 = imageClose.histogram()
            result = math.sqrt(reduce(operator.add, list(map(lambda  a,b: (a-b)**2, h3, h4)))/len(h3))
            if result <= 5:
                driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.RelativeLayout/android.widget.ScrollView/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.RelativeLayout[2]/android.widget.CompoundButton").click()
                message = "原本小图模式状态为关闭,现已打开"
                return message,result
            else:
                message = "原本小图模式状态为开启"
                return message,result
        except Exception as e:
            return e

    elif func == "chooseSmallpicIfopen":
        try:
            now = "C:\\Users\\admin\\Desktop\\screenShot\\smallpic.png"
            driver.get_screenshot_as_file(now)
            cutpic(0.848, 0.249, 0.152, 0.036, now, now)
            imageNow = Image.open(now)
            imageClose = Image.open("C:\\Users\\admin\\Desktop\\screenShot\\小图模式状态图.png")
            h3 = imageNow.histogram()
            h4 = imageClose.histogram()
            result = math.sqrt(reduce(operator.add, list(map(lambda  a,b: (a-b)**2, h3, h4)))/len(h3))
            if result >= 5:
                driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.RelativeLayout/android.widget.ScrollView/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.RelativeLayout[2]/android.widget.CompoundButton").click()
                message = "原本小图模式状态为打开,现已关闭"
                return message,result
            else:
                message = "原本小图模式状态为关闭"
                return message,result
        except Exception as e:
            return e



    elif func == 'checkCont':
        code[1] = code[1].replace(")", "").split(",")
        type = code[1][0]
        id = code[1][1].replace("\"", "").replace("'", "")
        id = repr(id)
        elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" % (type, id))
        element = elementacc[0][4]
        driver.implicitly_wait(10)
        try:
            a = driver.find_element_by_accessibility_id(element).is_enabled()
            if a:
                print("找到元素")
                message = "找到元素(%s)成功,无需重跑" %elementacc[0][3]
                return message
            else:
                print("未找到元素")
                sendMessageToDD("未找到元素(%s)" %elementacc[0][3])
                message = "没有找到(%s)元素" %elementacc[0][3]
                return message
        except Exception as e:
            print("%s" %e)
            sendMessageToDD("%s" %e)
            message = e
            return message

    elif func == "inputXpath":
        code[1] = code[1].replace(")", "").split(",")
        type = code[1][0]
        id = code[1][1].replace("\"", "").replace("'", "")
        id = repr(id)
        elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" % (type, id))
        element = elementacc[0][4]
        words = code[1][2].replace("\"","").replace("'","")
        driver.implicitly_wait(10)
        try:
            driver.find_element_by_xpath(element).send_keys(u"%s" %words)
            message = "输入%s成功" %words
            return message
        except Exception:
            message = "输入%s失败" %words
            return message

    elif func == "inputXpathRandom":
        code[1] = code[1].replace(")", "").split(",")
        type = code[1][0]
        id = code[1][1].replace("\"", "").replace("'", "")
        id = repr(id)
        elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" % (type, id))
        element = elementacc[0][4]
        driver.implicitly_wait(10)
        try:
            driver.find_element_by_xpath(element).clear()
            driver.find_element_by_xpath(element).send_keys(getnum())
            message = "输入随机数成功"
            return message
        except Exception:
            message = "输入随机数失败"
            return message
    elif func == "paramUse":
        try:
            param = repalcenew(code[1].replace(")", "").replace("'", "").replace("\"", ""))
            paramsCount = finddatainDB("t_ui_params", "group_id=%s" %param)
            for i in range(len(paramsCount)):
                dc["%s" %paramsCount[i][1]] = '%s' %paramsCount[i][2]
            message = "参数载入成功"
            return message
        except Exception as e:
            message = "参数载入有问题"
            return e,message
    elif func == "预处理":
        try:
            script_name = repalcenew(code[1].replace(")", "").replace("'","").replace("\"",""))
            sid = finddatainDB("t_ui_script", "script_name=%s" %repr(script_name))[0][0]
            code = finddatainDB("t_ui_script_code", "script_id=%d" %sid)#[0][4]
            dealcodecount(code)
            message = "预处理流程走完,结果请根据上方几条分析。"
            return message
        except Exception:
            message = "预处理还有问题"
            return message
    elif func == "swipe":
        paramsTwo = repalcenew(code[1].replace(")","").replace("'","").replace("\"",""))
        param = paramsTwo.split(",")
        paNum = len(param)
        if paNum == 1:
            direction = param
            try:
                if direction == "down":
                    sleep(2)
                    l = driver.get_window_size()
                    x = l['width'] * 0.5
                    y1 = l["height"] * 0.75
                    y2 = l["height"] * 0.5
                    for i in range(random.randint(1, 50)):
                        sleep(1)
                        driver.swipe(x, y1, x, y2, 300)
                    message = "向下滑动成功"
                    return message
                elif direction == "up":
                    sleep(2)
                    l = driver.get_window_size()
                    x = l["width"] * 0.5
                    y1 = l["height"] * 0.75
                    y2 = l["height"] * 0.5
                    for i in range(random.randint(1, 50)):
                        sleep(1)
                        driver.swipe(x, y2, x, y1, 300)
                    message = "向上滑动成功"
                    return message
                elif direction == "left":
                    sleep(2)
                    l = driver.get_window_size()
                    x1 = l["width"] * 0.5
                    x2 = l["width"] * 0.75
                    y = l["height"] * 0.5
                    for i in range(random.randint(1, 50)):
                        sleep(1)
                        driver.swipe(x1, y, x2, y, 300)
                    message = "向左滑动成功"
                    return message
                elif direction == "right":
                    sleep(2)
                    l = driver.get_window_size()
                    x1 = l["width"] * 0.75
                    x2 = l["width"] * 0.5
                    y = l["height"] * 0.5
                    for i in range(random.randint(1, 50)):
                        sleep(1)
                        driver.swipe(x1, y, x2, y, 300)
                    message = "向右滑动成功"
                    return message
            except Exception as e:
                return e
        else:
            direction = param[0]
            time = int(param[1])
            try:
                if direction == "down":
                    sleep(2)
                    l = driver.get_window_size()
                    x = l['width'] * 0.5
                    y1 = l["height"] * 0.75
                    y2 = l["height"] * 0.5
                    for i in range(time):
                        sleep(1)
                        driver.swipe(x, y1, x, y2, 300)
                    message = "向下滑动成功"
                    return message
                elif direction == "up":
                    sleep(2)
                    l = driver.get_window_size()
                    x = l["width"] * 0.5
                    y1 = l["height"] * 0.75
                    y2 = l["height"] * 0.5
                    for i in range(time):
                        sleep(1)
                        driver.swipe(x, y2, x, y1, 300)
                    message = "向上滑动成功"
                    return message
                elif direction == "left":
                    sleep(2)
                    l = driver.get_window_size()
                    x1 = l["width"] * 0.5
                    x2 = l["width"] * 0.75
                    y = l["height"] * 0.5
                    for i in range(time):
                        sleep(1)
                        driver.swipe(x1, y, x2, y, 300)
                    message = "向左滑动成功"
                    return message
                elif direction == "right":
                    sleep(2)
                    l = driver.get_window_size()
                    x1 = l["width"] * 0.75
                    x2 = l["width"] * 0.5
                    y = l["height"] * 0.5
                    for i in range(time):
                        sleep(1)
                        driver.swipe(x1, y, x2, y, 300)
                    message = "向右滑动成功"
                    return message
            except Exception as e:
                return e

    elif func == "swipeone":
        direction = repalcenew(code[1].replace(")","").replace("'","").replace("\"",""))
        try:
            if direction == "down":
                sleep(2)
                l = driver.get_window_size()
                x = l['width'] * 0.5
                y1 = l["height"] * 0.75
                y2 = l["height"] * 0.5
                driver.swipe(x, y1, x, y2, 300)
                message = "向下滑动成功"
                return message
            elif direction == "up":
                sleep(2)
                l = driver.get_window_size()
                x = l["width"] * 0.5
                y1 = l["height"] * 0.75
                y2 = l["height"] * 0.5
                driver.swipe(x,y2,x,y1, 300)
                message = "向上滑动成功"
                return message
            elif direction == "left":
                sleep(2)
                l = driver.get_window_size()
                x1 = l["width"] * 0.5
                x2 = l["width"] * 0.75
                y = l["height"] * 0.5
                driver.swipe(x1,y,x2,y, 300)
                message = "向左滑动成功"
                return message
            elif direction == "right":
                sleep(2)
                l = driver.get_window_size()
                x1 = l["width"] * 0.75
                x2 = l["width"] * 0.5
                y = l["height"] * 0.5
                driver.swipe(x1,y,x2,y, 300)
                message = "向右滑动成功"
                return message
        except Exception as e:
            return e

    elif func == "collect":
        try:
            res = repalcenew(code[1].replace(")","").replace("'","").replace("\"",""))
            if res == "私照":
                sid = finddatainDB("t_ui_script", "script_name=私照code")[0][0]
                code = finddatainDB("t_ui_script_code", "script_id=%d" %sid)
                dealcodecount(code)
                message = "收藏预处理流程走完,结果请根据上方几条分析。"
                return message
            else:
                sid = finddatainDB("t_ui_script", "script_name=视频code")[0][0]
                code = finddatainDB("t_ui_script_code", "script_id=%d" % sid)
                dealcodecount(code)
                message = "收藏预处理流程走完,结果请根据上方几条分析。"
                return message
        except Exception:
            message = "收藏预处理流程仍有问题"
            return message
    elif func == "getname":
        try:
            code[1] = code[1].replace(")", "").split(",")
            type = code[1][0]
            id = code[1][1].replace("\"", "").replace("'", "")
            id = repr(id)
            elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" % (type, id))
            element = elementacc[0][4]
            text = driver.find_element_by_xpath(element).text
            return text
        except Exception as e:
            return e

    elif func == "登录预处理":
        try:
            a = finddatainDB("t_ui_script_code", "cid=23")
            dealcodecount(a)
            message = "登录预处理成功"
            return message
        except Exception:
            message = "登录预处理还有问题"
            return message

    elif func == 'quit':
        try:
            sleep(5)
            driver.quit()
            message="结束任务"
            return message
        except Exception as e:
            message = "结束失败,请重启客户端,消息为%s" %e
            return message

    elif  func == "deleteStoreData":
        code[1] = code[1].replace(")", "").split(",")
        type = code[1][0]
        id = code[1][1].replace("\"", "").replace("'", "")
        id = repr(id)
        elementacc = finddatainDB("t_ui_element", "ele_type=%s and ele_name=%s" % (type, id))
        element = elementacc[0][4]
        driver.implicitly_wait(10)
        try:
            a = driver.find_element_by_id(element).is_enabled()
            if a:
                driver.find_element_by_xpath()
                message = "找到元素(%s)成功,无需重跑" %elementacc[0][3]
                return message
            else:
                print("未找到元素")
                sendMessageToDD("未找到元素(%s)" %elementacc[0][3])
                message = "没有找到(%s)元素" %elementacc[0][3]
        except Exception as e:
            print("%s" %e)
            sendMessageToDD("%s" %e)
            message = e
            return message

    elif func == "返回":
        try:
            driver.back()
            message = "返回成功"
            return message
        except Exception as e:
            return e

    elif func == "刷新":
        try:
            driver.refresh()
            message = "刷新成功"
            return message
        except Exception as e:
            return e

    elif func == "test":
        try:
            daima = repalcenew(code[1].replace(")", "").replace("'", "").replace("\"", ""))
            changedatainDB("t_ui_script_code","num_repeat=0","cid=1")
            message = "重跑次数修改成功"
            return daima,message
        except Exception:
            message = "有问题"
            return message

    else:
        message="输入的函数有问题,请确认后重新编写(空行也会报这个错,如果确认是空行请忽略)"
        return message

你可能感兴趣的:(2019-06-17阶段性总结(Python3))