Selenium自动化Page模式(Python)

Selenium是当前主流的web自动化工具,提供了多种浏览器的支持(Chrome,Firefox, IE等等),固然你们也能够用本身喜欢的语言(Java,C#,Python等)来写用例,很容易上手。当你们写完第一个自动化用例的时候确定感受”哇...好牛x“,可是你们用余光扫了一下代码后,心里也许是崩溃的,由于太乱了!像这样:html

__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()

从几点来分析下上边的代码:web

1. 易读性:很是难理解。这么多find element?这难道也是test case?chrome

2. 可扩展性:都是一个个孤立的test case,无扩展性可言浏览器

3. 可复用性:无公共方法,很难提到复用ide

4. 可维护性:一旦页面元素修改,则须要相应修改全部相关用例,effort大工具

基于以上的问题,Python为咱们提供了Page模式来管理测试,它大概是这样子的:(TestCase中的虚线箭头应该是指向各个page,家里电脑没装修改软件,就不改了:))测试

 

关于Page模式:url

1. 抽象出来一个BasePage基类,它包含一个指向Selenium.webdriver的属性spa

2. 每个webpage都继承自BasePage基类,经过driver来获取本页面的元素,每一个页面的操做都抽象为一个个方法code

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

相关文章
相关标签/搜索