python-Selenium自动化测试笔记(3)——关键字框架

这是对应的关键字框架,用来处理解析好的外部脚本,降低自动化测试门槛:

import time
from selenium.webdriver.common.by import By
from DrivenFramework import Driver
from selenium import webdriver
from FileOp.TXTOP import TXTop
from FileOp.SQLOP import MySQLOP

class switch(object):
    def __init__(self, value):
        self.value = value
        self.fall = False

    def __iter__(self):
        """Return the match method once, then stop"""
        yield self.match
        raise StopIteration

    def match(self, *args):
        """Indicate whether or not to enter a case suite"""
        if self.fall or not args:
            return True
        elif self.value in args:  # changed for v1.5, see below
            self.fall = True
            return True
        else:
            return False


def find_value(data, key):
    for value in data:
        if value[0] == key:
            temp = key
            return value[1]
        else:
            continue
    return None
def run_sample(path):
    pg = Driver.Page(webdriver.Chrome())
    f = TXTop.read_txt_file(type[TXTop], path+'.txt')
    c = TXTop.read_txt_file(type[TXTop], 'control.txt')
    data = MySQLOP.select_sql(type[MySQLOP], 'root', '', 'automation', 'select * from properties' )

    for line in f:
        line = line.replace('\n', '')
        oparry = line.split('@')
        for case in switch(oparry[0]):
            if case('goto'):
                pg.goto(find_value(data, oparry[1]))
                break
            if case('input'):
                pg.input([By.ID, find_value(data, oparry[1])], oparry[2])
                break
            if case('click'):
                pg.click([By.ID, find_value(data, oparry[1])])
                break
            if case('sleep'):
                time.sleep(float(oparry[1]))
                break
            if case('get_text'):  # default, could also just omit condition or 'if True'
                content = pg.get_element_text([By.CSS_SELECTOR, find_value(data, oparry[1])])
                print(content)
                break
            if case('script'):
                run_sample(oparry[1])
                break
            if True:
                break

def run_list(path):
    pg = Driver.Page(webdriver.Chrome())
    f = TXTop.read_txt_file(type[TXTop], path)
    for line in f:
        run_sample(line)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值