selenium2 python自动化测试实战(回归测试)

selenium2 python自动化测试实战

最近接手商城的项目,针对后台测试,功能比较简单,但是流程比较繁多,涉及到前后台的交叉测试。在对整个项目进行第一轮测试完成之后,考虑以后回归测试任务比较重,为了减轻回归测试的工作量,所以考虑后台可以进行部分自动化测试。

之前一个项目使用robotframework进行回归测试,了解了python语言,所以这次就使用python2.7+selenium2进行自动化回归测试了。

配置环境

  • python2.7
  • 运行工具pycharm2017

一、项目结构介绍

mztestpro/

|------------bbs/

||---------------data/

|||-------------------upload.au3

|||-------------------upload.exe

||---------------report/

|||----------------------image/

||||----------------------------login.jpg

||||----------------------------addproduct.jpg

|||----------------------Y-02-02 10_27_10result.html

|||----------------------Y-02-02 10_28_10result.html

||---------------test_case/

|||-------------------------models/

||||-------------------------------driver.py

||||-------------------------------function.py

||||-------------------------------myunit.py

|||-------------------------page_obj/

||||----------------------------------agentPage.py

||||----------------------------------agentQueryPage.py

||||----------------------------------base.py

||||----------------------------------employeePage.py

||||----------------------------------employeeQueryPage.py

||||----------------------------------hotelPage.py

||||----------------------------------hotelQueryPage.py

||||----------------------------------productcategoryPage.py

||||----------------------------------productcategoryQueryPage.py

||||----------------------------------productinfoPage.py

||||----------------------------------productinfoQueryPage.py

||||----------------------------------rolePage.py

||||----------------------------------roleQueryPage.py

||||----------------------------------userPage.py

||||----------------------------------userQueryPage.py

||||----------------------------------warehousePage.py

||||----------------------------------warehouseQueryPage.py

|||-------------------------agent_sta.py

|||-------------------------employee_sta.py

|||-------------------------hotel_sta.py

|||-------------------------login_sta.py

|||-------------------------productcategory.py

|||-------------------------productinfo_sta.py

|||-------------------------role_sta.py

|||-------------------------user_sta.py

|||-------------------------warehouse_sta.py

|------------driver/

||-----------------sun.jpg

||-----------------upload.au3

||-----------------upload.exe

||-----------------upload1.au3

||-----------------upload1.exe

|------------package/

||--------------------addr.py

||--------------------card.py

||--------------------HTMLTestRunner.py

|------------run_bbs_test.py

|------------startup.bat

|­­­­­------------自动化测试项目说明文档.docx

项目结构如图所示


项目框架采用的是po模式,如果对框架有疑问可以参考selenium+python自动化测试系列(一):登录Selenium2+Python自动化测试实战

1.mztestpro测试项目模块介绍

bbs:用于存放BBS项目的测试用例、测试报告和测试数据等。

driver:用于存放浏览器驱动。如selenium­server­standalone­2.47.0jar、chromedriver.exe、IEDriverServer.exe等。在执行测试前根据执行场景将浏览器驱动复制到系统环境path目录下。

package:用于存放自动化所用到的扩展包。例如:HTMLTestRunner.py属于一个单独模块和随机手机号和身份证号模块

run_bbs_test.py:项目主程序。用来运行商城(BBS)自动化用例。

startup.bat:用于启动selenium server,默认启动driver目录下的selenium­server­standalone­2.44.0.jar。

自动化测试项目说明文档.docx:介绍当前项目的架构、配置和使用说明。

2.bbs目录

data:该目录用来存放测试相关数据。

report:用于存放HTML测试报告。其下面创建了image目录用于存放测试过程中的截图。

test_case:测试用例目录,用于存放测试用例及相关模块。

3.test_case

models:该目录下存放了一些公共的配置函数及公共类。

page_obj:该目录用于存放测试用例的页面对象(Page Object)。根据自定义规则,以“*Page.py”命名的文件为封装的页面对象文件。

_sta.py:测试用例文件。根据测试文件匹配规则,以“_sta.py”命名的文件被当作自动化测试用例执行。

二、编写公共模块

首先定义驱动文件:

...\mztestpro\bbs\test_case\models\driver.py

driver.py

# -*-coding:utf-8-*-
# _author_ = "janehost"
from selenium.webdriver import Remote
from selenium import webdriver
import sys
# 启动浏览器
reload(sys)
sys.setdefaultencoding('utf8')
def browser():
		driver = webdriver.Firefox()
		return driver


if __name__ == '__main__':
	dr = browser()
	dr.get("http://192.168.16.6/hotelManage")
	dr.quit()

定义浏览器驱动函数browser(),该函数可以进行配置,根据我们的需要,配置测试用例在不同的主机及浏览器下运行。

自定义测试框架类:

...\mztestpro\bbs\test_case\models\myunit.py

myunit.py

# -*-coding:utf-8-*-
# _author_ = "janehost"
import unittest,sys
from selenium import webdriver
from driver import browser
reload(sys)
sys.setdefaultencoding('utf8')

class MyTest(unittest.TestCase):
	def setUp(self):
	self.driver = browser()
	self.driver.implicitly_wait(10)
	self.driver.maximize_window()

def tearDown(self):
	self.driver.quit()


if __name__ == '__main__':
	unittest.main()

定义MyTest()类用于集成unittest.TestCase类,因为笔者创建的所有测试类中setUp()与tearDown()方法所做的
事情相同,所以,将他们抽象为MyTest()类,好处就是在编写测试用例时不再考虑这两个方法的实现。
定义截图函数:
...\mztestpro\bbs\test_case\models\function.py

function.py

# -*-coding:utf-8-*-
# _author_ = "janehost"
from selenium import webdriver
import os,sys
reload(sys)
sys.setdefaultencoding('utf8')
# 截图函数
def insert_img(driver, file_name):
	base_dir = os.path.dirname(os.path.dirname(__file__))
	base_dir = str(base_dir)
	base_dir = base_dir.replace('\\', '/')
	base = base_dir.split('test_case')[0]
	file_path = base + "report/image/" + file_name
	driver.get_screenshot_as_file(file_path)


if __name__ == '__main__':
	driver = webdriver.Firefox()
	driver.get("http://192.168.16.6/hotelManage")
	insert_img(driver, 'login.jpg')
	driver.quit()

创建截图函数insert_img(),为了保持自动化项目的移植性,采用相对路径的方式将测试截图保持到.\report\image目录中。

三、编写Page Object

首先创建基础Page基础类(商城为例):
...\mztestpro\bbs\test_case\page_obj\base.py

base.py

# -*-coding:utf-8-*-
# _author_ = "janehost"


from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import sys
reload(sys)
sys.setdefaultencoding('utf8')
class Page(object):

	'''
	页面基础类,用于所有页面的继承
	'''

	base_url = "http://192.168.16.6/hotelManage"

	def __init__(self, selenium_driver, base_url=base_url, parent=None):
		self.base_url = base_url
		self.driver = selenium_driver
		self.timeout = 30
		self.parent = parent

	def _open(self, url):
		url = self.base_url + url
		self.driver.get(url)
		assert self.on_page(), 'Did not land on %s' % url

	def open(self):
		self._open(self.url)

	def on_page(self):
		#return (self.driver.current_url).encode('utf-8') == (self.base_url + self.url)
		return self.driver.current_url.encode('utf-8') == (self.base_url + self.url)

	def find_element(self, *loc):
		# return self.driver.find_element(*loc)
		try:
		# 确保所有元素是可见的
		# 注意:以下入参为元组的元素,需要加*。python存在这种特性,就是将入参放在元组里。
		#WebDriverWait(self.driver,10).until(lambda driver: driver.find_element(*loc).is_displayed())
		# 注意:以下入参本身是元组,不需要加*
			WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(loc))
			return self.driver.find_element(*loc)
		except:
			print u"%s 页面中未能找到 %s 元素"%(self, loc)

	def find_elements(self, *loc):
		return self.driver.find_elements(*loc)

	def script(self, src):
		return self.driver.execute_script(src)

	def swtich_frame(self, loc):
		return self.driver.swith_to_frame(loc)

	def send_keys(self, loc, value, clear_first=True, click_first=True):
		try:
			# getattr相当于self.loc
			loc = getattr(self, "_%s" % loc)
			if click_first:
				self.find_element(*loc).click()
			if clear_first:
				self.find_element(*loc).clear()
			self.find_element(*loc).send_keys(value)
		except ArithmeticError:
			print u"%s 页面中未能找到 %s 元素" % (self, loc)

创建页面基础类,通过__init__()方法初始化参数:浏览器驱动、URL地址、超时时长等。定义基本方法:open()用于打开BBS地址:find_element()和find_elements()分别用来定位单个与多个元素;创建script()方法可以更简便地调用JavaScript代码。当然还可以对更多的WebDriver方法进行重定义。

创建BBS登录对象类:

...\mztestpro\bbs\test_case\page_obj\loginPage.py

loginPage.py

