Python实现Selenium自动化Page模式


Posted in Python onJuly 14, 2019

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,家里电脑没装修改软件,就不改了:))

Python实现Selenium自动化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自动化中做到游刃有余。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python的SimpleHTTPServer模块用处及使用方法简介
Jan 22 Python
pandas按若干个列的组合条件筛选数据的方法
Apr 11 Python
详解Python 切片语法
Jun 10 Python
Python3+Appium安装使用教程
Jul 05 Python
python虚拟环境的安装和配置(virtualenv,virtualenvwrapper)
Aug 09 Python
pytorch对梯度进行可视化进行梯度检查教程
Feb 04 Python
深入浅析Python代码规范性检测
Jul 31 Python
DRF框架API版本管理实现方法解析
Aug 21 Python
Python使用grequests并发发送请求的示例
Nov 05 Python
python将下载到本地m3u8视频合成MP4的代码详解
Nov 24 Python
matplotlib部件之套索Lasso的使用
Feb 24 Python
python垃圾回收机制原理分析
Apr 13 Python
详解Selenium+PhantomJS+python简单实现爬虫的功能
Jul 14 #Python
python基于Selenium的web自动化框架
Jul 14 #Python
Django项目使用CircleCI的方法示例
Jul 14 #Python
Python实现最常见加密方式详解
Jul 13 #Python
python Pandas库基础分析之时间序列的处理详解
Jul 13 #Python
简单了解python反射机制的一些知识
Jul 13 #Python
Python3内置模块之base64编解码方法详解
Jul 13 #Python
You might like
PHP防止表单重复提交的几种常用方法汇总
2014/08/19 PHP
PHP防止注入攻击实例分析
2014/11/03 PHP
CentOS6.5 编译安装lnmp环境
2014/12/21 PHP
php和editplus正则表达式去除空白行
2015/04/17 PHP
详解Grunt插件之LiveReload实现页面自动刷新(两种方案)
2015/07/31 PHP
分享10段PHP常用代码
2015/11/11 PHP
PHP的curl函数的用法总结
2019/02/14 PHP
浅谈JavaScript中面向对象技术的模拟
2006/09/25 Javascript
JQuery切换显示的效果实例代码
2013/02/27 Javascript
nodejs 中模拟实现 emmiter 自定义事件
2016/02/22 NodeJs
分享一道关于闭包、bind和this的面试题
2017/02/20 Javascript
详解各版本React路由的跳转的方法
2018/05/10 Javascript
vue微信分享出来的链接点开是首页问题的解决方法
2018/11/28 Javascript
[59:35]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#1COL VS Alliance第二局
2016/03/04 DOTA
python中循环语句while用法实例
2015/05/16 Python
PyTorch线性回归和逻辑回归实战示例
2018/05/22 Python
python判断计算机是否有网络连接的实例
2018/12/15 Python
解决在pycharm中显示额外的 figure 窗口问题
2019/01/15 Python
pyqt5移动鼠标显示坐标的方法
2019/06/21 Python
Django restframework 框架认证、权限、限流用法示例
2019/12/21 Python
pytorch对梯度进行可视化进行梯度检查教程
2020/02/04 Python
解决 jupyter notebook 回车换两行问题
2020/04/15 Python
Python中内建模块collections如何使用
2020/05/27 Python
使用python实现名片管理系统
2020/06/18 Python
HTML5中外部浏览器唤起微信分享
2020/01/02 HTML / CSS
美体小铺美国官网:The Body Shop美国
2017/11/10 全球购物
新秀丽官方旗舰店:Samsonite拉杆箱、双肩包、皮具
2018/03/05 全球购物
意大利巧克力店:Chocolate Shop
2019/07/24 全球购物
建筑专业自荐信
2013/10/18 职场文书
大学生自助营养快餐店创业计划书
2014/01/13 职场文书
医科大学毕业生自荐信
2014/02/03 职场文书
大学毕业感言50字
2014/02/07 职场文书
材料员岗位职责
2014/03/13 职场文书
煤矿百日安全活动总结
2015/05/07 职场文书
2015年中秋节主持词
2015/07/30 职场文书
Python 多线程之threading 模块的使用
2021/04/14 Python