1. 程式人生 > >關鍵字驅動模式 實現登錄qq郵箱並發送郵件的自動化測試

關鍵字驅動模式 實現登錄qq郵箱並發送郵件的自動化測試

mman quit http n) end term 按鈕 selenium com

測試數據文件
技術分享圖片


測試腳本:
#encoding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By

#定義獲取excel所有行的方法
def get_rows(file_path):

    from openpyxl import load_workbook
    try:
        wb = load_workbook(file_path)
        sheet = wb.active
        rows = []
        for row in sheet.iter_rows():
            rows.append(row)
        return rows
    except Exception as e:
        print(e)

#定義獲取頁面元素的方法
def get_element(driver,locateType,locatorexpression):
    try:
        if locateType.lower() == "id":
            locateType = By.ID
        elif locateType.lower() == "xpath":
            locateType = By.XPATH

        element = driver.find_element(locateType,locatorexpression)
        #print(element)
        return element
    except Exception as e:
        raise e

#打開瀏覽器關鍵字
def open_browser(browser):
    global driver
    try:
        if browser.lower() == "chrome":
            driver = webdriver.Chrome(executable_path="e:\\chromedriver")
        elif browser.lower() == "firefox":
            driver = webdriver.Firfox(executable_path = "e:\\geckodriver")
    except Exception as e:
        raise e

#訪問指定網址關鍵字
def visit_url(url):
    global driver
    try:
        driver.get(url)
    except Exception as e:
        raise e

#輸入框輸入內容關鍵字   
def input_string(locateType,locatorexpression,content):
    global driver
    try:
        element = get_element(driver,locateType,locatorexpression)
        element.send_keys(content)
    except Exception as e:
        raise e

#點擊按鈕關鍵字
def click(locateType,locatorexpression):
    global driver
    try:
        element = get_element(driver,locateType,locatorexpression)
        element.click()
    except Exception as e:
        raise e

#暫停等待關鍵字
def pause(seconds):
    import time
    try:
        time.sleep(seconds)
    except Exception as e:
        raise e

#切換進入指定frame關鍵字      
def switch_to_frame(locateType,locatorexpression):
    global driver
    try:
        element = get_element(driver,locateType,locatorexpression)
        driver.switch_to.frame(element) 
    except Exception as e:
        raise e  
#從子frame切換回主frame關鍵字
def switch_to_parent_frame():
    global driver
    try:

        driver.switch_to.parent_frame() 
    except Exception as e:
        raise e      

#斷言關鍵字
def assert_word(assertWord):
    global driver
    try:
        assert assertWord in driver.page_source
    except Exception as e:
        raise e

#退出瀏覽器驅動,並關閉所有瀏覽器窗口關鍵字
def close_browser():
    global driver
    try:
        driver.quit()
    except Exception as e:
        raise e
#測試主函數,實現測試主邏輯
def main():
    import traceback
    try:
        test_file_path = "G:\\個人\\學習\\光榮之路\\自動化\\自動化練習\\testdata.xlsx"
        excel_rows = get_rows(test_file_path)
        #遍歷測試數據庫文件excel的每行
        for row in excel_rows[1:]:
            action = row[1].value
            locateType = row[2].value
            locatorExpression = row[3].value
            operateValue = row[4].value
            #print(action,locateType,locatorExpression,operateValue)
            #根據每行的內容拼接需要執行的關鍵字
            if locateType == None and locatorExpression == None and operateValue == None:
                command = "%s()"  %action
            elif locateType == None and locatorExpression == None and operateValue != None:
                if  isinstance(operateValue,int):
                    command = "%s(%d)" %(action,operateValue)
                else:
                    command = "%s(‘%s‘)" %(action,operateValue)
            elif locateType != None and locatorExpression != None and operateValue == None:
                command = "%s(‘%s‘,‘%s‘)" %(action,locateType,locatorExpression)
            elif locateType != None and locatorExpression != None and operateValue != None:
                command = "%s(‘%s‘,‘%s‘,‘%s‘)" %(action,locateType,locatorExpression,operateValue)
            print(command)
            try:
                #執行關鍵字函數
                eval(command)
            except Exception as e:
                print(traceback.print_exc())

    except Exception as e:
        print(e)
if __name__ == "__main__":
    main()
    #driver = webdriver.Chrome(executable_path = "e:\\chromedriver")
    #driver.get("http://mail.qq.com")
    #login_frame = driver.find_element(By.XPATH,‘//iframe[@id="login_frame"]‘)
    #print(login_frame)
    #driver.switch_to.frame(login_frame)
    #username = driver.find_element(By.XPATH,‘//input[@id="u"]‘)
    #username.send_keys("xxxx")

關鍵字驅動模式 實現登錄qq郵箱並發送郵件的自動化測試