Selenium(Python)頁面對象+資料驅動測試架構

來源:互聯網
上載者:User

標籤:oca   com   local   erb   close   nbsp   百度   unittest   unp   

整個工程的目錄結構:

 

 

常用方法類:

 

class SeleniumMethod(object):
# 封裝Selenium常用方法

def __init__(self, driver):
self.driver = driver

def getTitle(self):
# 擷取頁面標題
return self.driver.title

def clearAndInput(self, location, value):
# 根據xpath定位元素並清除、輸入
element = self.driver.find_element_by_xpath(location)
element.clear()
element.send_keys(value)

def click(self, location):
# 根據xpath定位元素並點擊
return self.driver.find_element_by_xpath(location).click()

def getText(self, location):
# 根據xpath定位元素並擷取文本值
return self.driver.find_element_by_xpath(location).text



頁面對象類:


from commonMethod.WebDriverMethod import SeleniumMethod

class BaiduPage(SeleniumMethod):
# 百度頁面對象

inputBox = ".//*[@id=‘kw‘]"
# 百度輸入框
searchBotton = ".//*[@id=‘su‘]"
# 百度搜尋按鈕
oneResult = ".//*[@id=‘1‘]/h3/a"
# 搜尋結果第一行

def searchChinese(self, keyword):
# 搜尋索引鍵
self.clearAndInput(self.inputBox, keyword)
self.click(self.searchBotton)


測試案例類:

import csv
import unittest
from time import sleep

from ddt import ddt, data, unpack
from selenium import webdriver

from pageObject.BaiduHome import BaiduPage


def getCsvData():
value_rows = []
dataPath = "testCase/testDirectory/testData/CsvTestData.csv"
with open(dataPath, encoding="UTF-8") as f:
f_csv = csv.reader(f)
next(f_csv)
for r in f_csv:
value_rows.append(r)
return value_rows

@ddt
class MyTestCase(unittest.TestCase):

def setUp(self):
self.driver = webdriver.Firefox(executable_path="drive/geckodriver.exe")
self.driver.maximize_window()
self.driver.get("https://www.baidu.com/")
assert self.driver.title, "百度一下,你就知道"
sleep(2)

@data(*getCsvData())
@unpack
def test_searchChinese(self, searchTerm, answerTitle, answerResult):
"""百度搜尋的測試案例"""
homePage = BaiduPage(self.driver)
homePage.searchChinese(searchTerm)
sleep(3)
assert homePage.getTitle(), answerTitle
assert homePage.getText(homePage.oneResult), answerResult

def tearDown(self):
self.driver.close()
self.driver.quit()

if __name__ == ‘__main__‘:
unittest.main()


測試執行類:

import HTMLTestRunner

from time import strftime, localtime, time
from unittest import defaultTestLoader


if __name__ == "__main__":
testProject = "testCase"
organize = defaultTestLoader.discover(testProject, pattern="*Test.py")
now = strftime("%Y-%m-%M-%H_%M_%S", localtime(time()))
filename = "testCase/testDirectory/report/" + now + ".html"
fp = open(filename, "wb")
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
verbosity=2,
title="測試報告的標題",
description="測試案例執行的情況")
runner.run(organize)
fp.close()


測試資料:

搜尋關鍵詞,搜尋結果標題,搜尋結果第一行
中國,中國_百度搜尋,中國_百度百科
美國,美國_百度搜尋,美國_百度百科
英國,英國_百度搜尋,英國_百度百科


測試報告:

 




Selenium(Python)頁面對象+資料驅動測試架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.