1、新建一个包名:common(用于存放基本函数封装)
(1)在common包下新建一个base.py文件,作用:页面操作封装。base.py文件代码如下:
# coding=utf-8
"""
------------------------------------
@Time : 2020/01/15
@Auth : Anker
@File : base.py
@Description:页面操作封装
@IDE : PyCharm
@Motto: Believe in yourself and persistence can make success!
------------------------------------
"""
from selenium.webdriver.support.wait import WebDriverWait
from config.read_config import ReadConf
class BasePage(object):
# 读取config.ini配置文件,传入sections值
url = ReadConf()
# 传入sections模块
standard_url = url.readConf("sections")
# 这里传入sections模块中的url
base_url = standard_url['url']
def __init__(self, driver, test_url=base_url):
"""
构造函数
:param driver
:param 传入url
"""
self.driver = driver
self.url = test_url
# 设置全局元素隐式等待时间为10秒钟
self.driver.implicitly_wait(10)
def open_url(self):
"""
打开url
:param:
"""
url = self.url
self.driver.get(url)
title = self.driver.title
print("项目名称:", title + "2.6")
print("项目地址:", self.driver.current_url)
def back(self):
"""
浏览器后退按钮
:param:
"""
self.driver.back()
def forward(self):
"""
浏览器前进按钮
:param:
"""
self.driver.forward()
def close(self):
"""
关闭并停止浏览器服务
:param:
"""
self.driver.quit()
def find_element(self, *loc):
"""
判断定位方式(常见的有8种获取元素的方法)
:param * loc
"""
try:
WebDriverWait(self.driver, 20).until(lambda driver: driver.find_element(*loc).is_displayed())
return self.driver.find_element(*loc)
except:
print("元素在页面中未找到!", *loc)
def find_elements(self, *loc):
return self.driver.find_elements(*loc)
def input_content(self, loc, content):
"""
文本框内容输入
:param loc
:param content
"""
self.find_element(*loc).send_keys(content)
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.mouse_click(loc) # 调用鼠标点击事件方法
if clear_first:
self.mouse_clear(loc) # 调用鼠标清理事件方法
self.find_element(*loc).send_keys(value)
except ArithmeticError:
print(u"%s 页面中未能找到 %s 元素" % (self, loc))
def mouse_clear(self, loc):
"""
鼠标清理事件
:param loc
"""
return self.find_element(*loc).clear()
def mouse_click(self, loc):
"""
鼠标点击事件
:param loc
"""
return self.find_element(*loc).click()
def script(self, src):
return self.driver.execute_script(src)
def switch_frame(self, loc):
return self.driver.switch_to_frame(loc)
def isElementPresent(self, element_xpath):
"""
封装一个函数,用来判断页面某个值是否存在
:param element_xpath
"""
try:
self.driver.find_element_by_xpath(element_xpath)
return True
except:
return False
(2)在common包下新建一个driver.py文件,作用:浏览器选择,默认为谷歌浏览器。driver.py文件代码如下:
# coding=utf-8
"""
------------------------------------
@Time : 2020/01/15
@Auth : Anker
@File : driver.py
@Description:浏览器选择,默认为谷歌浏览器
@IDE : PyCharm
@Motto: Believe in yourself and persistence can make success!
------------------------------------
"""
from selenium import webdriver
browser_type = "Chrome"
def open_browser():
"""
浏览器选择(Selenium支持Chrome、Firefox、IE浏览器)
:param:
"""
global driver
if browser_type == 'Firefox':
driver = webdriver.Firefox()
elif browser_type == 'Chrome':
driver = webdriver.Chrome()
elif browser_type == 'IE':
driver = webdriver.Ie()
elif browser_type == '':
driver = webdriver.Chrome()
return driver
if __name__ == '__main__':
driver = open_browser()
(3)在common包下新建一个HTMLTestRunner.py文件,作用:用于生成html报告文件。HTMLTestRunner.py文件代码如下:
#-*- coding: utf-8 -*-
"""
A TestRunner for use with the Python unit testing framework. It
generates a HTML report to show the result at a glance.
The simplest way to use this is to invoke its main method. E.g.
import unittest
import HTMLTestRunner
... define your tests ...
if __name__ == '__main__':
HTMLTestRunner.main()
For more customization options, instantiates a HTMLTestRunner object.
HTMLTestRunner is a counterpart to unittest's TextTestRunner. E.g.
# output to a file
fp = file('my_report.html', 'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title='My unit test',
description='This demonstrates the report output by HTMLTestRunner.'
)
# Use an external stylesheet.
# See the Template_mixin class for more customizable options
runner.STYLESHEET_TMPL = ''
# run the test
runner.run(my_test_suite)
------------------------------------------------------------------------
Copyright (c) 2004-2007, Wai Yip Tung
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name Wai Yip Tung nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
# URL: http://tungwaiyip.info/software/HTMLTestRunner.html
__author__ = "Wai Yip Tung"
__version__ = "0.8.3"
"""
Change History
Version 0.8.4 by GoverSky
* Add sopport for 3.x
* Add piechart for resultpiechart
* Add Screenshot for selenium_case test
* Add Retry on failed
Version 0.8.3
* Prevent crash on class or module-level exceptions (Darren Wurf).
Version 0.8.2
* Show output inline instead of popup window (Viorel Lupu).
Version in 0.8.1
* Validated XHTML (Wolfgang Borgert).
* Added description of test classes and test cases.
Version in 0.8.0
* Define Template_mixin class for customization.
* Workaround a IE 6 bug that it does not treat
%(heading)s
%(report)s
%(ending)s