繼上篇《web自動化測試(1):爲何選擇selenium作自動化測試》,本文介紹如selenium使用css
前端相關技術:HTML、XML、JavaScript、TCP/IP協議等html
合適的工具選型:好比selenium,好比UTF等;前端
編程語言:selenium支持多種語言,java、C++、python、JavaScript等java
需求分析:項目類型,特質,生命週期,是否適合開展自動化測試等;python
若是隻是作UI差別與還原度對比,用不着selenium大駕,phantomjs截圖對比就好,推薦一個基於dom diff算法UI監控工具:page-monitorjquery
原本準備萬字長文,好好嗑叨一番,可是,以爲實在務必要。官網的介紹都很細:https://selenium.dev/documentation/en/,中文把en改成zh-cn便可:https://selenium.dev/documentation/zh-cn/git
慕課網教程:Java Web自動化測試 Selenium基礎到企業實際應用 https://coding.imooc.com/class/359.htmlgithub
selenium如今官網分爲三件套:web
Selenium IDE:是嵌入到瀏覽器的插件(目前僅支持chrome、Firefox),錄製和回放Selenium腳本,錄製好的腳本轉換成各類Selenium WebDriver支持的程序語言,進而擴展到更普遍的瀏覽器類型;ajax
Selenium WebDriver:可支持多種語言,用於操做瀏覽器的一套API;支持各種型瀏覽器,跨操做系統;
Selenium Grid:用於遠程控制、分佈式部署等,都可實現Selenium腳本的高效執行與拓展;使得自動化測試能夠並行運行,甚至在跨平臺、異構的環境中運行,包括主流的移動端環境,如Android、iOS
關鍵是chrome因爲偉大的牆存在,下載不了chrome Selenium IDE擴展插件,若有須要,去本人的收集列表看看《Chrome擴展程序導出備份與本地導入瀏覽器 》
首先是Selenium IDE錄製頁面操做腳本,這個其實不作測試,平時玩着也蠻有趣的,操做以下
一、點擊Selenium IDE插件,彈出界面,新建工程,輸入測試用例名,點擊ok,打開一個新窗口,(後面默認開始錄製,你全部的操做沒有按中止前,都是在錄製轉態)
二、再次點擊,Selenium IDE插件,彈出界面,關閉錄製按鈕
三、操做界面。
這裏的圖很草。其實能夠隨便玩。像小孩子玩手機同樣,左monkey測試,點多了天然會了
Selenium 的核心是 WebDriver,這是一個編寫指令集的接口,能夠在許多瀏覽器中互換運行。
它容許用戶模擬終端用戶執行的常見活動;雖然 Selenium 主要用於網站的前端測試,但其核心是瀏覽器用戶代理庫。
webDrive直接下便可,https://selenium.dev/downloads/,前端,npm i selenium-webdriver
接着就是編寫腳本,也能夠從上面Selenium IDE
// 不論是java 仍是python,都是須要導入webdriver包 // 庫下載 https://selenium.dev/documentation/zh-cn/selenium_installation/installing_selenium_libraries/ const {Builder, By, Key, until} = require('selenium-webdriver'); (async function example() { let driver = await new Builder().forBrowser('chrome').build(); try { // 控制瀏覽器:https://selenium.dev/documentation/zh-cn/webdriver/browser_manipulation/ // 打開瀏覽器,WebDriver一般能夠說有一個阻塞API。web平臺本質上是異步的,因此WebDriver不跟蹤DOM的實時活動狀態。 await driver.get('https://www.zhoulujun.cn/search.html?m=search&c=index&a=init&siteid=1&typeid=1&ajax=1&q=tar'); // 找到搜索框(也能夠 By.id('bdcsMain')),輸入test,回車。 await driver.findElement(By.name('q')).sendKeys('test', Key.ENTER); // 搜索頁面,找到 let firstResult = await driver.wait(until.elementLocated(By.css('h3>div')), 10000); console.log(await firstResult.getAttribute('textContent')); // 關閉瀏覽器 browser.close() } finally{ driver.quit(); } })();
webDrive庫的安裝:https://selenium.dev/documentation/zh-cn/selenium_installation/installing_selenium_libraries/
webDrive驅動安裝:https://selenium.dev/documentation/zh-cn/selenium_installation/installing_webdriver_binaries/
若是不安裝驅動,就會報錯:The ChromeDriver could not be found on the current PATH. Please download the latest version of the
chrome驅動以下:http://chromedriver.storage.googleapis.com/80.0.3987.106/chromedriver_win32.zip
查找web元素,是selenium測試的重點,https://selenium.dev/documentation/zh-cn/webdriver/web_element/
By.id
By.name
By.tagName
By.className
By.xpath //經常使用
By.css("#kw")
By.partialLinkText //模糊匹配
By.linkText("糯米") eg:<a>糯米<a/>
其實也就是咱們常見的 css jquery 元素選擇器而已。
browser.find_element_by_css_selector('.logo')
這個去查API,或者查找browser方法
獲取元素後,能夠對頁面元素執行相應的動做
input.clear()
input.send_keys('zhoulujun',Key.ENTER)
input.click()
也能夠寫js執行
browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
browser.execute_script('alert("To Bottom")')
Grid服務網格, 一臺服務器做爲轉發器(hub)將JSON格式的測試命令轉發到1臺或多臺註冊的節點。 測試任務經過跟轉發器(hub)的交互來操做遠端瀏覽器實例。 轉發器(hub)維護了一個可供使用的註冊服務器列表,也容許咱們經過轉發器(hub)來控制這些實例。容許咱們在多臺節點服務器上並行執行測試, 同時也中心化的管理多個瀏覽器版本,多種瀏覽器的配置。(以替代傳統的基於我的的測試)
按照官方的說法,下載 selenium-server-standalone JAR 文件,而後放在Tomcat起個服務就可。具體怎麼配置,有空了再來寫一篇。
同行文章推薦:
如何進行前端自動化測試? https://www.zhihu.com/question/29922082/answer/189594079
使用 Selenium 實現基於 Web 的自動化測試 https://www.ibm.com/developerworks/cn/web/1209_caimin_seleniumweb/index.html參
Selenium自動化測試入門(基於Python)http://www.javashuo.com/article/p-sxafxjlp-c.html