溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

簡單數(shù)據(jù)庫驅(qū)動實現(xiàn)

發(fā)布時間:2020-06-28 20:29:36 來源:網(wǎng)絡(luò) 閱讀:154 作者:qq5a16e6241946e 欄目:編程語言

數(shù)據(jù)文件

分別為要訪問的網(wǎng)址,輸入框的定位表達式,搜索按鈕的定位表達式,搜索關(guān)鍵字,斷言關(guān)鍵字

https://www.bing.com||//input[@id="sb_form_q"]||sb_form_go||"測試開發(fā)"||"測試開發(fā)"
https://www.sogou.com||//input[@id="query"]||stb||"測試開發(fā)"||"測試開發(fā)"

測試腳本:

方式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:封裝函數(shù)

#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("文件不存在!")

#定義實際的測試步驟:打開瀏覽器 輸入 關(guān)鍵字 進行搜索
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

#主函數(shù),獲取測試數(shù)據(jù),執(zhí)行測試步驟
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")
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI