# -*- coding:utf-8 -*- """
python 实现对象模型
创建 bmicalcpage 类
""" class bmicalcpage(object):
def __init__(self, driver):
self._driver = driver
self._url = r'F:\电子书\selenium\Selenium Testing Tools Cookbook(中文)\demo\BMICalculator.html'
self._title = 'BMI Calculator' @property
def is_loaded(self):
return self._driver.title == self._title @property
def bmi(self):
bmi_field = self._driver.find_element_by_id('bmi')
return bmi_field.get_attribute('value') @property
def bmi_category(self):
bmi_category_field = self._driver.find_element_by_id('bmi_category')
return bmi_category_field.get_attribute('value') def open(self):
self._driver.get(self._url) def calculate(self, height, weight):
height_field = self._driver.find_element_by_id('heightCMS')
weight_field = self._driver.find_element_by_id('weightKg')
calc_button = self._driver.find_element_by_id('Calculate') height_field.send_keys(height)
weight_field.send_keys(weight)
calc_button.click() def close(self):
self._driver.close()
# -*-coding:utf-8 -*-
"""
创建 BMI 计算的测试
"""
from selenium import webdriver
from BmiCalcPage import bmicalcpage
import unittest class BmiCalcTest(unittest.TestCase):
def testCalc(self):
driver = webdriver.Firefox()
bmi_calc = bmicalcpage(driver)
bmi_calc.open() self.assertTrue(bmi_calc.is_loaded) bmi_calc.calculate('', '') self.assertEqual('24.4', bmi_calc.bmi)
self.assertEqual('Normal', bmi_calc.bmi_category)
bmi_calc.close()