關(guān)于一些前提準備條件和說明
支持Chrome、Firefox、IE、Phantomjs等瀏覽器
需要下載對應(yīng)瀏覽器的webDriver,其中Phantomjs無頭瀏覽器不需要下載webdriver,其優(yōu)點是不需要加載網(wǎng)頁,速度比較快,F(xiàn)irefox有些條件下可以不用driver
這里引入常見的driver下載地址,Chrome webdriver下載地址一...
為防止官方下載地址失效,這里備份到文件服務(wù)器70.0~73.0 Chrome版本(同一大版本,基本兼容小版本) Chrome webDrive下載地址二...
火狐瀏覽器Firefox WebDriver IE瀏覽器IE webDriver方法1:webDriver需要放在瀏覽器安裝目錄下(執(zhí)行文件同級目錄,并且加入環(huán)境變量)。 方法2:直接將webDriver拷貝到腳本同級目錄下執(zhí)行。方法3:
導(dǎo)入os包,將腳本執(zhí)行目錄切換到webDriver的目錄下(os.chdir(path))Phantomjs瀏覽器安裝,下載地址,win下解壓zip包,將bin目錄加到系統(tǒng)環(huán)境變量,在命令行輸入phantomjs,既可看到。
Phantomjs使用教程安裝selenium,基于Python的selenium安裝,
pip install selenium,查看版本pip show selenium-
最新Firefox和Seleniu兼容有問題,相關(guān)解決方案:
- 安裝舊版本Firefox47,同時安裝插件firebug和firepath(寫爬蟲很有用),這里需要關(guān)閉火狐瀏覽器的自動更新功能(百度查找方法),
Firefox下載地址 - Selenium3以前的版本,使用firefox不需要webdriver,3.x后的版本需要安裝webdriver,下載地址上有說明
- 安裝舊版本Firefox47,同時安裝插件firebug和firepath(寫爬蟲很有用),這里需要關(guān)閉火狐瀏覽器的自動更新功能(百度查找方法),
-
結(jié)合的測試框架(基于Python):
- unittest: 一般python環(huán)境自帶,示例代碼:
import unittest def fun(x): return x + 1 class MyTest(unittest.TestCase): def test(self): self.assertEqual(fun(3), 4)使用說明詳細
常見斷言:#msg:判斷不成立時需要反饋的字符串 assertEqual(self, first, second, msg=None) --判斷兩個參數(shù)相等:first == second assertNotEqual(self, first, second, msg=None) --判斷兩個參數(shù)不相等:first != second assertIn(self, member, container, msg=None) --判斷是字符串是否包含:member in container assertNotIn(self, member, container, msg=None) --判斷是字符串是否不包含:member not in container assertTrue(self, expr, msg=None) --判斷是否為真:expr is True assertFalse(self, expr, msg=None) --判斷是否為假:expr is False assertIsNone(self, obj, msg=None) --判斷是否為None:obj is None assertIsNotNone(self, obj, msg=None) --判斷是否不為None:obj is not None- unittest2: unittest升級版,詳細文檔 https://pypi.org/project/unittest2/
- py.test:
Selenium + Pytest + Allure
pytest+allure2+jenkins
Python&Selenium&pytest借助allure生成自動化測試報告 - Nose: Nose是對unittest的擴展,使得python的測試更加簡單。nose自動發(fā)現(xiàn)測試代碼并執(zhí)行,nose提供了大量的插件,比如測試輸出的xUnitcompatible,覆蓋報表等等
nose的詳細文檔:https://nose.readthedocs.org/en/latest/
注意:nose本身是支持python3的,但是很多它的插件不支持 - tox:
最大的特色,是自動最測試環(huán)境的管理以及使用多個解析器配置進行測試。
tox的詳細文檔:http://testrun.org/tox/latest/
Selenium介紹,來源: selenium中文文檔
注意!??!如果使用遠程webDriver,需要安裝selenium服務(wù)器:
- 需要Java Runtime Environment (JRE) 1.6或者更高的版本是推薦的運行環(huán)境。
- server下載地址:下載地址1,我的服務(wù)器下載地址2
- 針對以及裝好java環(huán)境和下載好了server的win系統(tǒng),命令行運行:
java -jar selenium-server-standalone-2.x.x.jar(這里我沒有成功,可能是java環(huán)境沒裝好,什么時候用得到遠程再來折騰)
linux下/path/to/java -jar /path/to/selenium-server-standalone-2.x.x.jar
1. 打開一個頁面
導(dǎo)入selenium包后綁定對應(yīng)瀏覽器的webdriver
driver.get("http://www.zhihu.com")-
頁面元素查找
find_element_by_name()find_element_by_class_name()-
find_element_by_xpath()這種定位方式參考 xpath語法一般瀏覽器自帶的源代碼檢查可以copy xpath格式 -
find_element_by_css_selector()參考CSS語法 selector語法一般瀏覽器自帶的源代碼檢查可以copy selector格式 -
find_element_by_tag_name ()標簽名定位 -
find_element_by_link_text()find_element_by_partial_link_text()二者都是點位超鏈接,第一種需要定位完整的超鏈接字段,第二種可以匹配部分超鏈接字段 find_element(By.ID,"kw")find_element(By.NAME,"wd")find_element(By.CLASS_NAME,"s_ipt")find_element(By.TAG_NAME,"input")find_element(By.LINK_TEXT,u"新聞")find_element(By.PARTIAL_LINK_TEXT,u"新")find_element(By.XPATH,"http://*[@class='bg s_btn']")find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su")find_elements_by_id()find_elements_by_name()find_elements_by_class_name()find_elements_by_tag_name()find_elements_by_link_text()find_elements_by_partial_link_text()find_elements_by_xpath()find_elements_by_css_selector()
-
頁面交互
-
element.send_keys("some text")發(fā)送關(guān)鍵詞到input -
element.send_keys(" and some", Keys.ARROW_DOWN)自帶的鍵盤模擬類執(zhí)行enter -
.click選擇 -
.submit提交表單
-