您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
簡易Selenium自動化測試框架(Python)
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/6/14 11:42:10 ] 推薦標(biāo)簽:自動化測試工具 Selenium

  近空閑時(shí)間在探索Selenium的自動化測試,簡單的寫了一個(gè)小框架來測試公司的一個(gè)web產(chǎn)品。該框架包括以下模塊:

  1. Test case編寫模式(page模式,參考之前的博文http://www.cnblogs.com/AlwinXu/p/5537955.html)
  2. Test case的管理及執(zhí)行 (主要是用nose)

  該模塊借助了一個(gè)外部txt文件來記錄測試用例,每個(gè)用例為自身的文件名,如果不需要在本次執(zhí)行,只需在文件名前添加一個(gè)“#”標(biāo)識符可以跳過該用例的執(zhí)行。
  3. 測試報(bào)告的生成(xml和html兩種格式)
  對于自動化測試而言,這些模塊應(yīng)該是基本的配置了,當(dāng)然還有一些輔助模塊比如日志,其他公共庫模塊等需要根據(jù)具體的業(yè)務(wù)逐漸豐富。閑話少說,用代碼交流吧。
  測試用例編寫
  該模塊用了Page模式,之前介紹過,這次只貼代碼了
  BasePage.py:
__author__ = 'xua'
#super class
class BasePage(object):
def __init__(self, driver):
self.driver = driver
  然后是各個(gè)web page繼承BasePage,LoginPage.py:
from BasePage import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
class LoginPage(BasePage):
"""description of class"""
#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()
  測試用例信息類:
  TestCaseInfo.py
class TestCaseInfo(object):
"""description of class"""
def __init__(self, id="",name="",owner="",result="Failed",starttime="",endtime="",errorinfo=""):
self.id = id
self.name = name
self.owner = owner
self.result = result
self.starttime = starttime
self.endtime = endtime
self.errorinfo = errorinfo
  后是每個(gè)測試用例的編寫:(每個(gè)用例必須有自己的用例信息,這里有ID,Name等等信息,也會調(diào)用測試結(jié)果報(bào)告生成模塊來添加測試結(jié)果)
  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
from LoginPage import LoginPage
from TestCaseInfo import TestCaseInfo
from TestReport import TestReport
class Test_Login(unittest.TestCase):
#Setup
def setUp(self):
self.driver = webdriver.Chrome(r'C:UsersxuaDownloadschromedriver_win32chromedriver.exe')
self.driver.implicitly_wait(30)
self.base_url = "http://10.222.30.145:9000/"
#test case information
self.testcaseinfo = TestCaseInfo(id="3",name="Login to floor manager lite using sbxadmin",owner="xua")
self.testResult = TestReport()
def test_Login(self):
try:
self.testcaseinfo.starttime = str(time.asctime())
#Step1: open base site
self.driver.get(self.base_url)
#Step2: Open Login page
login_page = 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","Not Equal")
#Step5: Cancel dialog
login_page.click_cancel()
self.testcaseinfo.result = "Pass"
except Exception as err:
self.testcaseinfo.errorinfo = str(err)
finally:
self.testcaseinfo.endtime = str(time.asctime())
#tearDown
def tearDown(self):
self.driver.close()
#write test result
self.testResult.WriteHTML(self.testcaseinfo)
if __name__ == "__main__":
unittest.main()

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd