代码展示:
from selenium import webdriver
from time import sleep
driver = webdriver.Firefox()
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys("jack")
sleep(1)
driver.find_element_by_id("su").click()
sleep(2)
driver.quit()
代码结构分析:
1、不同的运行脚本环境,游览器不同:驱动webdriver.Firefox()可以剥离
2、请求地址的变化(生产环境和测试环境):url = http://www.baidu.com可以剥离
3、操作元素时,常常要等待元素加载完成方可进行操作:可以把webdriver提供的find_element*方法封 装,在元素操作前,先判断元素是否可以操作
4、实际测试场景中,可能有多个测试场景,如果每个测试场景都需要维护url,游览器驱动,元素定位等,效 率会非常低
5、因此基于以上分析,是否可以设计一个所有测试界面(selenium本身是B/S系统开展测试)的基类,来维护公共的方法,此处定义名字为BasePage.py,用于存放页面公共方法及? ? ? ? ? ? webdriver原有方法二次封装等
1、实现BasePage
代码实现展示:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains #鼠标操作
class BasePage():
'''BasePage封装所有界面都公用的方法。例如driver,find_element等'''
'''实例化BasePage类时,事先执行的__init__方法,该方法需要传递参数'''
def __init__(self,driver):
self.driver = driver
# 进入网址
def get(self,url):
self.driver.get(url)
#元素定位,替代八大定位
def get_element(self,*locator):
return self.driver.find_element(*locator)
#点击
def left_click(self,*locator):
ActionChains(self.driver).click(self.get_element(*locator)).perform()
#输入
def send_text(self,text,*locator):
self.driver.find_element(*locator).send_keys(text)
#清除
def clear_text(self,*locator):
self.driver.find_element(*locator).clear()
#表单切换
def switch_iframe(self,*locator):
self.driver.switch_to.frame(self.driver.find_element(*locator))
#窗口切换
def switch_window(self,n):
self.driver.switch_to.window(self.driver.window_handles[n])
2、实现SearchPage
代码实现展示:
'''
实现步骤:(1)继承basepage,(2)元素传参,(3)调取方法
'''
from selenium.webdriver.common.by import By
from pomodel.Base.base_page import BasePage
class Search(BasePage):
def __init__(self,driver):
BasePage.__init__(self,driver)
#进入百度
def open_baidu(self,url):
self.get(url)
#输入搜索内容
def input_search_content(self,text):
self.send_text(text,By.ID,"kw")
#点击按钮
def click_baidu_search(self):
self.left_click(By.ID,"su")
3、实现TestCase
代码实现展示:
import unittest
from selenium import webdriver
from pomodel.Pages.search_pages import Search
class BaiBu(unittest.TestCase):
def setUp(self) -> None:
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(10)
def test_serach(self):
url="http://www.baidu.com"
s = Search(self.driver)
s.open_baidu(url)
s.input_search_content("jack")
s.click_baidu_search()
def tearDown(self) -> None:
self.driver.quit()
if __name__ == '__main__':
unittest.main()
1、提供一个basepage类
from selenium.webdriver.support.ui import WebDriverWait # 显示等待
from selenium.webdriver.support import expected_conditions as EC # 期望条件
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains #鼠标操作
class BasePage():
'''BasePage封装所有界面都公用的方法。例如driver,find_element等'''
def __init__(self,driver):
self.driver = driver
# 进入网址
def get(self,url):
self.driver.get(url)
#元素定位,替代八大定位
def get_element(self,*locator):
return self.driver.find_element(*locator)
#点击
def left_click(self,*locator):
ActionChains(self.driver).double_click(self.get_element(*locator)).perform()
#输入
def send_text(self,text,*locator):
self.driver.find_element(*locator).send_keys(text)
#清除
def clear_text(self,*locator):
self.driver.find_element(*locator).clear()
#用于断言
def get_attribuit_value(self,name,*locator):
return self.get_element(*locator).get_attribute(name)
def get_title(self):
return self.driver.title
def get_current_url(self):
return self.driver.current_url
def get_element_text(self,*locator):
return self.driver.find_element(*locator).text
def switch_iframe(self,*locator):
self.driver.switch_to.frame(self.driver.find_element(*locator))
#显示等待
def find_element_by_wait(self,*locator):
element =
WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((locator[
0],locator[1])))
return element
2、多个page(界面操作)
page1:在游览器中访问:qq邮箱(https://qzone.qq.com/)
'''
实现步骤:(1)继承basepage,(2)元素传参,(3)调取方法
'''
from po10.basepage.base_page import BasePage
from selenium.webdriver.common.by import By
class DLpage(BasePage):
def __init__(self,driver):
BasePage.__init__(self,driver)
def open_qq(self,url):
self.get(url)
def dlget_title(self):
self.get_title()
page2:在page2中在表单中输入用户名和密码
from po10.basepage.base_page import BasePage
from selenium.webdriver.common.by import By
class BDpage(BasePage):
def __init__(self,driver):
BasePage.__init__(self,driver)
def bd_switch_frame(self):
self.switch_iframe(By.ID,"login_frame")
def click_zhmmdl(self):
self.left_click(By.ID,"switcher_plogin")
def clear_zh(self):
self.clear_text(By.ID,"u")
def send_zh(self,text):
self.send_text(text,By.ID,"u")
def clear_mm(self):
self.clear_text(By.ID,"p")
def send_mm(self,text):
self.send_text(text,By.ID,"p")
def clink_login(self):
self.left_click(By.ID,"login_button")
def dyys(self):
self.get_element_text(By.ID,"forgetpwd")
page3:在主界面中点击留言板?
from po10.basepage.base_page import BasePage
from selenium.webdriver.common.by import By
class ZjmClass(BasePage):
def __init__(self,driver):
BasePage.__init__(self,driver)
def click_lyb(self):
self.left_click(By.XPATH,".//*[@id='menuContainer']/div/ul/li[4]/a")
3、单元测试
import unittest
from selenium import webdriver
from po10.pages.dlpage import DLpage
from po10.pages.bdpage import BDpage
from po10.pages.zjmpage import ZjmClass
class Baidu(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.driver =webdriver.Firefox()
cls.driver.maximize_window()
cls.driver.implicitly_wait(30)
def test_01qq(self):
url ="https://qzone.qq.com/"
s= DLpage(self.driver)
s.open_qq(url)
def test_02bd(self):
db = BDpage(self.driver)
db.bd_switch_frame()
db.click_zhmmdl()
db.clear_zh()
db.send_zh("账号")
db.clear_mm()
db.send_mm("密码")
db.clink_login()
def test_03zjm(self):
zjm = ZjmClass(self.driver)
zjm.click_lyb()
@classmethod
def tearDownClass(cls) -> None:
cls.driver.close()
if __name__ == '__main__':
unittest.main()
4、生成测试报告
import unittest
from testcase.test02 import Baidu
from tooldemo.HTMLTestRunner import HTMLTestRunner
class HtmlClass():
def htmlMethod(self):
suite = unittest.TestSuite() # 创建测试套件
case_list = ["test_01qq","test_02bd","test_03zjm"]
for case in case_list:
suite.addTest(Baidu(case))
with open("../report.html","wb") as f:
HTMLTestRunner(
stream=f,
title="单元测试",
description="测试一期",
verbosity=2
).run(suite)
htmlclass = HtmlClass()
htmlclass.htmlMethod()
5、测试报告代码展示