Selenium自动化Page模式(Python)

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模式来管理测试,它大概是这样子的:

关于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自动化中做到游刃有余。

时间: 2024-08-27 23:45:07

Selenium自动化Page模式(Python)的相关文章

selenium自动化实战-基于python语言(环境搭建)

在看过了<selenium 2自动化测试实战-基于python语言> 这本书之后,有一些自己发现的问题,在这里记录下来方便自己查阅,也希望大家指正或给出建议. Windows环境搭建: 1. 安装FireBug以及FirePath前端工具来帮助我们查看前端代码. 可以直接通过Firefox的添加插件直接安装,非常方便 IE以及Chrome等浏览器一般使用F12即可调出此类开发人员工具 2. 安装Python 通过python官网或者其他途径下载python安装包,我这里安装的是3.5 64bi

selenium自动化实战-基于python语言(二: 编写脚本)

上一篇文章说到显示等待和隐式等待语句,我们继续学习下面的命令方法. 8.  定位一组元素 这里书上是自己写了一个页面代码,通过访问本地这个页面来举例.但我觉得找一个现有的页面自己琢磨更有意思,而且复杂的页面也会遇到复杂的问题.比如我根据163邮箱登录页面的 "十天内免登录" 复选框写了如下的代码: from selenium import webdriverimport time driver = webdriver.Firefox()driver.get('http://mail.1

selenium自动化实战-基于python语言(三: 编写脚本)

继续webdriver的学习. 11. 警告框处理: switch_to_alert() 方法定位到alert/confirm/prompt,然后使用下面的方法进行操作 返回 alert/confirm/prompt 中的文字信息 text   接受现有警告框 accept() 举例:switch_to_alert().accept() 解散现有警告框 dismiss()   发送文本至警告框 send_keys(keysToSend)   12. 上传文件 send_keys(''): 和模拟

python selenium自动化屏蔽chrome“正受到自动化测试软件的控制”、“开发者模式”、“保存密码提示”

python selenium自动化屏蔽chrome“正受到自动化测试软件的控制”.“开发者模式”.“保存密码提示” from selenium import webdriver option = webdriver.ChromeOptions() #屏蔽自动化受控提示 && 开发者提示 option.add_experimental_option("excludeSwitches", ['enable-automation', 'load-extension']) #

搭建Python开发环境(含Selenium自动化部署)

a.从Python官网下载 Python2.7https://www.python.org/b.安装Python2.7时选择勾上pip和自动配置环境变量(默认Python安装路径C:\Python27),若未自动配置环境变量,手动配置也可c.确定Python和pip安装成功后(命令行下输入python进入python交互式环境>>>),附安装第三方模块PIL的方法 pip install PIL提示出错,因为PIL官方只支持32位,没有提供64位版本可以安装非官方模块Pillow代替,p

Python+Selenium自动化模拟用户登录(备注:记录一次强行卸载rpm依赖包,引发的rpm、yum等命令异常,无法远程xftp工具)

近期在摸索Python+Selenium自动化,实现模拟用户登录搜索等操作,反馈相关日志,再交由Zabbix分析,监控页面访问是否正常. 期间需要对Linux火狐浏览器进行升级,由于生产环境为内网环境,无法使用yum进行升级.本想在外网环境下使用Docker进行安装,然后打成镜像后迁移到内网环境.但由于系统为Red Hat 6.4对Docker支持较差,故在外网将相关依赖包下载后进行安装. 外网环境下,下载Firefox及相关依赖组件相关操作.         1.更新本地yum源为阿里yum源

python+selenium自动化环境配置及使用实例

一.搭建环境相关地址以及相关模块下载地址 1.#各个浏览器驱动下载地址: https://www.cnblogs.com/nancyzhu/p/8589764.html 2.#sublime+python+selenium自动化配置教程: http://python.tedu.cn/know/289803.html 3.#html测试报告: https://github.com/defnngj/HTMLTestRunner         http://www.testpub.cn/t/213

Python+selenium自动化公共逻辑步骤封装

开篇 个人博客"Python+selenium的GUI自动化实现"提到的chrome与IE浏览器调用插件已上传至51CTO下载,对应链接分别为:chrome,http://down.51cto.com/data/2171584:IE,http://down.51cto.com/data/2171585:有需要的直接下载即可:  正文 关于自动化,其实质就是用机器操作代替手工执行,从而减少人力投入.节约项目运营成功.优秀的自动化框架,可能的一个发展过程,前期自动化用例写作实现过程,可能需

如何写好Python+Selenium自动化?

哈喽,各位客官好,今天我给大家讲讲如何使用Python+Selenium做自动化测试,楼主在做开发测试之前做得java开发,由于种种原因,楼主转成了开发测试,接着又自学了脚本语言linux和python,对于这两门脚本,我是非常的喜欢,为什么呢,因为用起来效率太高了,所以楼主弃java转python,至于为什么,不做多解释,你懂得.接下来我将给各位讲讲如何用python+selenium自动化 1,什么是selenium selenium是一个开源的自动化测试框架,主要适用WEB测试,可以支持多