工廠設計模式在自動化中的引用(一)

在自動化測試的範圍中,目前依據webdriver的,web應用測試框架有selenium2,對於移動app自動化的測試,有appium,selenium2和appium有很多的共同使用的地方,如對屬性對象的定位,都是有id,name等,所以,完全可以把selenium2和appium整合到一個完整的框架中,這樣的目的就是一個框架可以實現對web應用程序自動化的測試,也是可以實現對移動產品UI自動化的測試,同時selenium2和appium都提供了不同的API,這些可以放在個字獨立的類下面,而把selenium2和appium對屬性元素的定位方法,以及共同使用的方法,放在另外的一個類中,這樣,測試web程序,就繼承web對應的類,測試移動的產品,就繼承移動對應的類。工廠設計模式正好符號這樣的需求,即在一個工廠中,可以生產很多的產品,依據消費者的需求要什麼,可以構造一個產品然後提供給消費者。因此,利用這樣的一個理念,可以編寫一個工廠的類Factory,再編寫AutomationPage類,編寫web和移動共同使用到的方法編寫在這個類中,WebPage類和AppPage類全部繼承AutomationPage類,同時WebPage類編寫web應用程序使用到的方法進行封裝,AppPage封裝移動使用到的方法,具體實現的代碼在dashPage.py的模塊中,見實現的代碼:

#!/usr/bin/env python #coding:utf-8

from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support.expected_conditions import NoSuchElementException from selenium.webdriver.support.expected_conditions import NoSuchFrameException from selenium.webdriver.support import expected_conditions from selenium.webdriver.common.by import By from selenium import webdriver from appium import webdriver

class Factory: def createAutomation(self,page): if page=='web': return WebPage() elif page=='app': return AppPage()

class AutomationPage: def __str__(self): return 'page'

def findElement(self,*loc): try: return self.driver.find_element(*loc) except(NoSuchElementException,KeyError,ValueError),e: print 'Error details :%s'%(e.args[0])

class WebPage(AutomationPage): def __str__(self): return 'web'

def __init__(self,driver): self.driver=driver

def goTo(self,base_url): self.driver.get(base_url)

def getCurrentUrl(self): return self.driver.current_url

class AppPage(AutomationPage): def __str__(self): return 'app' 見代碼的截圖:

下面開始實現web應用程序的自動化測試,編寫demoPage.py的模塊,繼承dashPage.WebPage類,在該類中,編寫要測試的應用程序的page對象,本模塊中編寫的是一個簡單的登錄,見如下的代碼:

#coding:utf-8

from selenium import webdriver from selenium.webdriver.common.by import By from Page import dashPage import time

class demoWebPage(dashPage.WebPage): username_loc=(By.ID,'l-1') password_loc=(By.ID,'l-2') loginButton_loc=(By.ID,'l-4')

def inputUserName(self,username): self.findElement(*self.username_loc).send_keys(username) time.sleep(2)

def inputPasswd(self,password): self.findElement(*self.password_loc).send_keys(password) time.sleep(2)

def clickLogin(self): self.findElement(*self.loginButton_loc).click() time.sleep(2)

def login(self,username='admin',password='admin'): self.goTo('http://my.weke.com/login.html') self.inputUserName(username) self.inputPasswd(password) self.clickLogin()

見代碼截圖:

測試代碼就好很多了,編寫測試的模塊demoPageTest.py,該測試模塊中的類繼承TestCase,實例化demoPage類,就可以實現完整的自動化了,見如下的代碼:

#coding:utf-8

from selenium import webdriver from selenium.common.exceptions import * from time import sleep,time,ctime import unittest from Page import dashPage,demoPage import time import logging from selenium.webdriver.common.desired_capabilities import DesiredCapabilities DesiredCapabilities.INTERNETEXPLORER['ignoreProtectedModeSettings'] = True

class singPage(unittest.TestCase): def setUp(self): self.driver=webdriver.Firefox() self.driver.maximize_window() self.driver.implicitly_wait(30)

def testTitle(self): page=demoPage.demoWebPage(self.driver) page.login()

def tearDown(self): self.driver.quit()

if __name__=='__main__': unittest.main(verbosity=2)

見代碼截圖:

這樣實現的意義有什麼好處,個人認爲有如下的幾點優勢:

1、父類層只編寫selenium2,appium共同可以使用到的方法;

2、在對象層中,selenium2和appium完全隔離開,selenium2寫web的,appium寫app的

3、在測試層中,也是完全分開的,web和app各自執行自己的case,完全不影響

selenium2和webdriver的整合在某些程度上是一個趨勢,更多層次的原因是這二個框架都符合webdriver,另外在語言層次上,都是可以使用的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章