Python_Xpath应用_关键字驱动

关键字驱动

一、简单示例

数据文件内容:

open||None||firefox

visit||None||http://www.sogou.com

input||//input[@id='query']||光荣之路自动化测试

click||//input[@id='stb']||None

assert||None||测试

close||None||None

 

程序运行内容:

from selenium import webdriver

import time

import os.path

import sys

 

def get_data_from_file(data_file_path,coding="utf-8"):

    if not os.path.exists(data_file_path):

        print("数据文件的路径不存在!")

        sys.exit(0)

    test_data = []

    with open(data_file_path,'r',encoding=coding) as fp:

        for line in fp:

            if line.strip()=="":

                continue

            test_data.append(line.split("||"))

    return test_data

test_data = get_data_from_file("e:\\testcase.txt")

print(test_data)

 

driver = None

def open(browser_name):

    global driver

    if browser_name=="firefox":

        driver=webdriver.Firefox(executable_path = r"e:\geckodriver")   

 

def visit(url):

    global driver

    driver.get(url)

 

def input(xpath,value):

    global driver

    driver.find_element_by_xpath(xpath).send_keys(value)

 

for line in test_data[:3]:

        action =line[0]

        xpath = line[1]

        value = line[2].strip()

        print(action,xpath,value)

        if (xpath == "None") and (value == "None"):

            print("1",action+"()")

            exec(action+"()")

        elif (xpath == "None") and (value  != "None"):

             print(2,action+"(\""+value+"\")")

             exec(action+"(\""+value+"\")")

        elif (xpath  != "None") and (value  == "None"):

             print(3,action+"(\""+xpath+"\")")

             exec(action+"(\""+xpath+"\")")

        else:

            print(4,action+"(\""+xpath+"\",\""+value+"\")")

            exec(action+"(\""+xpath+"\",\""+value+"\")")

 

posted @ 2019-10-21 23:51  翻滚的小强  阅读(216)  评论(0编辑  收藏  举报