1. 程式人生 > >Selenium Webdriver學習記錄(一):環境搭建(Java+Maven+Eclipse+Selenium3.x)+第一個測試demo+部分問題解決

Selenium Webdriver學習記錄(一):環境搭建(Java+Maven+Eclipse+Selenium3.x)+第一個測試demo+部分問題解決

1.Selenium的學習網站:

    官網:http://www.seleniumhq.org/docs/

    中文網站:易百教程-->Selenium教程:http://www.yiibai.com/selenium/

2.搭建環境

    準備:安裝了Maven外掛的Eclipse

    (1).新建一個Maven工程:File->New->Project,然後選擇Maven Project,如下圖:


然後一路Next(選擇預設工作區間workspace location),直到下一個介面,需要輸入Group Id和Artifact Id,按自己喜好,一般Artifact Id會成為工程名,Group Id和Artifact Id會組成包名,例如我填寫的如下左圖,最後生成的工程目錄如下右圖所示:

                       

 (2).修改pom.xml檔案,為工程新增selium依賴包,在預設的pom.xml的<dependencies>模組中新增一個新的<dependency>,我使用的是最新的selenium,已經到3.4.0了,如果又有更新了,你可以在http://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java裡找到最新版本,如下圖所示:


選擇3.4.0,進入可以看到該版本的相關詳細資訊,並且在下方它給我們提供了maven支援的新增dependence的配置,我們可以直接複製然後貼上到pom.xml中去:


新增到pom.xml中後,直接點選“儲存”,Maven就會自動去下載和拉取相應的依賴庫。

3.編寫第一個測試demo

    編寫第一個測試demo,在src/main/java/selenium/WebDriverTest/下有個預設的類App.java,現在我們在這個目錄下新建一個我們自己的java類,我取名為Demo1,在Demo1中我使用了ChromeDriver,程式碼如下(程式碼中有註釋):

package selenium.WebDriverTest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Demo1 {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//如果測試的瀏覽器沒有安裝在預設目錄,那麼必須在程式中設定 
		//bug1:System.setProperty("webdriver.chrome.driver", "C://Program Files (x86)//Google//Chrome//Application//chrome.exe");
		//bug2:System.setProperty("webdriver.chrome.driver", "C://Users//Yoga//Downloads//chromedriver_win32//chromedriver.exe");
		System.setProperty("webdriver.chrome.driver", "D://tanzhenTest//chromedriver_win32//chromedriver.exe");
		
		WebDriver driver = new ChromeDriver();
		driver.get("https://www.baidu.com");
		// 獲取 網頁的 title  
        System.out.println("The testing page title is: " + driver.getTitle()); 
        
		driver.quit();
	}
}

然後執行,會彈出chrome瀏覽器的介面,然後馬上變成百度首頁,並在控制檯如程式要求有以下輸出:



程式執行成功!

4.問題解決

在進行第3步- 編寫第一個測試demo時我遇到以下一些問題,並加以解決,如果你遇到類似問題,希望可以幫到你。

(1).雖然chrome正常啟動了,但是並沒有執行get百度首頁的操作,控制檯當然也沒有列印資訊,報錯資訊如下:

[12140:12000:0605/224013.947:ERROR:cache_util_win.cc(20)] Unable to move the cache: 5
[12140:12000:0605/224013.947:ERROR:cache_util.cc(134)] Unable to move cache folder C:\Users\Yoga\AppData\Local\Google\Chrome\User Data\ShaderCache\GPUCache to C:\Users\Yoga\AppData\Local\Google\Chrome\User Data\ShaderCache\old_GPUCache_000
[12140:12000:0605/224013.947:ERROR:disk_cache.cc(132)] Unable to create cache
[12140:12000:0605/224013.947:ERROR:shader_disk_cache.cc(593)] Shader Cache Creation failed: -2
Exception in thread "main" org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start.
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'LENOVO-PC', ip: '192.168.56.1', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_51'
Driver info: driver.version: ChromeDriver
	at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:193)
	at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:181)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
	at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)
	at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:184)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:171)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:124)
	at selenium.WebDriverTest.Demo1.main(Demo1.java:17)
Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:25606/status] to be available after 20012 ms
	at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:107)
	at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:190)
	... 10 more
Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException
	at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:140)
	at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:80)
	... 11 more
Caused by: java.util.concurrent.TimeoutException
	at java.util.concurrent.FutureTask.get(Unknown Source)
	at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:128)
	... 12 more

從報錯可以看出chromedriver無法呼叫,因為我筆記本上裝有chrome瀏覽器,所以一開始我以為在配置測試的瀏覽器時System.setProperty()中填入的就是我筆記本上chrome的安裝位置,即最開始程式碼中是這樣的:System.setProperty("webdriver.chrome.driver", "C://Program Files (x86)//Google//Chrome//Application//chrome.exe");經查閱得知selenium操作chrome瀏覽器是通過chromedriver這樣一個東西,所以還必須下載對應的瀏覽器driver,然後配置時使用這個就行了,即程式碼改為:System.setProperty("webdriver.chrome.driver", "D://tanzhenTest//chromedriver_win32//chromedriver.exe");。下載chromedriver的網址在:https://chromedriver.storage.googleapis.com/index.html