登录页面元素的封装page_obj\loginPage.py,代码如下

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:创建登录页面对象,对用户登录页面上的用户名/密码输入框、登录按钮和
提示信息等元素的定位进行封装。
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class login(Page):

	'''
	用户登录界面
	'''
	url = '/'
	# 登录用户名的定位
	login_username_loc = (By.ID, 'username')
	# 登录密码的定位
	login_password_loc = (By.ID,'password')
	# 登录按钮的定位
	login_button_loc = (By.CSS_SELECTOR,'.btn.btn-success.btn-block')
	# 登录错误提示的定位
	login_error_loc = (By.ID,'error_msg')
	# 登录成功用户名信息
	login_user_success_loc = (By.XPATH, 'html/body/div[3]/div[2]/ul/li[1]/a/strong')

	# 登录用户名
	def login_username(self, username):
		self.find_element(*self.login_username_loc).clear()
		self.find_element(*self.login_username_loc).send_keys(username)

	# 登录密码
	def login_password(self, password):
		self.find_element(*self.login_password_loc).clear()
		self.find_element(*self.login_password_loc).send_keys(password)

	# 登录按钮
	def login_button(self):
	self.find_element(*self.login_button_loc).click()

	# 统一登录入口
	def user_login(self, username="testuser01", password="testgood001"):
	# 获取用户名和页面登录
		self.open()
		self.login_username(username)
		self.login_password(password)
		self.login_button()
		sleep(3)

	# 登录错误提示信息
	def login_error_hint(self):
		return self.find_element(*self.login_error_loc).text

	# 登录成功用户名信息
	def login_user_success(self):
		#return self.find_element(*self.login_user_success_loc).text
		username = self.find_element(*self.login_user_success_loc).text
		username = username.strip('您好:')
		return username

创建登录页面对象,对用户登录页面上的用户名/密码输入框、登录按钮和提示信息等元素的定位进行封装。除此之外,还创建user_login()方法作为系统统一登录的入口。关于对操作步骤的封装可以放在Page Object当中,也可以放在测试用例当中,这个主要根据具体的需求来衡量。这里之所以要放在Page Object当中,主要考虑到还会有其他的测试用例调用到该登录方法。为username 和 password 入参数设置了默认值是为了方便其他用例在调用user_login()时不用再传递登录用户信息,因为该系统大多用例的执行使用该账号即可,同时也方便了在账号失效时的修改。

hotelPage.py

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:创建登录页面对象,对用户登录页面上的用户名/密码输入框、登录按钮和
提示信息等元素的定位进行封装。除此之外,还创建user_login()方法作为系统统
一登录的入口。关于对操作步骤的封装可以放在Page Object当中,
也可以放在测试用例当中,这个主要根据具体的需求来衡量。这里之所以要放在
Page Object当中,主要考虑到还会有其他的测试用例调用到该登录方法。
为username 和 password 入参数设置了默认值是为了方便其他用例在调用
user_login()时不用再传递登录用户信息,因为该系统大多用例的执行使用该账号
即可,同时也方便了在账号失效时的修改。
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class hotel(Page):

	'''
	酒店添加界面
	'''
	url = '/hotel/add'
	# 酒店名称的定位
	hotel_name_loc = (By.NAME, 'hotel.hotel_name')
	# 酒店联系人的定位
	hotel_contacts_loc = (By.NAME, 'hotel.contacts')
	# 酒店联系电话的定位
	hotel_phone_loc = (By.NAME, 'hotel.phone')
	# 酒店邮箱的定位
	hotel_email_loc = (By.NAME, 'hotel.email')
	# 酒店地址的定位
	hotel_address_loc = (By.NAME, 'hotel.address')
	# 酒店对账日的定位,前期默认
	# 酒店床垫最小消费时间的定位
	hotel_hour_loc = (By.NAME, 'hotel.min_hour')
# 酒店照片上传,使用autoit进行上传操作
	hotel_pic_loc = (By.ID, 'upload1')
	# 酒店添加页面中的保存按钮的定位
	hotel_button_loc = (By.XPATH, 'html/body/div[1]/form/div[3]/div/button')

	# 酒店名称
	def add_hotel_name(self, hotel_name):
		self.find_element(*self.hotel_name_loc).clear()
		self.find_element(*self.hotel_name_loc).send_keys(hotel_name)

	# 酒店联系人
	def add_hotel_contacts(self, hotel_contacts):
		self.find_element(*self.hotel_contacts_loc).clear()
		self.find_element(*self.hotel_contacts_loc).send_keys(hotel_contacts)

	# 酒店联系电话
	def add_hotel_phone(self, hotel_phone):
		self.find_element(*self.hotel_phone_loc).clear()
		self.find_element(*self.hotel_phone_loc).send_keys(hotel_phone)

	# 酒店邮箱
	def add_hotel_email(self, hotel_email):
		self.find_element(*self.hotel_email_loc).clear()
		self.find_element(*self.hotel_email_loc).send_keys(hotel_email)

	# 酒店地址
	def add_hotel_address(self, hotel_address):
		self.find_element(*self.hotel_address_loc).clear()
		self.find_element(*self.hotel_address_loc).send_keys(hotel_address)

	# 酒店床垫最小消费市场
	def add_hotel_hour(self, hotel_hour):
		self.find_element(*self.hotel_hour_loc).clear()
		self.find_element(*self.hotel_hour_loc).send_keys(hotel_hour)

	# # 酒店页面js打开方法
	# def get_hotel_url(self, url):
	# self.script(self.base_url+url)

	# 酒店添加页面保存按钮
	def add_hotel_button(self):
		self.find_element(*self.hotel_button_loc).click()

	# 统一酒店添加入口
	def add_hotel(self, hotel_name="test", hotel_contacts="123456",
		hotel_phone="15245898745", hotel_email="23090932@qq.com",
		hotel_address="weosdsdf", hotel_hour="2"):
		# 获取酒店添加页面
		self.open()
		# self.script(self.base_url + self.url)
		self.add_hotel_name(hotel_name)
		sleep(3)
		self.add_hotel_contacts(hotel_contacts)
		self.add_hotel_phone(hotel_phone)
		sleep(3)
		self.add_hotel_email(hotel_email)
		self.add_hotel_address(hotel_address)
		self.add_hotel_hour(hotel_hour)
		sleep(3)
		self.add_hotel_button()
		sleep(3)

		# 错误提示信息

hotelQueryPage.py

    主要为了验证添加的酒店信息正确,酒店添加成功之后页面会跳转到酒店查询页面中。

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:在酒店query页面中,获取酒店的名称
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class hotelquery(Page):

	'''
	酒店query界面
	'''
	hotel_query_url = '/hotel/query'
	# 酒店名称的定位
	wineshop_name_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[1]')

	# 获取酒店名称
	def get_hotel_name(self):
		# self.open()
		wineshop_name = self.find_element(*self.wineshop_name_loc).text
		return wineshop_name

agentPage.py

    主要是酒店的代理商和代理商query页面封装

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:代理商新增页面
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class agent(Page):

	'''
	代理商新增页面
	'''
	url = '/agent/toEdit'
	# 代理商编号的定位
	agent_code_loc = (By.NAME, 'agent.code')
	# 代理商名称的定位
	agent_name_loc = (By.NAME, 'agent.name')
	# 代理商联系电话的定位
	agent_phone_loc = (By.NAME, 'agent.phone')
	# 代理商邮箱的定位
	agent_email_loc = (By.NAME, 'agent.email')
	# 代理商地址定位
	agent_address_loc = (By.NAME, 'agent.address')
	# 代理商提成率的定位
	agent_payment_loc = (By.NAME, 'agent.payment_rate')
		# 代理商折扣率的定位
	agent_discount_loc = (By.NAME, 'agent.discount_rate')

	# 代理商添加页面中的保存按钮的定位
	agent_button_loc = (By.XPATH, 'html/body/div[1]/form/div[3]/div/button')

	# 代理商编号
	def get_agent_code(self):
		return self.find_element(*self.agent_code_loc).get_attribute("value")

	# 代理商名称
	def add_agent_name(self, agent_name):
		self.find_element(*self.agent_name_loc).clear()
		self.find_element(*self.agent_name_loc).send_keys(agent_name)

	# 代理商联系电话
	def add_agent_phone(self, agent_phone):
		self.find_element(*self.agent_phone_loc).clear()
		self.find_element(*self.agent_phone_loc).send_keys(agent_phone)

	# 代理商邮箱
	def add_agent_email(self, agent_email):
		self.find_element(*self.agent_email_loc).clear()
		self.find_element(*self.agent_email_loc).send_keys(agent_email)

	# 代理商地址
	def add_agent_address(self, agent_address):
		self.find_element(*self.agent_address_loc).clear()
		self.find_element(*self.agent_address_loc).send_keys(agent_address)

	# 代理商提成率
	def add_agent_payment(self, agent_payment):
		self.find_element(*self.agent_payment_loc).clear()
		self.find_element(*self.agent_payment_loc).send_keys(agent_payment)

	# 代理商折扣率
	def add_agent_discount(self, agent_discount):
		self.find_element(*self.agent_discount_loc).clear()
		self.find_element(*self.agent_discount_loc).send_keys(agent_discount)


	# 代理商添加页面保存按钮
	def add_agent_button(self):
		self.find_element(*self.agent_button_loc).click()

	# 统一代理商添加入口
	def add_agent(self, agent_name="test", agent_phone="15245898745",
		agent_email="23090932@qq.com", agent_address="weosdsdf",
		agent_payment="20", agent_discount="50"):
		# 获取代理商添加页面
		self.open()
		# self.script(self.base_url + self.url)
		# 获取添加页面的代理商编码
		# agent_code = self.find_element(*self.agent_code_loc).get_attribute("value")
		# return agent_code
		self.add_agent_name(agent_name)
		sleep(3)
		self.add_agent_phone(agent_phone)
		self.add_agent_email(agent_email)
		sleep(3)
		self.add_agent_address(agent_address)
		self.add_agent_payment(agent_payment)
		self.add_agent_discount(agent_discount)
		sleep(3)
		self.add_agent_button()
		sleep(3)
		# 代理商编号

