Python+Selenium+Unittest+Ddt+HTMLReport分佈式數據驅動自動化測試框架結構

一、Business:公共業務模塊,如登陸模塊,能夠把登陸模塊進行封裝供調用web

------login_business.pychrome

from Page_Object.Common_Page.login_page import Login_Page
from HTMLReport import logger
import time


def login(driver, username, password):
    """
    登陸業務
    :param driver:瀏覽器驅動
    :param username:用戶名
    :param password:密碼
    :return:None
    """
    logger().info(f"使用用戶名:{username},密碼:{password}進行登錄")
    login_page = Login_Page(driver)
    login_page.send_username(username)
    login_page.send_password(password)
    login_page.submit()
    time.sleep(2)

----Base_url.py:對應的url地址瀏覽器

二、Common:與業務無關的公共模塊,如對Selenium的二次封裝,方便後期的調用,還有一些工具類,如在讀取數據時須要對讀取文件進行封裝框架

三、PageOBject:這個就應該不用多說了,頁面元素的封裝,這個根據本身公司系統的業務去作分層封裝(不作過多解釋)less

四、report:測試報告工具

五、TestCase:測試用例層,下面以logincase爲例子測試

---logincase.pyui

import unittest
from Business.Base_url import url_index
import ddt
from Common.tools.read_txt import read_txt
from HTMLReport import logger
from Page_Object.Common_Page.index_page import Index_Page
from Common.selenium_library import SeleniumBase
from Business.login_business import login


@ddt.ddt
class Test_login(unittest.TestCase):

    def setUp(self):
        self.driver = SeleniumBase().get_web_driver()

    def tearDown(self):

        SeleniumBase(self.driver).quit()

    @ddt.unpack
    @ddt.data(*read_txt('TestData/login_user_password.txt'))
    def test_login(self, username, password, assert_type):
        se = SeleniumBase(self.driver)
        se.get(url_index)
        login(self.driver, username, password)
        se.add_page_screen_shot()

        if assert_type == '1':
            logger().info("斷言登錄成功")
            text = Index_Page(self.driver).login_success()

            self.assertIn("測試", text, '登錄成功斷言')

        elif assert_type == "2":
            text = self.driver.find_element_by_id("submit").text
            self.assertIn("當即登陸", text, '登錄失敗斷言')

        elif assert_type == "3":
            text = self.driver.find_element_by_id("submit").text
            self.assertIn("當即登陸", text, '登錄失敗斷言')

        elif assert_type == "4":
            text = self.driver.find_element_by_id("submit").text
            self.assertIn("當即登陸", text, '登錄失敗斷言')

        else:
            logger().info(f"未知斷言類型{assert_type}")
            self.assertTrue(False, "未知斷言類型")

六、TestData:測試數據,對應用例的數據都是在這裏去取url

七、TestSuite:測試套件,以登陸套件爲例firefox

---loginsuite.py

import unittest

from TestCase.LoginCase import login_case


def return_suite():
    suite=unittest.TestSuite()

    loader=unittest.TestLoader()

    suite.addTests(loader.loadTestsFromTestCase(login_case.Test_login))

    return suite

八、browser.ini:瀏覽器配置文件

[local]
local_browser = False
wait_time = 10

[browser]
name = chrome
;name = firefox
;name = ie
window_width =
window_height =

[grid]
command_executor = http://127.0.0.1:4444/wd/hub
headless = False
proxy =

九、run.py:總體運行文件

import unittest
from TestSuite.LinesSuite import lines_suite
from TestSuite.LinesSuite import itinerary_suite

import HTMLReport

suite = unittest.TestSuite()

# suite.addTests(login_suite.return_suite())
suite.addTests(lines_suite.return_suite())
suite.addTests(itinerary_suite.return_suite())

HTMLReport.TestRunner(
    report_file_name="test",
    title='WEB UI 自動化',
    description='登錄',
    thread_count=3
).run(suite)

十、總體框架結構圖:

相關文章
相關標籤/搜索