突然想試試能否將web測試也自動化摔蓝,經(jīng)過搜索膘侮,發(fā)現(xiàn)了Selenium清女,尤其是支持最愛的Python阳惹,那就是它了!
準備工作
- CentOS7 + Python2.7 + Pip + Firefox(52版本) + VNCServer + GNOME
- 使用pip安裝selenium這個package,安裝后看到版本是3.4.1
- 下載最新的geckodriver-v0.16.1-linux64.tar.gz谍失,解壓后mv到/usr/local/bin
- Windows + Firefox + Selenium插件,用來獲取參考腳本的
牛刀小試
- 目標是能實現(xiàn)自動登陸功能莹汤,先在交換機上設置好登陸的用戶名密碼都是admin
-
在Windows先啟動Firefox快鱼,然后點“工具”,激活Selenium
se_plugin.png -
此時會看到彈出一個小的Firefox窗口纲岭,將它最小化即可
sese_IDE.png - 然后在最初的Firefox網(wǎng)頁輸入交換機管理網(wǎng)址抹竹,然后輸入用戶名和密碼,并點擊登陸
-
上述操作止潮,都會被記錄到Selenium-IDE里窃判,可以導出Python腳本
se_export.png - 打開就能看到是一個基于unittest框架的testcase
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://10.10.33.115/"
self.verificationErrors = []
self.accept_next_alert = True
def test_login(self):
driver = self.driver
driver.get(self.base_url + "/static/switch/index.html#/login")
driver.find_element_by_id("username").clear()
driver.find_element_by_id("username").send_keys("admin")
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("admin")
driver.find_element_by_id("submit").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
- 核心代碼肯定是def test_login,非常容易理解
- 這個腳本就已經(jīng)能夠運行了,會自動啟動
稍微進階
- 想在能夠自動登陸的基礎上喇闸,可以進入到vlan設置界面去添加vlan
- 這里走了點彎路袄琳,因為IDE會捕捉我點擊導航欄的動作窿凤,但其實這個是不保險的,最好是登陸后打開對應vlan設置的網(wǎng)址跨蟹,這個是最穩(wěn)的
driver.get(self.base_url + "/static/switch/index.html#/home/srv_mgr/vlan/status")
- 接下來想抓取表格中的內容,經(jīng)過同事指點橘沥,采用css_selector的方法對table指定表格進行定位
vlan_content = driver.find_element_by_css_selector('table tr:nth-child(2) td:nth-child(4)').txt
- 得到的vlan_content就可以用assert來做判斷了
- 另外定位網(wǎng)頁元素窗轩,還可以find_element_by_xpath,有時IDE捕捉到的動作中就包含這個樣式
- 還需要注意的點是網(wǎng)頁加載是需要時間的座咆,要么判斷elements是否已經(jīng)加載成功痢艺,或者簡單地time.sleep(5)再往下執(zhí)行
多處驗證
- 交換機無論通過web界面操作還是login用CLI操作,效果應該是一樣并同步的
- telnet登陸還是用熟悉的pexpect介陶,另外也可以用來check stdout的結果是否和期望一致
后續(xù)工作
如果要把這個框架完善起來堤舒,還需要
- 將相關參數(shù)放到同一的profile文件中
- 需要能夠將很多的case連跑
- 公共部分,例如login web哺呜,可以封裝起來
- 剩下的就是積累case了