agentQueryPage.py

    代理商query页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:在代理商query页面中,获取代理商的名称
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class agentquery(Page):

   '''
    代理商query页面
   '''
 agent_query_url = '/agent/query'
 # 代理商编号的定位
 factor_code_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[1]')
 # 代理商名称定位
 factor_name_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[2]')

  # 获取代理商编码
 def get_factor_code(self):
    # self.open()
     factor_code = self.find_element(*self.factor_code_loc).text
     return factor_code

  # 获取代理商名称
 def get_factor_name(self):
     factor_name = self.find_element(*self.factor_name_loc).text
     return factor_name

productcategoryPage.py

产品类型页面page类

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:产品类型新增页面
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class productcategory(Page):
		'''
		产品类型添加页面
		'''
		url = "/product/toEditCategory"
		# 产品类型名称定位
		productcategory_name_loc = (By.NAME, 'productCategory.name')
		# 产品类型描述定位
		productcategory_des_loc = (By.NAME, 'productCategory.description')
		# 产品类型有效性定位
		productcategory_enable_loc = (By.NAME,
                              'html/body/div[1]/form/div[2]/div[3]/div/div[1]/i')
		# 产品类型禁用定位
		productcategory_disable_loc = (By.NAME,
                               'html/body/div[1]/form/div[2]/div[3]/div/div[2]/i')
		# 产品类型保存按钮
		productcategory_button_loc = (By.XPATH, 'html/body/div[1]/form/div[3]/div/button')

		# 产品名称
		def add_productcategory_name(self, productcategory_name):
    		self.find_element(*self.productcategory_name_loc).clear()
    		self.find_element(*self.productcategory_name_loc).send_keys(productcategory_name)

	# 产品类型
		def add_productcategory_des(self, productcategory_des):
    		self.find_element(*self.productcategory_des_loc).clear()
    		self.find_element(*self.productcategory_des_loc).send_keys(productcategory_des)

	# 产品类型有效性
		def add_productcategory_en(self):
    		self.find_element(*self.productcategory_enable_loc).click()

	# 产品类型无效
		def add_productcategory_dis(self):
    		self.find_element(*self.productcategory_disable_loc).click()

		# 产品类型保存按钮
		def add_productcategory_button(self):
    		self.find_element(*self.productcategory_button_loc).click()

	# 产品类型统一添加入口
		def add_productcategory(self, productcategory_name = 'prodoctcategory001',
                        productcategory_des = 'productcategorydes001'):
    		self.open()
    		self.add_productcategory_name(productcategory_name)
    		self.add_productcategory_des(productcategory_des)
    		self.add_productcategory_button()

productcategoryQueryPage.py

主要是产品类型查询页面元素

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:在产品类型query页面中,获取酒店的名称
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class productcategoryquery(Page):

		'''
		产品类型query界面
		'''
		productcategory_query_url = '/product/queryCategory'
		# 产品类型名称的定位
		category_name_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[1]')
		# 产品类型状态定位
		category_state_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[3]')

	# 产品类型名称
	def get_category_name(self):
		category_name = self.find_element(*self.category_name_loc).text
    		return category_name

	# 产品类型状态
		def get_category_state(self):
    		# self.open()
    		category_state = self.find_element(*self.category_name_loc).text
    		return category_state

pageinfoPage.py

产品信息页面page

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:产品信息新增页面
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep
import os

class productinfo(Page):
		'''
		产品信息添加页面
		'''
		url = "/product/toEdit"
		# 未实现
		filepath = "C:\Users\Admin\PycharmProjects\untitled1\mztestpro\driver\upload1.exe"

	#产品名称定位
		product_name_loc = (By.NAME, 'product.name')
		# 产品类型定位
		product_category_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[2]/div[1]/div/div/div/input')
		# 产品类型选择按钮定位
		productinfo_category_loc = (By.XPATH,
                              'html/body/div[1]/form/div[2]/div[2]/div[1]/div/div/dl/dd[2]')
		# 产品信息租赁单价定位
		product_rent_loc = (By.NAME, 'product.rent_price')
		# 产品销售单价定位
		product_sale_loc = (By.NAME, 'product.sales_price')
		# 是否热销产品,此处默认选择否

		#产品描述定位
		product_des_loc = (By.NAME, 'product.description')
		# 产品出口绑定编号,是-[默认是否]
		product_bind_loc =(By.XPATH, 'html/body/div[1]/form/div[2]/div[4]/div/div[1]/i')
		# 产品上传图片
		'''
        	# 针对input格式的文件或图片上传可以使用下面此方法进行操作
        	# 导航图的定位和上传
        	#self.driver.find_element_by_id("upload1").clear()
        	self.driver.find_element_by_id("upload1").send_keys("C:\Users\Admin\PycharmProjects\untitled1\hotel\pic\sun.jpg")
        	time.sleep(3)
        	# 产品详情图的定位和上传
        	self.driver.find_element_by_id("test2").send_keys(
            "C:\Users\Admin\PycharmProjects\untitled1\hotel\pic\sun.jpg")
        	time.sleep(5)
        	# 第二张产品详情图的上传
        	self.driver.find_element_by_id("test2").send_keys(
            "C:\Users\Admin\PycharmProjects\untitled1\hotel\pic\sun.jpg")
        	time.sleep(5)
        	# 第三张产品详情图的上传
        	self.driver.find_element_by_id("test2").send_keys(
            "C:\Users\Admin\PycharmProjects\untitled1\hotel\pic\sun.jpg")
        	time.sleep(5)
        	# 第四张产品详情图的上传
        	self.driver.find_element_by_id("test2").send_keys(
            "C:\Users\Admin\PycharmProjects\untitled1\hotel\pic\sun.jpg")
        	time.sleep(5)

        	#产品导航多图上传
        	self.driver.find_element_by_id("test3").send_keys(
            "C:\Users\Admin\PycharmProjects\untitled1\hotel\pic\sun.jpg")
        	time.sleep(5)
        	# 第二张产品导航图的上传
        	self.driver.find_element_by_id("test3").send_keys(
            "C:\Users\Admin\PycharmProjects\untitled1\hotel\pic\sun.jpg")
        	time.sleep(5)
        	# 第三张产品导航图的上传
        	self.driver.find_element_by_id("test3").send_keys(
            "C:\Users\Admin\PycharmProjects\untitled1\hotel\pic\sun.jpg")
        	#第四张产品导航图的上传
        	self.driver.find_element_by_id("test3").send_keys(
            "C:\Users\Admin\PycharmProjects\untitled1\hotel\pic\sun.jpg")
        	time.sleep(5)
        '''

		# 第一步首先定位上传导航图的按钮
		product_upload1_loc = (By.ID, 'upload1')
		# 产品详情图片上传
		product_upload2_loc = (By.ID, 'test2')
		#产品导航多图上传
		product_upload3_loc = (By.ID, 'test3')

		# 产品信息保存按钮
		product_button_loc = (By.XPATH, 'html/body/div[1]/form/div[3]/div/button[1]')

		# 产品名称
		def add_product_name(self, product_name):
    		self.find_element(*self.product_name_loc).clear()
    		self.find_element(*self.product_name_loc).send_keys(product_name)

	# 产品类型
		def add_product_category(self):
   			self.find_element(*self.product_category_loc).click()

	# 产品信息中产品类型下拉选择
		def add_product_select(self):
    		self.find_element(*self.productinfo_category_loc).click()

	# 租赁单价
		def add_product_rent(self, product_rent):
    		self.find_element(*self.product_rent_loc).clear()
    		self.find_element(*self.product_rent_loc).send_keys(product_rent)

	# 销售单价
		def add_product_sale(self, product_sale):
    		self.find_element(*self.product_sale_loc).clear()
    		self.find_element(*self.product_sale_loc).send_keys(product_sale)

	# 是否热销产品下拉选择--未完成

		# 产品描述
		def add_product_des(self, product_des):
    		self.find_element(*self.product_des_loc).clear()
    		self.find_element(*self.product_des_loc).send_keys(product_des)

	# 添加产品绑定设备
		def add_product_bind(self):
    		self.find_element(*self.product_bind_loc).click()

	# 使用auotIt进行导航图上传
		def add_product_pic1(self):
    		self.find_element(*self.product_upload1_loc).click()
    	# 调用upload.exe文件
    		os.system("C:\Users\Admin\PycharmProjects\untitled1\mztestpro\driver\upload1.exe")

	# 使用autoIt进行产品详情图上传
		def add_product_pic2(self):
    		self.find_element(*self.product_upload2_loc).click()
    		# 调用upload.exe文件
    		os.system("C:\Users\Admin\PycharmProjects\untitled1\mztestpro\driver\upload1.exe")

	# 使用autoIt进行导航多图上传
		def add_product_pic3(self):
    		self.find_element(*self.product_upload3_loc).click()
    		# 调用upload.exe文件
    		os.system("C:\Users\Admin\PycharmProjects\untitled1\mztestpro\driver\upload1.exe")

	# 产品类型保存按钮
		def add_product_button(self):
    		self.find_element(*self.product_button_loc).click()

	# 产品类型统一添加入口
		def add_product(self, product_name='product001',product_rent='10',
                product_sale='20', product_des='productinofdes'):
    		self.open()
    		# 产品名称输入
    		self.add_product_name(product_name)
    		# 点击产品类型
    		self.add_product_category()
    		# 产品类型下拉框选择
    		self.add_product_select()
    		# 产品租赁单价输入
    		self.add_product_rent(product_rent)
    		# 产品销售单价输入
    		self.add_product_sale(product_sale)
    		# 产品热销产品默认

    		#产品描述输入
    		self.add_product_des(product_des)
    		# 产品图片上传
    		self.add_product_pic1()
    		#详情图片上传
    		# sleep(3)
    		self.add_product_pic2()
    		# sleep(4)
    		self.add_product_pic3()
    		sleep(3)
    		self.add_product_pic3()
    		# 产品保存按钮
    		self.add_product_button()

