1 # -*- coding:utf-8 -*- 2 3 """ 4 python 实现对象模型 5 创建 bmicalcpage 类 6 """ 7 8 class bmicalcpage(object): 9 def __init__(self, driver): 10 self._driver = driver 11 self._url = r'F:电子书seleniumSelenium Testing Tools Cookbook(中文)demoBMICalculator.html' 12 self._title = 'BMI Calculator' 13 14 @property 15 def is_loaded(self): 16 return self._driver.title == self._title 17 18 @property 19 def bmi(self): 20 bmi_field = self._driver.find_element_by_id('bmi') 21 return bmi_field.get_attribute('value') 22 23 @property 24 def bmi_category(self): 25 bmi_category_field = self._driver.find_element_by_id('bmi_category') 26 return bmi_category_field.get_attribute('value') 27 28 def open(self): 29 self._driver.get(self._url) 30 31 def calculate(self, height, weight): 32 height_field = self._driver.find_element_by_id('heightCMS') 33 weight_field = self._driver.find_element_by_id('weightKg') 34 calc_button = self._driver.find_element_by_id('Calculate') 35 36 height_field.send_keys(height) 37 weight_field.send_keys(weight) 38 calc_button.click() 39 40 def close(self): 41 self._driver.close()
1 # -*-coding:utf-8 -*- 2 """ 3 创建 BMI 计算的测试 4 """ 5 from selenium import webdriver 6 from BmiCalcPage import bmicalcpage 7 import unittest 8 9 class BmiCalcTest(unittest.TestCase): 10 def testCalc(self): 11 driver = webdriver.Firefox() 12 bmi_calc = bmicalcpage(driver) 13 bmi_calc.open() 14 15 self.assertTrue(bmi_calc.is_loaded) 16 17 bmi_calc.calculate('181', '80') 18 19 self.assertEqual('24.4', bmi_calc.bmi) 20 self.assertEqual('Normal', bmi_calc.bmi_category) 21 bmi_calc.close()