JSON文件读写
- 读取JSON文件
- with open(‘data.json’, encoding=‘UTF-8’) as f :
- data = json.load(f) # 返回的数据类型为字典或列表
- 写入JSON文件
- param = {‘name’: ‘tom’, ‘age’: 20}
- with open(‘data2.json’, ‘w’, encoding=‘UTF-8’) as f:
- json.dump(param, f)
(1)加法器案例:网址
浏览器驱动工具类(驱动启动和关闭)
from selenium import webdriver
class DriverUtil:
_driver = None
@classmethod
def get_driver(cls):
if cls._driver is None:
cls._driver = webdriver.Chrome()
cls._driver.maximize_window()
cls._driver.implicitly_wait(10)
cls._driver.get("http://cal.apple886.com/")
return cls._driver
@classmethod
def quit_driver(cls):
if cls._driver:
cls._driver.quit()
cls._driver = None
浏览器驱动工具类(计算器页面-对象库层)
from selenium.webdriver.common.by import By
from ddt.calculator.utils import DriverUtil
class CalculatorPage:
def __init__(self):
self.driver = DriverUtil.get_driver()
self.digit_btn = (By.ID, "simple{}")
self.add_btn = (By.ID, "simpleAdd")
self.eq_btn = (By.ID, "simpleEqual")
self.result = (By.ID, "resultIpt")
def find_digit_btn(self, digit):
location = (self.digit_btn[0], self.digit_btn[1].format(digit))
return self.driver.find_element(*location)
def find_add_btn(self):
return self.driver.find_element(*self.add_btn)
def find_eq_btn(self):
return self.driver.find_element(*self.eq_btn)
def find_result_btn(self):
return self.driver.find_element(*self.result)
浏览器驱动工具类(计算器页面-操作层)
class CalculatorHandle:
def __init__(self):
self.calculator_page = CalculatorPage()
def click_digit_btn(self, digit):
self.calculator_page.find_digit_btn(digit).click()
def click_add_btn(self):
self.calculator_page.find_add_btn().click()
def click_eq_btn(self):
self.calculator_page.find_eq_btn().click()
def get_result(self):
return self.calculator_page.find_result_btn().get_attribute("value")
def input_numbers(self, numbers):
for num in numbers:
self.click_digit_btn(num)
浏览器驱动工具类(计算器页面-业务层)
class CalculatorProxy:
def __init__(self):
self.calculator_handle = CalculatorHandle()
def add(self, num1, num2):
self.calculator_handle.input_numbers(str(num1))
self.calculator_handle.click_add_btn()
self.calculator_handle.input_numbers(str(num2))
self.calculator_handle.click_eq_btn()
def get_result(self):
return self.calculator_handle.get_result()
浏览器驱动工具类(计算器页面-数据驱动层)
import json
import time
import unittest
from parameterized import parameterized
from ddt.calculator.page.calculator_page import CalculatorProxy
from ddt.calculator.utils import DriverUtil
class TestCalculator(unittest.TestCase):
def build_data(self):
test_data = []
with open("../data/calculator.json", encoding='UTF-8') as f:
test_data = json.load(f)
print("test_data=", test_data)
return test_data
@classmethod
def setUpClass(cls):
cls.driver = DriverUtil.get_driver()
cls.calculatorProxy = CalculatorProxy()
@classmethod
def tearDownClass(cls):
DriverUtil.quit_driver()
@parameterized.expand(self.build_data)
def test_add(self, a, b, expect):
print('a={} b={} expect={}'.format(a, b, expect))
self.calculatorProxy.add(a, b)
result = self.calculatorProxy.get_result()
print("result=", result)
self.assertEqual(result, str(expect))
(2)对TPshop网站的登录模块进行单元测试
浏览器驱动工具类(驱动启动和关闭)
class DriverUtil:
_driver = None
@classmethod
def get_driver(cls):
if cls._driver is None:
cls._driver = webdriver.Firefox()
cls._driver.maximize_window()
cls._driver.implicitly_wait(10)
cls._driver.get("http://localhost")
return cls._driver
@classmethod
def quit_driver(cls):
if cls._driver:
cls._driver.quit()
cls._driver = None