productinfoQueryPage.py

产品信息页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:在产品类型query页面中,获取酒店的名称
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class productinfoquery(Page):

		'''
		产品信息query界面
		'''
		productinfo_query_url = '/product/query'
		# 产品信息名称的定位
		productinfo_name_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[1]')
		# 产品信息编码定位
		productinfo_code_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[2]')

	# 产品信息名称
		def get_productinfo_name(self):
    		productinfo_name = self.find_element(*self.productinfo_name_loc).text
    		return productinfo_name

	# 产品类型状态
		def get_productinfo_code(self):
    		# self.open()
    		productinfo_code = self.find_element(*self.productinfo_code_loc).text
    		return productinfo_code

warehouse.py

仓库信息页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:仓库新增页面
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class warehouse(Page):

		'''
		仓库新增页面
		'''
		url = '/warehouse/toEdit'
		# 仓库编号的定位
		warehouse_code_loc = (By.NAME, 'house.code')
		# 仓库名称的定位
		warehouse_name_loc = (By.NAME, 'house.name')
		# 仓库省市县定位
		warehouse_province_loc = (By.ID, 'province')
		warehouse_city_loc = (By.ID, 'city')
		warehouse_region_loc = (By.ID, 'region')
		warehouse_street_loc = (By.ID, 'street')
		# 仓库地址定位
		warehouse_address_loc = (By.ID, 'detail')
		# 仓库联系人的定位
		warehouse_contacts_loc = (By.ID, 'contacts')
		# 仓库联系电话的定位
		warehouse_phone_loc = (By.ID, 'phone')

	 # 仓库状态的定位
		warehouse_statue_loc = (By.XPATH, ".//*[@id='app']/form/div[2]/div[1]/div[3]/div/div/input")
		# 仓库描述信息定位
		warehouse_des_loc = (By.NAME, 'house.description')
		# 仓库添加页面中的保存按钮的定位
		warehouse_button_loc = (By.XPATH, ".//*[@id='app']/form/div[3]/div/button")

		# 仓库编号
		def get_warehouse_code(self):
    		return self.find_element(*self.warehouse_code_loc).get_attribute("value")

	# 仓库名称
		def add_warehouse_name(self, warehouse_name):
   	 		self.find_element(*self.warehouse_name_loc).clear()
    		self.find_element(*self.warehouse_name_loc).send_keys(warehouse_name)

	# 仓库省市区信息
		def add_warehouse_province(self,warehouse_province):
    		self.find_element(*self.warehouse_province_loc).clear()
    		self.find_element(*self.warehouse_province_loc).send_keys(warehouse_province)

		def add_warehouse_city(self,warehouse_city):
    		self.find_element(*self.warehouse_city_loc).clear()
    		self.find_element(*self.warehouse_city_loc).send_keys(warehouse_city)

		def add_warehouse_region(self,warehouse_region):
    		self.find_element(*self.warehouse_region_loc).clear()
    		self.find_element(*self.warehouse_region_loc).send_keys(warehouse_region)

		def add_warehouse_street(self, warehouse_street):
    		self.find_element(*self.warehouse_street_loc).clear()
    		self.find_element(*self.warehouse_street_loc).send_keys(warehouse_street)

    		# 仓库详细地址
		def add_warehouse_address(self, warehouse_address):
    		self.find_element(*self.warehouse_address_loc).clear()
    		self.find_element(*self.warehouse_address_loc).send_keys(warehouse_address)

    		# 仓库联系人
		def add_warehouse_contacts(self, warehouse_contacts):
    		self.find_element(*self.warehouse_contacts_loc).clear()
    		self.find_element(*self.warehouse_contacts_loc).send_keys(warehouse_contacts)

	# 仓库联系电话
		def add_warehouse_phone(self, warehouse_phone):
    		self.find_element(*self.warehouse_phone_loc).clear()
    		self.find_element(*self.warehouse_phone_loc).send_keys(warehouse_phone)

	# 仓库描述信息
		def add_warehouse_des(self, warehouse_des):
    		self.find_element(*self.warehouse_des_loc).clear()
    		self.find_element(*self.warehouse_des_loc).send_keys(warehouse_des)

	# 仓库添加页面保存按钮
		def add_warehouse_button(self):
    		self.find_element(*self.warehouse_button_loc).click()

		# 统一仓库添加入口
		def add_warehouse(self, warehouse_name="warehouse001",
                  warehouse_province="15245898745", warehouse_city='city15245898745',
                  warehouse_region='region15245898745', warehouse_street='street15245898745',
                  warehouse_address="23090932address", warehouse_contacts="warehousecontacts",
                  warehouse_phone='15245898745',warehouse_des='deswarehousecontacts'):
    		# 获取仓库添加页面
    		self.open()
    		# self.script(self.base_url + self.url)
    		# 获取添加页面的仓库编码
    		# agent_code = self.find_element(*self.agent_code_loc).get_attribute("value")
    		# return agent_code
    		self.add_warehouse_name(warehouse_name)
    		sleep(3)
    		self.add_warehouse_province(warehouse_province)
    		self.add_warehouse_city(warehouse_city)
    		sleep(3)
    		self.add_warehouse_region(warehouse_region)
    		self.add_warehouse_street(warehouse_street)
    		self.add_warehouse_address(warehouse_address)
    		self.add_warehouse_contacts(warehouse_contacts)
    		self.add_warehouse_phone(warehouse_phone)
    		self.add_warehouse_des(warehouse_des)
    		sleep(3)
    		self.add_warehouse_button()
    		sleep(3)

warehouseQueryPage.py

仓库查询页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:在仓库query页面中,获取仓库的名称
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class warehousequery(Page):

		'''
		仓库query页面
		'''
		agent_query_url = '/warehouse/query'
		# 仓库编号的定位
		depot_code_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[1]')
		# 仓库名称定位
		depot_name_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[2]')

	# 获取仓库编码
		def get_depot_code(self):
		# self.open()
    		depot_code = self.find_element(*self.depot_code_loc).text
    		return depot_code

	# 获取仓库名称
		def get_depot_name(self):
    		depot_name = self.find_element(*self.depot_name_loc).text
    		return depot_name

employeePage.py

职员添加页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:职员新增页面
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class employee(Page):

		'''
		职员新增页面
		'''
		url = '/employee/toEdit'
		# 职员名称的定位
		employee_code_loc = (By.NAME, 'employees.employees_name')
		# 职员工号的定位
		employee_name_loc = (By.NAME, 'employees.job_number')
		# 职员性别为男
		employee_man_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[3]/div/div[1]/i')
		# 职员性别为女
		employee_wom_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[3]/div/div[2]/i')
		# 职员性别未知
		employee_sec_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[3]/div/div[3]/i')
		# 职员身份证号码定位
		employee_id_loc = (By.NAME, 'employees.card')
		# 职员联系电话的定位
		employee_phone_loc = (By.NAME, 'employees.phone')
		# 职员婚姻定位,这里选择默认

		# 职员文化程度,这里选择默认

		# 职员入职时间定位
		employee_date_loc = (By.ID, 'date')
		# 职员邮箱定位
		employee_email_loc = (By.NAME, 'employees.email')

		# 职员添加页面中的保存按钮的定位
		employee_button_loc = (By.XPATH, 'html/body/div[1]/form/div[3]/div/button[1]')

		# 职员名称
		def add_employee_name(self, employee_name):
   	 		self.find_element(*self.employee_name_loc).clear()
    		self.find_element(*self.employee_name_loc).send_keys(employee_name)

	# 职员工号
		def add_employee_code(self,employee_code):
    		self.find_element(*self.employee_code_loc).clear()
    		self.find_element(*self.employee_code_loc).send_keys(employee_code)

	# 职工身份证号码
		def add_employee_id(self, employee_id):
    		self.find_element(*self.employee_id_loc).clear()
    		self.find_element(*self.employee_id_loc).send_keys(employee_id)

	# 职员联系电话
		def add_employee_phone(self, employee_phone):
    		self.find_element(*self.employee_phone_loc).clear()
    		self.find_element(*self.employee_phone_loc).send_keys(employee_phone)

    	# 职员入职时间
		def add_employee_date(self, employee_date):
    		self.find_element(*self.employee_date_loc).clear()
    		self.find_element(*self.employee_date_loc).send_keys(employee_date)

    		# 职员邮箱
		def add_employee_email(self, employee_email):
    		self.find_element(*self.employee_email_loc).clear()
    		self.find_element(*self.employee_email_loc).send_keys(employee_email)

	# 职员添加页面保存按钮
		def add_employee_button(self):
    		self.find_element(*self.employee_button_loc).click()

		# 统一职员添加入口
		def add_employee(self, employee_name="employee001", employee_code='employee001',
                 employee_id='500106200406101329', employee_phone="15645898745",
                 employee_date='2018-01-11', employee_email="23090932@qq.com" ):
    		# 获取职员添加页面
    		self.open()
    		# self.script(self.base_url + self.url)
    		# 获取添加页面的职员编码
    		# employee_code = self.find_element(*self.employee_code_loc).get_attribute("value")
    		# return employee_code
    		self.add_employee_name(employee_name)
    		self.add_employee_code(employee_code)
    		sleep(3)
    		self.add_employee_id(employee_id)
    		self.add_employee_phone(employee_phone)
    		self.add_employee_date(employee_date)
    		self.add_employee_email(employee_email)
    		sleep(3)
    		self.add_employee_button()
    		sleep(3)

