Python實現(xiàn)Selenium自動化Page模式
Selenium是當前主流的web自動化工具,提供了多種瀏覽器的支持(Chrome,Firefox, IE等等),當然大家也可以用自己喜歡的語言(Java,C#,Python等)來寫用例,很容易上手。當大家寫完第一個自動化用例的時候肯定感覺”哇...好牛x“,但是大家用余光掃了一下代碼后,內(nèi)心也許是崩潰的,因為太亂了!像這樣:
__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類來實現(xiàn)相應的test case步驟
利用Page模式實現(xiàn)上邊的用例,代碼如下:
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. 易讀性: 現(xiàn)在單看test_login方法,確實有點test case的樣子了,每一步都很明了
2. 可擴展性:由于把每個page的元素操作都集成到一個page類中,所以增刪改查都和方便
3. 可復用性: page的基本操作都變成了一個個的方法,在不同的test case中可以重復使用
4. 可維護性:如果頁面修改,只需修改相應page類中的方法即可,無需修改每個test case
總結(jié):
Page模式給我們提供了一個很好的頁面和用例實現(xiàn)的分離機制,降低了耦合,提高了內(nèi)聚,可以使我們在web自動化中做到游刃有余。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python requests.post帶head和body的實例
今天小編就為大家分享一篇python requests.post帶head和body的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01

