简单数据库驱动实现

数据文件

公司主营业务:成都网站制作、成都做网站、外贸营销网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出邵东免费做网站回馈大家。

分别为要访问的网址,输入框的定位表达式,搜索按钮的定位表达式,搜索关键字,断言关键字

https://www.bing.com||//input[@id="sb_form_q"]||sb_form_go||"测试开发"||"测试开发"
https://www.sogou.com||//input[@id="query"]||stb||"测试开发"||"测试开发"

测试脚本:

方式1:脚本方式
#encoding=utf-8
from selenium import webdriver
import time
with open("e://data.txt",encoding="gbk") as fp:
    datas = fp.readlines()
driver = webdriver.Firefox(executable_path = "d:\\geckodriver")   
for data in datas:
    data = data.strip().split("||")
    driver.get(data[0])
    searchBox = driver.find_element_by_xpath(data[1])
    searchBox.send_keys(data[3])
    searchButton = driver.find_element_by_id(data[2])
    searchButton.click()
    time.sleep(5)
    assert data[4] in driver.page_source

driver.quit()

方式2:封装函数

#encoding=utf-8
from selenium import webdriver
import time,os
driver = webdriver.Firefox(executable_path = "d:\\geckodriver")
def get_data(file_path):
    if os.path.exists(file_path):
        with open(file_path,encoding="gbk") as fp:
            test_datas = fp.readlines()
        return test_datas
    else:
        print("文件不存在!")

#定义实际的测试步骤:打开浏览器 输入 关键字 进行搜索
def test_step(url,searchBox_xpath,searchButton_id,searchWord,assertWord):
    global driver
    driver.get(url)
    searchBox = driver.find_element_by_xpath(searchBox_xpath)
    searchButton = driver.find_element_by_id(searchButton_id)
    searchBox.send_keys(searchWord)
    searchButton.click()
    time.sleep(5)
    assert assertWord in driver.page_source

#主函数,获取测试数据,执行测试步骤
def main(file_path):
    global driver
    test_datas = get_data(file_path)
    for data in test_datas:
        if data.strip() != "":
            data = data.split("||")
            url = data[0].strip()
            print(url)
            searchBox_xpath = data[1].strip()

            searchButton_id = data[2].strip()
            searchWord = data[3].strip()
            assertWord = data[4].strip()

            test_step(url,searchBox_xpath,searchButton_id,searchWord,assertWord)
    driver.quit()

if __name__ == "__main__":

    main("e:\\data.txt")

名称栏目:简单数据库驱动实现
文章分享:http://ybzwz.com/article/jpehph.html