employeeQueryPage.py

职员查询页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:在职员query页面中,获取代理商的名称
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class employeequery(Page):

		'''
		职员query页面
		'''
		employee_query_url = '/employee/query'
		# 职员工号的定位
		staff_code_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[2]')
		# 职员名称定位
		staff_name_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[1]')

	# 获取职员编码
		def get_staff_code(self):
    		# self.open()
    		staff_code = self.find_element(*self.staff_code_loc).text
    		return staff_code

	# 获取职员名称
		def get_staff_name(self):
    		staff_name = self.find_element(*self.staff_name_loc).text
    		return staff_name

rolePage.py

角色新增页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:角色新增页面
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class role(Page):

		'''
		角色新增页面
		'''
		url = '/role/toEdit'
		# 角色名称的定位
		role_name_loc = (By.NAME, 'role.role_name')
		# 角色描述定位
		role_des_loc = (By.NAME, 'role.role_desc')
		# 角色用户定位
		role_select_loc = (By.XPATH, ".//*[@id='b']/option[1]")
		# 选中角色的用户
		role_selected_loc = (By.NAME, 'bb')
		# 职员添加页面中的保存按钮的定位
		role_button_loc = (By.XPATH, 'html/body/div[1]/form/div[7]/div/button[1]')

		# 角色名称
		def add_role_name(self, role_name):
    		self.find_element(*self.role_name_loc).clear()
    		self.find_element(*self.role_name_loc).send_keys(role_name)

	# 角色描述
		def add_role_desc(self, role_desc):
    		self.find_element(*self.role_des_loc).clear()
    		self.find_element(*self.role_des_loc).send_keys(role_desc)

	# 角色用户操作
		def add_role_select(self):
    		self.find_element(*self.role_select_loc).click()
    		self.find_element(*self.role_selected_loc).click()

	# 角色添加页面保存按钮
		def add_role_button(self):
    		self.find_element(*self.role_button_loc).click()

		# 统一角色添加入口
		def add_role(self, role_name="role001", role_desc='role001'):
    		# 获取角色添加页面
    		self.open()
    		# self.script(self.base_url + self.url)
    		# 获取添加页面的职员编码
    		# employee_code = self.find_element(*self.employee_code_loc).get_attribute("value")
    		# return employee_code
    		self.add_role_name(role_name)
    		self.add_role_desc(role_desc)
    		self.add_role_select()
    		sleep(3)
    		self.add_role_button()
    		sleep(3)

roleQueryPage.py

角色查询页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:在角色query页面中,获取角色的名称
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class rolequery(Page):

		'''
		角色query页面
		'''
		role_query_url = '/role/query'
		# 角色名称的定位
		part_name_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[1]')

	# 获取职员编码
		def get_part_name(self):
    		# self.open()
    		part_name = self.find_element(*self.part_name_loc).text
    		return part_name

hotelpricePage.py

酒店单价管理页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:创建登录页面对象,对用户登录页面上的用户名/密码输入框、登录按钮和
提示信息等元素的定位进行封装。除此之外,还创建user_login()方法作为系统统
一登录的入口。关于对操作步骤的封装可以放在Page Object当中,
也可以放在测试用例当中,这个主要根据具体的需求来衡量。这里之所以要放在
Page Object当中,主要考虑到还会有其他的测试用例调用到该登录方法。
为username 和 password 入参数设置了默认值是为了方便其他用例在调用
user_login()时不用再传递登录用户信息,因为该系统大多用例的执行使用该账号
即可,同时也方便了在账号失效时的修改。
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class hotelprice(Page):

	'''
	酒店单价添加界面
	'''
	url = '/hUnitPrice/toEdit'
	# 酒店单价新增中的酒店名称
	hotelprice_name_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[1]/div/div/div/div/input')
	# 酒店名称选择
	hotelprice_name_selected_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[1]/div/div/div/dl/dd[2]')
	# 酒店单价中的产品信息
	hotelprice_product_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[2]/div/div/div/div/input')
	# 酒店产品选择信息
	hotelprice_product_selected_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[2]/div/div/div/dl/dd[2]')
	# 酒店单价中的最小采购数量
	hotelprice_min_loc = (By.NAME, 'hotelUnitPrice.min_qty')
	# 酒店单价中的最大采购数量
	hotelprice_max_loc = (By.NAME, 'hotelUnitPrice.max_qty')
	# 酒店单价管理中的单价信息
	hotelprice_price_loc = (By.NAME, 'hotelUnitPrice.price')

	# 酒店单价添加页面中的保存按钮的定位
	hotelprice_button_loc = (By.XPATH, 'html/body/div[1]/form/div[3]/div/button[1]')

	# 酒店单价添加的酒店名称
	def add_hotelprice_name(self):
		self.find_element(*self.hotelprice_name_loc).click()
		self.find_element(*self.hotelprice_name_selected_loc).click()


	# 酒店单价中添加的产品信息
	def add_hotelprice_product(self):
		self.find_element(*self.hotelprice_product_loc).click()
		self.find_element(*self.hotelprice_product_selected_loc).click()

	# 酒店单价中的最小采购数量
	def add_hotelprice_min(self, hotelprice_min):
		self.find_element(*self.hotelprice_min_loc).clear()
		self.find_element(*self.hotelprice_min_loc).send_keys(hotelprice_min)

	# 酒店单价中的最大采购数量
	def add_hotelprice_max(self, hotelprice_max):
		self.find_element(*self.hotelprice_max_loc).clear()
		self.find_element(*self.hotelprice_max_loc).send_keys(hotelprice_max)

	# 酒店单价中的单价信息
	def add_hotelprice_price(self, hotelprice_price):
		self.find_element(*self.hotelprice_price_loc).clear()
		self.find_element(*self.hotelprice_price_loc).send_keys(hotelprice_price)

	# # 酒店页面js打开方法
	#     def get_hotel_url(self, url):
	#         self.script(self.base_url+url)

	# 酒店单价添加页面保存按钮
	def add_hotelprice_button(self):
		self.find_element(*self.hotelprice_button_loc).click()

	# 统一酒店添加入口
	def add_hotelprice(self, hotelprice_min="1", hotelprice_max="1000",
              hotelprice_price="1000"):
		# 获取酒店添加页面
		self.open()
		# self.script(self.base_url + self.url)
		# 选择酒店单价管理中的酒店名称
		self.add_hotelprice_name()
		# 选择酒店单价管理中高端酒店产品
		self.add_hotelprice_product()
		# 输入采购的数量和单价信息
		self.add_hotelprice_min(hotelprice_min)
		sleep(3)
		self.add_hotelprice_max(hotelprice_max)
		self.add_hotelprice_price(hotelprice_price)
		sleep(3)
		self.add_hotelprice_button()
		sleep(3)

		# 错误提示信息

hotelpirceQueryPage.py

酒店单价管理查询页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:在酒店query页面中,获取酒店的名称
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class hotelpricequery(Page):

	'''
	酒店query界面
	'''
	hotel_query_url = '/hUnitPrice/query'
		# 酒店单价产品名称的定位
		hotelproduct_name_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[1]')
		# 酒店单价酒店名称定位
		hotelproduct_hotel_name_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[3]')

	# 获取酒店产品名称
		def get_hotelprice_name(self):
    		# self.open()
    		hotelproduct_name = self.find_element(*self.hotelproduct_name_loc).text
    		return hotelproduct_name

			# 获取酒店名称
		def get_hotelprice_hotel_name(self):
    		hotelproduct_hotel_name = self.find_element(*self.hotelproduct_hotel_name_loc).text
    		return hotelproduct_hotel_name

agentpricePage.py

