首先,先看一下,我的项目目录结构:
主要包括4个部分:
1. config: 存储全局变量,例如测试报告存储位置,测试web url等
2. data:测试数据存放位置。以数据驱动测试,脚本编写好之后,除非修改脚本bug以外,基本不用再动脚本,只需要维护不同场景下的测试数据即可,这是自动化测试的目标。
3. src:存放测试脚本的位置:
- common:存放公共方法
- pages:存放测试页面的定义,此部分定义测试页面有关元素的定位以及页面元素操作,例如:对于登陆页面,在此定义有关登陆页面所有的操作,如:输入用户名方法,点击登陆按钮方法,检查登陆是否成功方法;
- test_case: 存放测试用例,在pages 目录下定义的页面的基础上,将测试有关操作整合成一个个测试用例。
4. sar.py: 这个文件是一个运行测试用例的小工具,可以对test_case目录下的测试用例指定一个运行,也可以指定几个来运行。
下面,开始对上述架构中的每一个细节逐个讲解。在此之前,config目录下创建一个存放全局变量的文件globalparameter.py
//config/globalparameter.py
import time,os
'''
config gloable parameters
'''
# the web url for test
web_url = "http://xxxx/xxx/"
# the project absolute path
project_path = "C:\\Users\\xxx\\Desktop\\test\\unittest_project\\"
print project_path
# The test cases path
test_case_path = project_path+"\\src\\test_case\\"
# log file path
log_path = "C:\\Users\\xxx\\Desktop\\test\\test_records\\log\\"
print 'log path:'+log_path
# test report path, the report file starts with the current time
report_path = "C:\\Users\\xxx\\Desktop\\test\\test_records\\report\\"
# Error screenshots path,notice: it must be /, not \
img_path = "C:/Users/xxx/Desktop/test/test_records/img/"
然后,在common创建一个BasePage基类,这里封装了一些公共方法,例如:查找元素/ 输入文本框等,后续所有page/页面都基于BasePage类,这样在page只着重于页面元素的定位和操作即可:
//common/BasePage.py
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import sys
'''
page public function
'''
class BasePage(object):
def __init__(self, selenium_driver, base_url):
self.driver = selenium_driver
self.url = base_url
# open one page
def _open_page(self, url):
try:
self.driver.get(url)
self.driver.maximize_window()
except:
raise Exception('Failed to open page:' + url)
else:
Print "Success to open web page."
# override find_element method
def find_element(self, *loc):
try:
for i in range(0,3):
try:
WebDriverWait(self.driver,10).until(EC.visibility_of_element_located(loc))
return self.driver.find_element(*loc)
break
except:
continue
except:
raise Exception('Faild to locate web element:'+str(loc))
# override send_keys method
def send_keys(self, value, clear=True, *loc):
try:
if clear:
self.find_element(*loc).clear()
self.find_element(*loc).send_keys(value)
except AttributeError:
raise Exception('Failed to send key,loc='+str(loc)+u';value='+value)
#screenshot
def img_screenshot(self, img_path, img_name):
try:
img_real_path = img_path +'/'+ img_name + ".png"
print "img_path is %s" % img_real_path
self.driver.get_screenshot_as_file(img_real_path)
except:
print 'Screenshots failed: '+img_real_path
def title(self):
return self.driver.title
def check_page_loaded_correct(self, text, *loc):
"""
After opening a new page by clicking a menu or link
check if the given text is included in the element *loc
"""
print "check page load:step1: locate the given element: %s" % str(loc)
element = self.find_element(*loc)
print "The full content included in the given element: %s" % element.text
print "check page load:step2: check the text given: %s" % text
return EC.text_to_be_present_in_element((loc),text)(self.driver)