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的模块中添加一个新的,我使用的是最新的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.(RemoteWebDriver.java:137)
	at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:184)
	at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:171)
	at org.openqa.selenium.chrome.ChromeDriver.(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+部分问题解决)