代理商单价页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:创建登录页面对象,对用户登录页面上的用户名/密码输入框、登录按钮和
提示信息等元素的定位进行封装。除此之外,还创建user_login()方法作为系统统
一登录的入口。关于对操作步骤的封装可以放在Page Object当中,
也可以放在测试用例当中,这个主要根据具体的需求来衡量。这里之所以要放在
Page Object当中,主要考虑到还会有其他的测试用例调用到该登录方法。
为username 和 password 入参数设置了默认值是为了方便其他用例在调用
user_login()时不用再传递登录用户信息,因为该系统大多用例的执行使用该账号
即可,同时也方便了在账号失效时的修改。
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class agentprice(Page):

	'''
	代理商单价添加界面
	'''
	url = '/aUnitPrice/toEdit'
		# 代理商单价新增中的代理商名称
		agentprice_name_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[1]/div/div/div/div/input')
		# 代理商名称选择
		agentprice_name_selected_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[1]/div/div/div/dl/dd[2]')
		# 代理商单价中的产品信息
		agentprice_product_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[2]/div/div/div/div/input')
		# 代理商产品选择信息
		agentprice_product_selected_loc = (By.XPATH, 'html/body/div[1]/form/div[2]/div[2]/div/div/div/dl/dd[2]')
		# 代理商单价中的最小采购数量
		agentprice_min_loc = (By.NAME, 'agentUnitPrice.min_qty')
		# 代理商单价中的最大采购数量
		agentprice_max_loc = (By.NAME, 'agentUnitPrice.max_qty')
		# 代理商单价管理中的单价信息
		agentprice_price_loc = (By.ID, 'price')

		# 代理商单价添加页面中的保存按钮的定位
		agentprice_button_loc = (By.XPATH, 'html/body/div[1]/form/div[3]/div/button[1]')

	# 代理商单价添加的酒店名称
		def add_agentprice_name(self):
    		self.find_element(*self.agentprice_name_loc).click()
    		self.find_element(*self.agentprice_name_selected_loc).click()


	# 代理商单价中添加的产品信息
		def add_agentprice_product(self):
    		self.find_element(*self.agentprice_product_loc).click()
    		self.find_element(*self.agentprice_product_selected_loc).click()

	# 代理商单价中的最小采购数量
		def add_agentprice_min(self, agentprice_min):
    		self.find_element(*self.agentprice_min_loc).clear()
    		self.find_element(*self.agentprice_min_loc).send_keys(agentprice_min)

	# 代理商单价中的最大采购数量
		def add_agentprice_max(self, agentprice_max):
    		self.find_element(*self.agentprice_max_loc).clear()
    		self.find_element(*self.agentprice_max_loc).send_keys(agentprice_max)

	# 代理商单价中的单价信息
		def add_agentprice_price(self, agentprice_price):
    		self.find_element(*self.agentprice_price_loc).clear()
    		self.find_element(*self.agentprice_price_loc).send_keys(agentprice_price)

	# # 酒店页面js打开方法
	#     def get_hotel_url(self, url):
	#         self.script(self.base_url+url)

	# 代理商单价添加页面保存按钮
		def add_agentprice_button(self):
    		self.find_element(*self.agentprice_button_loc).click()

		# 统一代理商添加入口
		def add_agentprice(self, agentprice_min="1", agentprice_max="1000", agentprice_price="1000"):
    		# 获取代理商添加页面
    		self.open()
    		# self.script(self.base_url + self.url)
    		# 选择代理商单价管理中的酒店名称
    		self.add_agentprice_name()
    		# 选择代理商单价管理中高端酒店产品
    		self.add_agentprice_product()

    		# 输入采购的数量和单价信息
    		self.add_agentprice_min(agentprice_min)
    		sleep(3)
    		self.add_agentprice_max(agentprice_max)
    		self.add_agentprice_price(agentprice_price)
    		sleep(3)
    		self.add_agentprice_button()
    		sleep(3)

			# 错误提示信息

agentpriceQueryPage.py

代理商单价查询页面

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:在酒店query页面中,获取酒店的名称
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class agentpricequery(Page):

		'''
		代理商单价query界面
		'''
		agent_query_url = '/aUnitPrice/query'
		# 代理商单价产品名称的定位
		agentproduct_name_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[1]')
		# 代理商单价酒店名称定位
		agentproduct_agent_name_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[3]')

	# 获取代理商单价产品名称
		def get_agentprice_name(self):
    		# self.open()
    		agentproduct_name = self.find_element(*self.agentproduct_name_loc).text
    		return agentproduct_name

		# 获取代理商单价名称
		def get_agentprice_agent_name(self):
    		agentproduct_agent_name = self.find_element(*self.agentproduct_agent_name_loc).text
    		return agentproduct_agent_name

userPage.py

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:用户新增页面
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from base import Page
from time import sleep

class user(Page):

		'''
		职员用户页面
		'''
		url = '/user/toEdit'
		# 用户工号的定位
		user_code_loc = (By.ID, 'empIds')
		# 用户名的定位,默认修改
		user_name_loc = (By.ID, 'username')
		# 用户密码定位
		password = (By.ID, 'password')
		# 用户确认密码定位
		ppassword = (By.ID, 'ppassword')

		# 用户角色定位
		user_role_loc = (By.XPATH, ".//*[@id='b']/option[1]")
		# 用户角色选择定位
		user_select_loc = (By.NAME, 'bb')

		# 用户添加页面中的保存按钮的定位
		user_button_loc = (By.XPATH, 'html/body/div[1]/form/div[7]/div/button[1]')

		# 用户名称和工号
		def add_user_code(self):
    		Select(self.find_element(*self.user_code_loc)).select_by_index(1)

		# 用户名称
		def add_user_name(self, user_name):
    		self.find_element(*self.user_name_loc).clear()
    		self.find_element(*self.user_name_loc).send_keys(user_name)

		#用户密码
		def add_user_password(self, password):
    		self.find_element(*self.password).clear()
    		self.find_element(*self.password).send_keys(password)

		# 用户密码
		def add_user_ppassword(self, password):
    		self.find_element(*self.ppassword).clear()
    		self.find_element(*self.ppassword).send_keys(password)

    		# 用户角色选择操作
		def add_user_selected(self):
    		self.find_element(*self.user_role_loc).click()
    		self.find_element(*self.user_select_loc).click()

	# 用户添加页面保存按钮
		def add_user_button(self):
    		self.find_element(*self.user_button_loc).click()

		# 统一用户添加入口
		def add_user(self, username='sz001', password='123456', ppassword='123456'):
    		# 获取用户添加页面
    		self.open()
    		# self.script(self.base_url + self.url)
    		# 获取添加页面的职员编码
    		# employee_code = self.find_element(*self.employee_code_loc).get_attribute("value")
    		# return employee_code
    		self.add_user_code()
    		self.add_user_name(username)
    		self.add_user_password(password)
    		sleep(3)
    		self.add_user_ppassword(ppassword)
    		self.add_user_selected()
    		self.add_user_button()
    		sleep(3)

userQueryPage.py

# -*-coding:utf-8-*-
# _author_ = "janehost"
"""
思路:在职员query页面中,获取代理商的名称
"""
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from base import Page
from time import sleep

class userquery(Page):

		'''
		用户query页面
		'''
		user_query_url = '/user/query'
		# 用户名称的定位
		customer_loc = (By.XPATH, 'html/body/div[1]/table/tbody/tr[1]/td[1]')

	# 获取用户名称
		def get_customer(self):
    		# self.open()
    		customer = self.find_element(*self.customer_loc).text
    		return customer

四、编写测试用例

现在开始编写测试用程序,因为前面已经做好了基础工作,此时测试用例的编写将会简单的许多,更能集中精力考虑用例的设计和事项。

创建BBS登录类:

...\mztestpro\bbs\test_case\login_sta.py

此处需要注意文件名的创建。例如,假设登录页的对象命名为loginPage.py,那么关于测试登录的用例文件应该命名为login_sta.py,这样方便后期用例报错时问题跟踪。尽量把一个页面上的元素定位封装到一个“Page.py”文件中,把针对这个页面的测试用例集中到一个“_sta.py”文件中

login_sta.py

登录测试用例信息test_case\login_sta.py,代码如下

# -*-coding:utf-8-*-
# _author_ = "janehost"
from time import sleep
import unittest, random, sys
from models import myunit, function
from page_obj.loginPage import login
sys.path.append("./models")
sys.path.append("./page_obj")
reload(sys)
sys.setdefaultencoding('utf8')
class loginTest(myunit.MyTest):

	'''
	测试用户登录
	'''

	def user_login_verify(self, username="", password=""):
		login(self.driver).user_login(username, password)

	def test_login1(self):
	'''用户名、密码为空登录'''
		self.user_login_verify()
		po = login(self.driver)
		self.assertEqual(po.login_error_hint(), '用户名或密码不能为空')
		function.insert_img(self.driver, "user_pawd_empty.jpg")

	def test_login2(self):
	'''用户名正确,密码为空登录验证'''
		self.user_login_verify(username="ces")
		po = login(self.driver)
		self.assertEqual(po.login_error_hint(), "用户名或密码不能为空")
		function.insert_img(self.driver,"pawd_empty.jpg")

	def test_login3(self):
	'''用户名为空,密码正确'''
		self.user_login_verify(password="12334ddf")
		po = login(self.driver)
		self.assertEqual(po.login_error_hint(),"用户名或密码不能为空")
		function.insert_img(self.driver, "user_empty.jpg")

	def test_login4(self):
	'''用户名和密码不匹配'''
		character = random.choice('abcdefghijklmnopqrstuvwxyz')
		username = "sdw" + character
		self.user_login_verify(username=username, password="2sdfd")
		po = login(self.driver)
		self.assertEqual(po.login_error_hint(), "用户名或密码错误")
		function.insert_img(self.driver, "user_pass_error.jpg")

	def test_login5(self):
	'''用户名、密码正确'''
		self.user_login_verify(username="adtest" , password="4dscsdx")
		sleep(3)
		po = login(self.driver)
		self.assertEqual(po.login_user_success(), u'adtest')
		function.insert_img(self.driver, "user_pwd_true.jpg")


if __name__ == '__main__':
	unittest.main()

    这样登录的测试用例就完成了。使用po模式之后,如果页面ui发生变化,我们只需要修改元素的定位方法,而不需要改动整个框架,相对而言比较快捷。

    根据登录测试用例,整理下自动化的测试思路:在整个商城页面中,尽量分页面进行操作。如登录页面,封装在一个page中,添加代理商封装在另外的一个页面中;页面和页面之间是独立的,互不影响。这里主页面的url是http://192.168.16.6/hotelManage而其他页面的url都是主页面+其他url拼接而成的。

以上登录的测试用例已经完成,接下来是酒店的测试用例编写

hotel_sta.py

    添加酒店的测试用例,这里主要添加了成功的测试用例;异常的用例中断言后期完善。

