Selenium(Python)页面对象+数据驱动测试框架

整个工程的目录结构:

Selenium(Python)页面对象+数据驱动测试框架Selenium(Python)页面对象+数据驱动测试框架

常用方法类:

class SeleniumMethod(object):<br />    # 封装Selenium常用方法<br /><br />    def __init__(self, driver):<br />        self.driver = driver<br /><br />    def getTitle(self):<br />        # 获取页面标题<br />        return self.driver.title<br /><br />    def clearAndInput(self, location, value):<br />        # 根据xpath定位元素并清除、输入<br />        element = self.driver.find_element_by_xpath(location)<br />        element.clear()<br />        element.send_keys(value)<br /><br />    def click(self, location):<br />        # 根据xpath定位元素并点击<br />        return self.driver.find_element_by_xpath(location).click()<br /><br />    def getText(self, location):<br />        # 根据xpath定位元素并获取文本值<br />        return self.driver.find_element_by_xpath(location).text<br /><br /><br /><br />页面对象类:<br /><br /><br />
from commonMethod.WebDriverMethod import SeleniumMethod<br /><br />class BaiduPage(SeleniumMethod):<br />    # 百度页面对象<br /><br />    inputBox = ".//*[@id='kw']"<br />    # 百度输入框<br />    searchBotton = ".//*[@id='su']"<br />    # 百度搜索按钮<br />    oneResult = ".//*[@id='1']/h3/a"<br />    # 搜索结果第一行<br /><br />    def searchChinese(self, keyword):<br />        # 搜索关键字<br />        self.clearAndInput(self.inputBox, keyword)<br />        self.click(self.searchBotton)<br /><br /><br />测试用例类:<br /><br />
import csv<br />import unittest<br />from time import sleep<br /><br />from ddt import ddt, data, unpack<br />from selenium import webdriver<br /><br />from pageObject.BaiduHome import BaiduPage<br /><br /><br />def getCsvData():<br />    value_rows = []<br />    dataPath = "testCase/testDirectory/testData/CsvTestData.csv"<br />    with open(dataPath, encoding="UTF-8") as f:<br />        f_csv = csv.reader(f)<br />        next(f_csv)<br />        for r in f_csv:<br />            value_rows.append(r)<br />    return value_rows<br /><br />@ddt<br />class MyTestCase(unittest.TestCase):<br /><br />    def setUp(self):<br />        self.driver = webdriver.Firefox(executable_path="drive/geckodriver.exe")<br />        self.driver.maximize_window()<br />        self.driver.get("https://www.baidu.com/")<br />        assert self.driver.title, "百度一下,你就知道"<br />        sleep(2)<br /><br />    @data(*getCsvData())<br />    @unpack<br />    def test_searchChinese(self, searchTerm, answerTitle, answerResult):<br />        """百度搜索的测试用例"""<br />        homePage = BaiduPage(self.driver)<br />        homePage.searchChinese(searchTerm)<br />        sleep(3)<br />        assert homePage.getTitle(), answerTitle<br />        assert homePage.getText(homePage.oneResult), answerResult<br /><br />    def tearDown(self):<br />        self.driver.close()<br />        self.driver.quit()<br /><br />if __name__ == '__main__':<br />    unittest.main()<br /><br /><br />测试执行类:<br /><br />
import HTMLTestRunner<br /><br />from time import strftime, localtime, time<br />from unittest import defaultTestLoader<br /><br /><br />if __name__ == "__main__":<br />    testProject = "testCase"<br />    organize = defaultTestLoader.discover(testProject, pattern="*Test.py")<br />    now = strftime("%Y-%m-%M-%H_%M_%S", localtime(time()))<br />    filename = "testCase/testDirectory/report/" + now + ".html"<br />    fp = open(filename, "wb")<br />    runner = HTMLTestRunner.HTMLTestRunner(<br />        stream=fp,<br />        verbosity=2,<br />        title="测试报告的标题",<br />        description="测试用例执行的情况")<br />    runner.run(organize)<br />    fp.close()
<br /><br />测试数据:<br /><br />
搜索关键词,搜索结果标题,搜索结果第一行<br />中国,中国_百度搜索,中国_百度百科<br />美国,美国_百度搜索,美国_百度百科<br />英国,英国_百度搜索,英国_百度百科<br /><br /><br />测试报告:<br /><br />
Selenium(Python)页面对象+数据驱动测试框架
<br /><br />

相关推荐