1. 程式人生 > >Selenium學習筆記之004:使用Selenium IDE錄製指令碼並分析unittest框架

Selenium學習筆記之004:使用Selenium IDE錄製指令碼並分析unittest框架

unittest在Python單元測試中很常見,下面藉助IDE錄製的指令碼並匯出來分析它的框架。

在百度首頁輸入selenium,空格,刪除空格,然後回車搜尋,錄製如下:


將錄製完的指令碼匯出,另存為baidu_test.py


開啟另存為的指令碼,其內容如下:

# -*- 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 BaiduTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()#
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.baidu.com/"
        self.verificationErrors = []
        self.accept_next_alert = True
    
    def test_baidu(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("kw").click()
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("Selenium")
        driver.find_element_by_id("su").click()
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True
    
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, 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()
程式碼很多,簡要分析下:

1、setup()裡面大部分熟悉,不過有些還是第一次見:

self.verificationErrors = []  #指令碼執行時,錯誤的資訊將被列印到這個列表中。
self.accept_next_alert = True  #是否繼續接受下一下警告

2、這個測試操作程式碼,熟悉:

<span style="font-family: Arial, Helvetica, sans-serif;">def test_baidu(self):</span>
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("kw").click()
        driver.find_element_by_id("kw").clear()
        driver.find_element_by_id("kw").send_keys("Selenium")
        driver.find_element_by_id("su").click()
#這個是判斷頁面是否有元素,作用不大  
def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True


4、彈窗異常處理:
    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True

5、關閉警告和對得到的文字框的處理:
    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

6、teardown()下的程式碼:self.assertEqual([], self.verificationErrors) 是個難點,對前面verificationErrors方法獲得的列表進行比較;如查verificationErrors的列表不為空,輸出列表中的報錯資訊。這個東西,也可以將來被你自己更好的呼叫和使用,根據自己的需要寫入你希望的資訊。
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)