(2).當我將System.setProperty中的chrome.exe換成chromedriver.exe以後,執行程式,同樣彈出了chrome瀏覽器,但依然不能訪問百度,瀏覽器上出現了類似“xxx不安全xxx”字樣,控制檯有以下錯誤:

Starting ChromeDriver (v2.9.248315) on port 42166
六月 05, 2017 10:55:37 下午 org.openqa.selenium.remote.ProtocolHandshake createSession
資訊: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.WebDriverException: java.net.SocketException: Connection reset
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'LENOVO-PC', ip: '192.168.56.1', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_51'
Driver info: driver.version: RemoteWebDriver
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:91)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
	at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:364)
	at selenium.WebDriverTest.Demo1.main(Demo1.java:18)
Caused by: java.net.SocketException: Connection reset
	at java.net.SocketInputStream.read(Unknown Source)
	at java.net.SocketInputStream.read(Unknown Source)
	at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
	at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
	at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:282)
	at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
	at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
	at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
	at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
	at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:165)
	at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
	at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
	at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
	at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
	at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
	at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
	at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
	at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:139)
	at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:87)
	at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:161)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
	... 3 more

或者這樣的錯誤:
unknown error: Runtime.executionContextCreated has invalid ‘context’: {“auxData”:{“frameId”:”11740.1”,”isDefault”:true},”id”:1,”name”:”“,”origin”:”://”} 
(Session info: chrome=xx.xx 
(Driver info: chromedriver=2.9.248307,platform=Windows xx) 
則原因是chromedriver和chrome版本不匹配,老版本的chromedriver無法正常啟動chrome,解決辦法還是在這個網址上下載最新的chromedriver:https://chromedriver.storage.googleapis.com/index.html,注意這裡chromedriver不是按版本順序排列的,我開始以為是,就下載了最下面的版本是2.9,結果上面還有更新的版本,最新到2.29,如下圖所示:


在notes.txt中可以詳細說明了各個版本的chromedriver和chrome瀏覽器的配套關係,查閱notes.txt可以知道例如2.29版本的driver就適配至chrome 58

----------ChromeDriver v2.29 (2017-04-04)----------
Supports Chrome v56-58
Resolved issue 1521: Assignment to Object.prototype.$family causes a crash [['Pri-1']]
Resolved issue 1482: Chromedriver cannot handle the alert generated by onbeforeunload event [['OS-All', 'Pri-2']]
Resolved issue 1315: |switch_to.window| does not visually switch tabs [['OS-All', 'Pri-3']]
換成最新的2.29版本的chromedriver後,上述問題就解決了。

相關推薦

Selenium Webdriver學習記錄環境搭建Java+Maven+Eclipse+Selenium3.x+第一測試demo+部分問題解決

1.Selenium的學習網站:     官網:http://www.seleniumhq.org/docs/     中文網站:易百教程-->Selenium教程:http://www.yiibai.com/selenium/ 2.搭建環境     準備:安裝了Ma

vue學習環境搭建

因為最近在做移動端html5的專案,專案中的資料渲染用的template,js ,這種模板的好處在於至少不用在js中去拼接字串來渲染頁面,但是缺點也很明顯,難以修改和格式化,並且會破壞編輯時頁面的佈局,需要做到很細的劃分,不然一旦頁面有所改變,真的改的頭疼。而現在頁面載入的

Liferay 6.1開發學習環境搭建

一、資源準備 liferay-plugins-sdk-6.1.1(Plugins SDK)liferay-portal-6.1.1-ce-ga2(Bundled with Tomcat)liferay-portal-src-6.1.1-ce-ga2(Portal Sour

Python+Selenium - Web自動化測試環境搭建

清單列表:   Python 3x Selenium Chrome Pycharm 一、Python的安裝:   Python官網下載地址:https://www.python.org/   1. 

[Python接口自動化]從零開始學習python自動化1環境搭建

help ins cnblogs 文件中 ssi 空格 plugins 變量 mod 第一步:安裝python編譯環境 安裝python編譯環境之前,必須保證已安裝jdk哈,如果為安裝,請參考https://jingyan.baidu.com/article/6dad507

Stuts2學習筆記1環境搭建Demo

原始碼: github: https://github.com/liaotuo/Struts2-Demo/tree/master/struts2-demo 環境搭建 下載struts2 官網下載:http://mirror.bit.edu.cn/apache/struts

Spring Boot / Spring MVC 入門實踐 環境搭建第一專案

宣告 本系列文章系本人原創,歡迎轉載,轉載請註明出處。 本系列文章通過具體的例子,介紹如何通過Spring Boot來幫助簡化Spring MVC開發Web Application的過程,旨在通過具體的實踐,使讀者能夠入門利用Spring Boot開發Web

CUDA從入門到精通環境搭建

NVIDIA於2006年推出CUDA(Compute Unified Devices Architecture),可以利用其推出的GPU進行通用計算,將平行計算從大型叢集擴充套件到了普通顯示卡,使得使用者只需要一臺帶有Geforce顯示卡的筆記本就能跑較大規模的並行處理程式

hadoop學習之路---叢集環境搭建2.7.3版本

三:下載解壓 hadoop 到某個目錄(例如 /usr/loacl/hadoop) 四:賬號建立: 即為hadoop叢集專門設定一個使用者組及使用者,這部分比較簡單,參考示例如下: groupadd hadoop //設定h

Solr學習筆記 ---Solr執行環境搭建tomcat7 + solr 3.5 +ubuntu 10.04

一、軟體下載 二、軟體安裝   1、安裝Tomcat7, 解壓apache-tomcat-7.0.26.tar.gz,增加環境變數      export $CATALINA_HOME=apache-tomcat-7.0.26    export $CATALINA_B

activiti自定義流程之Spring整合activiti-modeler5.16例項環境搭建

專案中需要整合activiti-modeler自定義流程,找了很多資料後,終於成功的跳轉到activiti-modeler流程設計介面,以下是記錄: 一、整合基礎:eclipse4.4.1、tomcat7、jdk1.7、mysql5.6.25、maven3.2.5、acti

python學習1環境搭建python3.X

num pytho 計算機 nbsp 變量名 搭建 idt http mic 1.安裝包下載 python官網:https://www.python.org/downloads/ 2.安裝(windows) win7和win10都可以安裝。安裝包下載後

安卓JNI精細化講解,讓你徹底瞭解JNI環境搭建與HelloWord

目錄 1、基礎概念 ├──1.1、JNI ├──1.2、NDK ├──1.3、CMake與ndk-build 2、環境搭建 3、Native C++ 專案(HelloWord案例) ├── 3.1、專案建立(java、kotlin) ├── 3.2、CMake的應用(詳細講解) ├── 3.3、ndk-bui

從零開始學習音視頻編程技術 開發環境搭建Qt4.86手動設置環境,主要就是設置g++和qmake,比較透徹,附下載鏈接

路徑 details 分享 baidu 末尾 是我 其中 找到 source 1.先下載安裝Qt 我們使用的版本是4.8。 可以自行百度下載也可以從下面的網盤地址下載: Qt庫和編譯器下載: 鏈接:http://pan.baidu.com/s/1hrUxLIG 密碼

、OpenCV環境搭建採用官方已經編譯好的

一、下載OpenCV安裝包: 到OpenCV的官網(http://opencv.org/)下載最新版本的OpenCV安裝包,由於OpenCV針對不同平臺都有安裝程式,所以我們只需要根據當前開發環境選擇合適的平臺版本即可,這裡我們是在win7 64bit作業系統下進行開發,所以應該下載Open

比特幣BTC原始碼分析0環境搭建

一、Bitcoin編譯執行啟動過程 1、從Github上clone bitcoin原始碼 至本地 ~/go/src/github.com/bitcoin$git clone https://github.com/bitcoin/bitcoin.git Cloning into 'bi

Linux學習 LAMP 環境搭建還有apache不能解析php問題

Ubuntu下面的LAMP環境搭建: 先說一下Ubuntu下面一些檔案的目錄: Debian, Ubuntu (Apache httpd 2.x): ServerRoot          &

Python札記-開發環境搭建ForMac和第一個Python程式

 今天的內容大致如下: 自己先看一下Python的功效,多多少少去了解一下。我們必須安裝一個Python環境在自己的PC上,用來解釋自己書寫的程式碼,本來Mac是內建的,但是是Python2.7.10(隨著新電腦的出售和系統更新,預裝版本可能會有差異,在終端輸入Python

易學筆記-第0章環境搭建/0.1 在eclipse中加入web開發外掛並設定自動提示

在eclipse中加入web開發外掛 web開發外掛包括:HTML、CSS、JavaScript等,這些外掛可以幫助快速編寫程式碼,最明顯的幫助就是自動提示 預覽eclipse未安裝web外掛之前的配置項: 路徑:Window-->preferenc

》MyBatise 入門案例 環境搭建 與入門的案 --- 最原始 main方法的測試

封紅衛 — 友情提示: 使用 mybatis 是非常容易的一件事情,因為只需要編寫 Dao 介面並且按照 mybatis 要求編寫兩個配置檔案,就可以實現功能。遠比我們之前的 jdbc 方便多了。(我們使用註解之後,將變得 更為簡單,只需要編寫一個 my