# -*-coding:utf-8-*-
# _author_ = "jane_host"
import string
from time import sleep
import unittest, random, sys
from models import myunit, function
from page_obj.hotelPage import hotel
from page_obj.loginPage import login
from page_obj.hotelQueryPage import hotelquery
sys.path.append("./models")
sys.path.append("./page_obj")
reload(sys)
sys.setdefaultencoding('utf8')
class hotelTest(myunit.MyTest):

'''
测试酒店添加
'''
def test_hotel(self):
	'''测试酒店添加'''
	username = "testeres01"
	password = "123456"
	po = login(self.driver)
	# po.user_login(username=username, password=password)
	po.user_login()
	function.insert_img(self.driver, "login_success.jpg")
	# 酒店添加
	num = ''.join(random.sample(string.digits, 8))
	# print num
	hotel_name = "name" + num
	contacts = "contacts" + num
	phone = "15256255412"
	email = "85135535656@mail.com"
	address = "address" + num
	min_hour = "1"
	# 酒店添加操作
	ho = hotel(self.driver)
	ho.add_hotel(hotel_name, contacts, phone, email, address, min_hour)
	# 酒店query操作
	hqo = hotelquery(self.driver)
	self.assertEqual(hqo.get_hotel_name(), hotel_name)
	function.insert_img(self.driver, "hotel_success.jpg")


if __name__ == '__main__':
	unittest.main()

agent_sta.py

代理商添加测试用例

# -*-coding:utf-8-*-
# _author_ = "jane_host"
import string
from time import sleep
import unittest, random, sys
from models import myunit, function
from page_obj.agentPage import agent
from page_obj.loginPage import login
from page_obj.agentQueryPage import agentquery
sys.path.append("./models")
sys.path.append("./page_obj")
reload(sys)
sys.setdefaultencoding('utf8')
class agentTest(myunit.MyTest):

		'''
		测试代理商添加
		'''
		def test_agent(self):
    		'''测试代理商添加'''
    		# username = "123456"
    		# password = "test123"
    		po = login(self.driver)
    		# po.user_login(username=username, password=password)
    		po.user_login()
    		function.insert_img(self.driver, "login_success1.jpg")
    		# 代理商添加
    		num = ''.join(random.sample(string.digits, 8))
    		# print num
    		agent_name = "agent" + num
    		phone = "15356255412"
    		email = "86135535656@mail.com"
    		address = "address" + num
    		payment = "30"
    		discount = "60"
    		# 代理商添加操作
    		ao = agent(self.driver)
    		# agent_code = ao.get_agent_code()
    		ao.add_agent(agent_name, phone, email, address, payment, discount)

    		# agent_code = ao.add_agent(agent_name, phone, email, address, payment, discount)

    		# 代理商query操作,此处验证的是代理商的名称
    		aqo = agentquery(self.driver)
    		self.assertEqual(aqo.get_factor_name(), agent_name)
    		function.insert_img(self.driver, "agent_success.jpg")


	if __name__ == '__main__':
			unittest.main()

agentpirce_sta.py

代理商单价测试用例

# -*-coding:utf-8-*-
# _author_ = "jane_host"
import string
from time import sleep
import unittest, random, sys
from models import myunit, function
from page_obj.agentpricePage import agentprice
from page_obj.loginPage import login
from page_obj.agentpriceQueryPage import agentpricequery
sys.path.append("./models")
sys.path.append("./page_obj")
reload(sys)
sys.setdefaultencoding('utf8')
class agentpriceTest(myunit.MyTest):

		'''
		测试代理商单价添加
		'''
		def test_hotelprice(self):
    		'''测试代理商单价添加'''
		username = "tester01"
    		password = "123456"
    		po = login(self.driver)
    		po.user_login(username=username, password=password)
    		#po.user_login()
    		function.insert_img(self.driver, "login_sys2_success.jpg")
    		# 代理商单价添加
    		num = ''.join(random.sample(string.digits, 8))
    		# print num
    		min = "1"
    		max = "1000"
    		price = "1000"
    		# 代理商单价添加操作
    		apo = agentprice(self.driver)
    		apo.add_agentprice(min, max, price)
    		# 酒店单价query操作
    		apqo = agentpricequery(self.driver)
    		# self.assertEqual(apqo.get_hotelprice_hotel_name(), hotel_name)
    		function.insert_img(self.driver, "agentprice_success.jpg")


	if __name__ == '__main__':
			unittest.main()

employee_sta.py

职员测试用例

# -*-coding:utf-8-*-
# _author_ = "jane_host"
import string
from time import sleep
import unittest, random, sys
from models import myunit, function
from page_obj.employeePage import employee
from page_obj.loginPage import login
from page_obj.employeeQueryPage import employeequery
from mztestpro.package import card
sys.path.append("./models")
sys.path.append("./page_obj")

reload(sys)
sys.setdefaultencoding('utf8')
class employeeTest(myunit.MyTest):

		'''
		测试职员添加
		'''
		def test_employee(self):
    		'''测试职员添加'''
    		# username = "testeres01"
    		# password = "123456"
    		po = login(self.driver)
    		# po.user_login(username=username, password=password)
    		po.user_login()
    		function.insert_img(self.driver, "login_success4.jpg")
    		# 职员添加
    		num = ''.join(random.sample(string.digits, 8))
    		# print num
    		employee_name = "employee" + num
    		# 随机身份证的验证
    		employee_id = card.getRandomIdNumber(0)[0]
    		phone = card.createPhone()
    		employee_time = "2018-01-11"
    		email = "86135535656@mail.com"
    		# 代理商添加操作
    		epo = employee(self.driver)
    		# agent_code = ao.get_agent_code()
    		epo.add_employee(employee_name, employee_name, employee_id, phone, employee_time, email)

    		# agent_code = ao.add_agent(agent_name, phone, email, address, payment, discount)

    		# 代理商query操作,此处验证的是代理商的名称
    		eqo = employeequery(self.driver)
    		self.assertEqual(eqo.get_staff_name(), employee_name)
    		function.insert_img(self.driver, "employee_success.jpg")


	if __name__ == '__main__':
			unittest.main()

hotelprice_sta.py

酒店单价测试用例

# -*-coding:utf-8-*-
# _author_ = "jane_host"
import string
from time import sleep
import unittest, random, sys
from models import myunit, function
from page_obj.hotelPricePage import hotelprice
from page_obj.loginPage import login
from page_obj.hotelPriceQueryPage import hotelpricequery
sys.path.append("./models")
sys.path.append("./page_obj")
reload(sys)
sys.setdefaultencoding('utf8')
class hotelpriceTest(myunit.MyTest):

		'''
		测试酒店单价添加
		'''
		def test_hotelprice(self):
    		'''测试酒店单价添加'''
    		username = "tester01"
   	 		password = "123456"
    		po = login(self.driver)
    		po.user_login(username=username, password=password)
    		#po.user_login()
    		function.insert_img(self.driver, "login_sys_success.jpg")
    		# 酒店单价添加
    		num = ''.join(random.sample(string.digits, 8))
   	 		# print num
    		min = "1"
    		max = "1000"
    		price = "1000"
    		# 酒店单价添加操作
    		hpco = hotelprice(self.driver)
    		hpco.add_hotelprice(min, max, price)
    		# 酒店单价query操作
    		hpqo = hotelpricequery(self.driver)
    		# self.assertEqual(hpqo.get_hotelprice_hotel_name(), hotel_name)
    		function.insert_img(self.driver, "hotelprice_success.jpg")


	if __name__ == '__main__':
			unittest.main()

productcategory_sta.py

产品类型测试用例

# -*-coding:utf-8-*-
# _author_ = "jane_host"
import string
from time import sleep
import unittest, random, sys
from models import myunit, function
from page_obj.productcategoryPge import productcategory
from page_obj.loginPage import login
from page_obj.productcategoryQueryPage import productcategoryquery
sys.path.append("./models")
sys.path.append("./page_obj")
reload(sys)
sys.setdefaultencoding('utf8')
class productcategoryTest(myunit.MyTest):

		'''
		测试产品类型添加
		'''
		def test_productcategory(self):
    		'''测试产品类型添加'''
    		username = "testeres01"
    		password = "123456"
    		po = login(self.driver)
    		# po.user_login(username=username, password=password)
    		po.user_login()
    		function.insert_img(self.driver, "login_success2.jpg")
    		# 酒店添加
    		num = ''.join(random.sample(string.digits, 8))
    		# print num
    		productcategory_name = "productcategory" + num
    		productcategory_des = "productcategorydes" + num
    		# 产品类型添加操作
    		pco = productcategory(self.driver)
    		pco.add_productcategory(productcategory_name, productcategory_des)
    		# 产品类型query操作
    		pcq = productcategoryquery(self.driver)
    		self.assertEqual(pcq.get_category_name(), productcategory_name)
    		function.insert_img(self.driver, "category_success.jpg")


	if __name__ == '__main__':
			unittest.main()

productinfo_sta.py

产品信息测试用例

