Selenium是当前主流的web自动化工具,提供了多种浏览器的支持(Chrome,Firefox, IE等等),当然大家也可以用自己喜欢的语言(Java,C#,Python等)来写用例,很容易上手。当大家写完第一个自动化用例的时候肯定感觉”哇...好牛x“,但是大家用余光扫了一下代码后,内心也许是崩溃的,因为太乱了!像这样:
__author__ = 'xua'
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest
class TCRepeatLogin(unittest.TestCase):
def setUp(self):
#webdriver
self.driver = webdriver.Chrome(r'C:\Users\xua\Downloads\chromedriver_win32\chromedriver.exe')
self.driver.implicitly_wait(30)
self.base_url = "http://10.222.30.145:9000/"
def test_(self):
driver = self.driver
driver.get(self.base_url)
#enter username and password
driver.find_element_by_id("username").clear()
driver.find_element_by_id("username").send_keys("sbxadmin")
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("IGTtest1"+Keys.RETURN)
#find dialog and check
dialogTitle = driver.find_element(By.XPATH,'//html/body/div[7]/div/div/div[1]/h3')
self.assertEqual("Sign in",dialogTitle.text)
#find cancel button and click
cancelBtn = driver.find_element(By.XPATH,'//html/body/div[7]/div/div/div[3]/button[2]')
cancelBtn.click()
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
从几点来分析下上边的代码:
1. 易读性:非常难理解。这么多find element?这难道也是test case?
2. 可扩展性:都是一个个孤立的test case,无扩展性可言
3. 可复用性:无公共方法,很难提到复用
4. 可维护性:一旦页面元素修改,则需要相应修改所有相关用例,effort大
基于以上的问题,Python为我们提供了Page模式来管理测试,它大概是这样子的:(TestCase中的虚线箭头应该是指向各个page,家里电脑没装修改软件,就不改了:))
关于Page模式:
1. 抽象出来一个BasePage基类,它包含一个指向Selenium.webdriver的属性
2. 每一个webpage都继承自BasePage基类,通过driver来获取本页面的元素,每个页面的操作都抽象为一个个方法
3. TestCase继承自unittest.Testcase类,并依赖相应的Page类来实现相应的test case步骤
利用Page模式实现上边的用例,代码如下:
BasePage.py:
__author__ = 'xua'
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
#super class
class BasePage(object):
def __init__(self, driver):
self.driver = driver
class LoginPage(BasePage):
#page element identifier
usename = (By.ID,'username')
password = (By.ID, 'password')
dialogTitle = (By.XPATH,'//html/body/div[7]/div/div/div[1]/h3')
cancelButton = (By.XPATH,'//html/body/div[7]/div/div/div[3]/button[2]')
#Get username textbox and input username
def set_username(self,username):
name = self.driver.find_element(*LoginPage.usename)
name.send_keys(username)
#Get password textbox and input password, then hit return
def set_password(self, password):
pwd = self.driver.find_element(*LoginPage.password)
pwd.send_keys(password + Keys.RETURN)
#Get pop up dialog title
def get_DiaglogTitle(self):
digTitle = self.driver.find_element(*LoginPage.dialogTitle)
return digTitle.text
#Get "cancel" button and then click
def click_cancel(self):
cancelbtn = self.driver.find_element(*LoginPage.cancelButton)
cancelbtn.click()
Test_Login.py:
__author__ = 'xua'
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
import unittest
import time
import BasePage
class Test_Login(unittest.TestCase):
#Setup
def setUp(self):
self.driver = webdriver.Chrome(r'C:\Users\xua\Downloads\chromedriver_win32\chromedriver.exe')
self.driver.implicitly_wait(30)
self.base_url = "http://10.222.30.145:9000/"
#tearDown
def tearDown(self):
self.driver.close()
def test_Login(self):
#Step1: open base site
self.driver.get(self.base_url)
#Step2: Open Login page
login_page = BasePage.LoginPage(self.driver)
#Step3: Enter username
login_page.set_username("sbXadmin")
#Step4: Enter password
login_page.set_password("IGTtest1")
#Checkpoint1: Check popup dialog title
self.assertEqual(login_page.get_DiaglogTitle(),"Sign in")
#Step5: Cancel dialog
login_page.click_cancel()
if __name__ == "__main__":
unittest.main()
Ok, 那么我们回头来看,Page模式是否解决了上边的四个方面的问题:
1. 易读性: 现在单看test_login方法,确实有点test case的样子了,每一步都很明了
2. 可扩展性:由于把每个page的元素操作都集成到一个page类中,所以增删改查都和方便
3. 可复用性: page的基本操作都变成了一个个的方法,在不同的test case中可以重复使用
4. 可维护性:如果页面修改,只需修改相应page类中的方法即可,无需修改每个test case
总结:
Page模式给我们提供了一个很好的页面和用例实现的分离机制,降低了耦合,提高了内聚,可以使我们在web自动化中做到游刃有余。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