# -*-coding:utf-8-*-
# _author_ = "jane_host"
import string
from time import sleep
import unittest, random, sys
from models import myunit, function
from page_obj.productinfoPage import productinfo
from page_obj.loginPage import login
from page_obj.productinfoQueryPage import productinfoquery
sys.path.append("./models")
sys.path.append("./page_obj")
reload(sys)
sys.setdefaultencoding('utf8')
class productinfoTest(myunit.MyTest):

		'''
		测试产品信息添加
		'''
		def test_productinfo(self):
    		'''测试产品信息添加'''
    		username = "testeres01"
    		password = "123456"
    		po = login(self.driver)
    		# po.user_login(username=username, password=password)
    		po.user_login()
    		function.insert_img(self.driver, "login_success3.jpg")
   	 		# 酒店添加
    		num = ''.join(random.sample(string.digits, 8))
    		# print num
    		product_name = "product" + num
    		rent = "15"
    		sale = "55"
    		product_des = "productdes" + num
    		# 产品信息添加操作
    		pio = productinfo(self.driver)
    		pio.add_product(product_name, rent, sale, product_des)
    		# 产品类型query操作
    		piq = productinfoquery(self.driver)
    		self.assertEqual(piq.get_productinfo_name(), product_name)
    		function.insert_img(self.driver, "productinfo_success.jpg")


	if __name__ == '__main__':
			unittest.main()

role_sta.py

角色测试用例

# -*-coding:utf-8-*-
# _author_ = "jane_host"
import string
from time import sleep
import unittest, random, sys
from models import myunit, function
from page_obj.rolePage import role
from page_obj.loginPage import login
from page_obj.roleQueryPage import rolequery
from mztestpro.package import card
sys.path.append("./models")
sys.path.append("./page_obj")

reload(sys)
sys.setdefaultencoding('utf8')
class roleTest(myunit.MyTest):

		'''
		测试角色添加
		'''
		def test_role(self):
    		'''测试角色添加'''
    		# username = "testeres01"
    		# password = "123456"
    		po = login(self.driver)
    		# po.user_login(username=username, password=password)
    		po.user_login()
    		function.insert_img(self.driver, "login_success5.jpg")
    		# 角色添加
    		num = ''.join(random.sample(string.digits, 8))
    		# print num
    		role_name = "role" + num
    		role_desc = "roledesc" + num
    		# 角色添加操作
    		ro = role(self.driver)
    		# agent_code = ao.get_agent_code()
    		ro.add_role(role_name, role_desc)

    		# agent_code = ao.add_agent(agent_name, phone, email, address, payment, discount)

    		# 角色query操作,此处验证的是角色的名称
    		rqo = rolequery(self.driver)
    		self.assertEqual(rqo.get_part_name(), role_name)
    		function.insert_img(self.driver, "role_success.jpg")


	if __name__ == '__main__':
			unittest.main()

user_sta.py

用户测试用例信息

# -*-coding:utf-8-*-
# _author_ = "jane_host"
import string
from time import sleep
import unittest, random, sys
from models import myunit, function
from page_obj.userPage import user
from page_obj.loginPage import login
from page_obj.userQueryPage import userquery
from mztestpro.package import card
sys.path.append("./models")
sys.path.append("./page_obj")

reload(sys)
sys.setdefaultencoding('utf8')
class userTest(myunit.MyTest):

		'''
		测试用户添加
		'''
		def test_employee(self):
    		'''测试用户添加'''
    		# username = "testeres01"
    		# password = "123456"
    		po = login(self.driver)
    		# po.user_login(username=username, password=password)
    		po.user_login()
    		function.insert_img(self.driver, "login_success6.jpg")
    		# 用户添加
    		num = ''.join(random.sample(string.digits, 8))
    		# print num
    		user_name = "user" + num
    		pwd = "123456"

    		# 用户添加操作
    		uo = user(self.driver)
    		# agent_code = ao.get_agent_code()
    		uo.add_user(user_name, pwd, pwd)

    		# agent_code = ao.add_agent(agent_name, phone, email, address, payment, discount)

    		# 用户query操作,此处验证的是代理商的名称
    		uqo = userquery(self.driver)
    		self.assertEqual(uqo.get_customer(), user_name)
    		function.insert_img(self.driver, "user_success.jpg")


	if __name__ == '__main__':
			unittest.main()

warehouse_sta.ppy

仓库添加测试用例

# -*-coding:utf-8-*-
# _author_ = "jane_host"
import string
from time import sleep
import unittest, random, sys
from models import myunit, function
from page_obj.warehousePage import warehouse
from page_obj.loginPage import login
from page_obj.warehouseQueryPage import warehousequery
sys.path.append("./models")
sys.path.append("./page_obj")
reload(sys)
sys.setdefaultencoding('utf8')
class warehouseTest(myunit.MyTest):

		'''
		测试仓库添加
		'''
		def test_agent(self):
    		'''测试仓库添加'''
    		# username = "testeres01"
    		# password = "123456"
    		po = login(self.driver)
    		# po.user_login(username=username, password=password)
    		po.user_login()
    		function.insert_img(self.driver, "login_success1.jpg")
    		# 仓库添加
    		num = ''.join(random.sample(string.digits, 8))
    		# print num
    		warehouse_name = "warehouse" + num
    		province = "province" + num
    		city = "city" + num
    		region = "region" + num
    		street = "street" + num
    		address = "address" + num
    		contacts = "contacts" + num
   			phone = "15556255412"
    		des = "waredes" + num
    		# 仓库添加操作
    		wo = warehouse(self.driver)
    		# agent_code = ao.get_agent_code()
    		wo.add_warehouse(warehouse_name, province, city, region, street,
                     address, contacts, phone, des)

    		# agent_code = ao.add_agent(agent_name, phone, email, address, payment, discount)

    		# 仓库query操作,此处验证的是仓库的名称
    		wqo = warehousequery(self.driver)
    		self.assertEqual(wqo.get_depot_name(), warehouse_name)
    		function.insert_img(self.driver, "warehouse_success.jpg")


	if __name__ == '__main__':
			unittest.main()

五、执行测试用例

为了在测试用例运行过程中不影响做其他事,笔者选择调用远程主机或虚拟机来运行测试用例,那么这里就需要使用Selenium Grid(其包含Selenium Server)来调用远程节点。

创建用例执行程序:...\mztestpro\run_bbs_test.py

run_bbs_test.py

# -*-coding:utf-8-*-
# _author_ = "janehost"
import os
import smtplib
import sys
import time
import unittest
from email.mime.text import MIMEText

from mztestpro.package.HTMLTestRunner import HTMLTestRunner

reload(sys)
sys.setdefaultencoding('utf8')
# =========================邮件接收者============================
mailto_list = ["*****@qq.com"]
#============= 设置服务器,用户名、口令以及邮箱的后缀===============
mail_host = "smtp.126.com"
mail_user = "*******@126.com"
mail_pass = "************"
#===========================发送邮件============================
def send_mail(to_list, file_new):
		"""
		to_list: 发给谁
		sub: 主题
		content:内容
		send_mail("*******@126.com","商城测试报告","主要功能回归测试")
		"""
		f = open(file_new, 'rb')
		mail_body = f.read()
		f.close()
		me = mail_user
		msg = MIMEText(mail_body, 'html', 'utf-8')
		msg['Subject'] = u'自动化测试报告'
		msg['From'] = me
		msg['To'] = ";".join(to_list)
		try:
    		s = smtplib.SMTP()
    		s.connect(mail_host, 25)
    		s.login(mail_user, mail_pass)
    		s.sendmail(me, to_list, msg.as_string())
    		s.close()
    		return True
		except Exception, e:
    		print str(e)
    		return False
# ==============查找测试报告目录,找到最新生成的测试报告文件==========
def new_report(testreport):
		lists = os.listdir(testreport)
		lists.sort(key=lambda fn:os.path.getatime(testreport + "\\" + fn))
		file_new = os.path.join(testreport, lists[-1])
		print (file_new)
		return file_new


if __name__ == '__main__':
		now = time.strftime("Y-%m-%d %H_%M_%S")
		filename = './bbs/report/' + now + 'result.html'
		fp = open(filename, 'wb')
		runner = HTMLTestRunner(stream=fp, title=u'商城自动化测试报告',
                        description=u'环境:windows 10 浏览器:firefox')
		discover = unittest.defaultTestLoader.discover('./bbs/test_case',
                                               pattern='*_sta.py')
		runner.run(discover)
		fp.close()
		file_path = new_report('./bbs/report/')
		if send_mail(mailto_list, file_path):
    		print u"发送成功"
		else:
    		print u"发送失败"

执行过程中并没有任何改动,集成HTMLTestRunner生成的HTML测试报告,以及集成自动发邮件功能等。唯一需要注意的是,脚本中的路径建议使用相对路径,以便于项目被移动到任意目录下执行。

打开...\mztestpro\driver.py 文件,修改脚本运行的节点及浏览器。现在可以通过运行run_bbs_test.py来执行测试项目了。

小结:

如果你完成了前面的操作,那么这只是自动化项目的开始,不过,我们已经把基本架构设计完成,后面大部分工作就是编写各个页面的Page.py以及测试用_sta.py。在这个过程中会遇到各种各样的问题,如元素定位、架构的扩展,需要读者自己去克服这些问题。上面只是实现了正常测试用例的编写,适用于回归测试;至于异常测试用例根据需要后期完善。

六、HTMLTestRunne和其他公共类

HTMLTestRunner类和身份证自动生成,位置mztestpro\package\HTMLTestRunner.py、addr.py和card.py
参考随机身份证和手机号

链接: https://share.weiyun.com/85d3c89fbe3d614a1d6352c7b261ba10 (密码:oN7M)

参考:

小小的博客 [http://www.cnblogs.com/xiaozhiblog/p/5378723.html]

selenium2 python自动化测试实战(虫师)

posted @ 2018-02-24 15:06  janehost  阅读(5743)  评论(5编辑  收藏  举报