此文主要通過(guò) ?三種瀏覽器(Chrome、Firefox、IE)啟動(dòng)腳本 ?功能,進(jìn)行 Selenium2 三種瀏覽器啟動(dòng)方法的實(shí)戰(zhàn)實(shí)例講解。文中所附源代碼于 2015-01-18 20:33 親測(cè)通過(guò),敬請(qǐng)親們閱覽。進(jìn)行編寫登錄自動(dòng)化測(cè)試腳本,若您直接使用此文所附的源代碼運(yùn)行測(cè)試,則需要修改對(duì)應(yīng) 瀏覽器 或 webdriver 的路徑,否則將會(huì)引起相應(yīng)的報(bào)錯(cuò),請(qǐng)知悉。
希望能對(duì)初學(xué) Selenium2 WebUI 自動(dòng)化測(cè)試編程的親們有所幫助。若有不足之處,敬請(qǐng)大神指正,不勝感激!
一、各瀏覽器 WebDriver 驅(qū)動(dòng)文件下載
- Chrome 驅(qū)動(dòng)文件(瀏覽器版本 31 下測(cè)試通過(guò)): http://yunpan.cn/cyadeJIxkwWwg ?提取碼 e475
- Internet Explorer 驅(qū)動(dòng)文件下載(瀏覽器版本 11 下測(cè)試通過(guò)): http://yunpan.cn/cyadeJIxkwWwg ?提取碼 e475
- Firefox 為 Selenium 原生支持,無(wú)需下載 webdriver
二、各瀏覽器啟動(dòng)腳本
當(dāng)前使用的 Selenium Jar 文件為:selenium-server-standalone-2.42.2.jar
- Chrome
1
/**
2
* Aaron.ffp Inc.
3
* Copyright (c) 2004-2015 All Rights Reserved.
4
*/
5
package
main.java.aaron.sele.demo;
6
7
import
java.util.concurrent.TimeUnit;
8
9
import
org.openqa.selenium.WebDriver;
10
import
org.openqa.selenium.chrome.ChromeDriver;
11
12
/**
13
* UI自動(dòng)化功能測(cè)試腳本:?jiǎn)?dòng) Chrome 瀏覽器
14
*
15
* 實(shí)現(xiàn) Chrome 瀏覽器啟動(dòng)的步驟如下:
16
* 1.設(shè)定需要啟動(dòng)的 Chrome 的安裝路徑
17
* 2.設(shè)定 Chrome 對(duì)應(yīng)的 webdriver
18
* 3.啟動(dòng) Chrome, 并最大化
19
* 4.打開百度
20
* 5.關(guān)閉并退出
21
*
22
*
@author
Aaron.ffp
23
*
@version
$Id: StartBrowerChrome.java, V0.1 2015-1-18 15:07:49 Aaron.ffp Exp $
24
*/
25
public
class
StartBrowerChrome {
26
private
static
WebDriver cd;
27
private
static
String baseUrl;
//
百度首頁(yè)網(wǎng)址
28
29
/**
30
* 主方法入口
31
*
@param
args
32
*/
33
public
static
void
main(String[] args) {
34
/*
啟動(dòng) chrome
*/
35
chromeStart();
36
/*
打開百度
*/
37
cd.get(baseUrl);
38
/*
等待加載
*/
39
cd.manage().timeouts().implicitlyWait(5
, TimeUnit.SECONDS);
40
/*
關(guān)閉 chrome
*/
41
chromeQuit();
42
}
43
44
/**
45
* Chrome WebDriver 設(shè)置, 網(wǎng)址及搜索內(nèi)容初始化, 打開 Chrome 瀏覽器
46
*/
47
public
static
void
chromeStart(){
48
/*
設(shè)定 chrome 啟動(dòng)文件的位置, 若未設(shè)定則取默認(rèn)安裝目錄的 chrome
*/
49
System.setProperty("webdriver.chrome.bin", "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
);
50
/*
設(shè)定 chrome webdirver 的位置
*/
51
System.setProperty("webdriver.chrome.driver", "C:/Windows/System32/chromedriver.exe"
);
52
/*
百度首頁(yè)網(wǎng)址賦值
*/
53
baseUrl = "http://www.baidu.com/"
;
54
/*
啟動(dòng) chrome 瀏覽器
*/
55
cd =
new
ChromeDriver();
56
/*
瀏覽器最大化
*/
57
cd.manage().window().maximize();
58
}
59
60
/**
61
* 關(guān)閉并退出 Chrome
62
*/
63
public
static
void
chromeQuit(){
64
/*
關(guān)閉 chrome
*/
65
cd.close();
66
/*
退出 chrome
*/
67
cd.quit();
68
}
69
}
- Firefox
1
/**
2
* Aaron.ffp Inc.
3
* Copyright (c) 2004-2015 All Rights Reserved.
4
*/
5
package
main.java.aaron.sele.demo;
6
7
import
java.util.concurrent.TimeUnit;
8
9
import
org.openqa.selenium.WebDriver;
10
import
org.openqa.selenium.firefox.FirefoxDriver;
11
12
/**
13
* UI自動(dòng)化功能測(cè)試腳本:?jiǎn)?dòng) Firefox 瀏覽器
14
*
15
* 實(shí)現(xiàn) Firefox 瀏覽器啟動(dòng)的步驟如下:
16
* 1.設(shè)定需要啟動(dòng)的 Firefox 的安裝路徑
17
* 2.啟動(dòng) Firefox, 并最大化
18
* 3.打開百度
19
* 4.關(guān)閉并退出
20
*
21
*
@author
Aaron.ffp
22
*
@version
$Id: StartBrowerFirefox.java, V0.1 2015-1-18 15:08:46 Aaron.ffp Exp $
23
*/
24
public
class
StartBrowerFirefox {
25
private
static
WebDriver ff;
26
private
static
String baseUrl;
//
百度首頁(yè)網(wǎng)址
27
28
/**
29
* Firefox WebDriver 設(shè)置, 網(wǎng)址及搜索內(nèi)容初始化, 打開 Firefox 瀏覽器
30
*/
31
public
static
void
FirefoxStart(){
32
/*
設(shè)定 Firefox 啟動(dòng)文件的位置, 若未設(shè)定則取默認(rèn)安裝目錄的 FirefoxQuit
*/
33
//
System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
34
/*
設(shè)定 Firefox webdirver 的位置, Selenium 提供了對(duì) Firefox 的原生支持,因而不要設(shè)定其 webdriver
*/
35
36
/*
百度首頁(yè)網(wǎng)址賦值
*/
37
baseUrl = "http://www.baidu.com/"
;
38
/*
啟動(dòng) Firefox 瀏覽器
*/
39
ff =
new
FirefoxDriver();
40
/*
瀏覽器最大化
*/
41
ff.manage().window().maximize();
42
}
43
44
/**
45
* @function 測(cè)試主入口
46
* @description
47
*
@author
Aaron.ffp
48
*
@version
V0.1: main, 2015年1月19日 下午5:26:05 Aaron.ffp Exp $
49
*
50
*
@param
args
51
*/
52
public
static
void
main(String[] args) {
53
/*
啟動(dòng) Firefox
*/
54
FirefoxStart();
55
/*
打開百度
*/
56
ff.get(baseUrl);
57
58
/*
等待加載
*/
59
ff.manage().timeouts().implicitlyWait(5
, TimeUnit.SECONDS);
60
61
/*
關(guān)閉 Firefox
*/
62
//
FirefoxQuit();
63
}
64
65
/**
66
* 關(guān)閉并退出 Firefox
67
*/
68
public
static
void
FirefoxQuit(){
69
/*
關(guān)閉 Firefox
*/
70
ff.close();
71
/*
退出 Firefox
*/
72
ff.quit();
73
}
74
}
- Internet Explorer
1
/**
2
* Aaron.ffp Inc.
3
* Copyright (c) 2004-2015 All Rights Reserved.
4
*/
5
package
main.java.aaron.sele.demo;
6
7
import
java.util.concurrent.TimeUnit;
8
9
import
org.openqa.selenium.WebDriver;
10
import
org.openqa.selenium.ie.InternetExplorerDriver;
11
import
org.openqa.selenium.remote.DesiredCapabilities;
12
13
/**
14
* UI自動(dòng)化功能測(cè)試腳本:?jiǎn)?dòng) InternetExplorer 瀏覽器
15
*
16
* 實(shí)現(xiàn) InternetExplorer 瀏覽器啟動(dòng)的步驟如下:
17
* 1.設(shè)定需要啟動(dòng)的 InternetExplorer 的安裝路徑
18
* 2.設(shè)定 InternetExplorer 對(duì)應(yīng)的 webdriver
19
* 3.設(shè)定 InternetExplorerDriver 啟動(dòng)參數(shù)
20
* 4.啟動(dòng) InternetExplorer, 并最大化
21
* 5.打開百度
22
* 6.關(guān)閉并退出
23
*
24
*
@author
Aaron.ffp
25
*
@version
$Id: StartBrowerIE.java, V0.1 2015-1-18 15:12:33 Aaron.ffp Exp $
26
*/
27
public
class
StartBrowerIE {
28
private
static
WebDriver ie;
29
private
static
String baseUrl;
//
百度網(wǎng)址
30
31
/**
32
* 設(shè)定系統(tǒng)環(huán)境, 啟動(dòng) IE 瀏覽器
33
*/
34
public
static
void
ieStart(){
35
/*
設(shè)定 IE 瀏覽器啟動(dòng)文件路徑
*/
36
System.setProperty("webdriver.ie.bin", "C:/Program Files/Internet Explorer/iexplore.exe"
);
37
/*
設(shè)定 IEDriverServer 文件路徑
*/
38
System.setProperty("webdriver.ie.driver", "c:/windows/system32/IEDriverServer.exe"
);
39
40
/*
設(shè)定百度網(wǎng)址
*/
41
baseUrl = "http://www.baidu.com"
;
42
43
/*
設(shè)定 InternetExplorerDriver 參數(shù), 忽略安全驗(yàn)證, 忽略后測(cè)試腳本將不穩(wěn)定或難于調(diào)試
*/
44
DesiredCapabilities ieCapabilities =
DesiredCapabilities.internetExplorer();
45
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true
);
46
ie =
new
InternetExplorerDriver(ieCapabilities);
47
48
/*
瀏覽器最大化
*/
49
ie.manage().window().maximize();
50
}
51
52
/**
53
*
54
*
@param
args
55
*/
56
public
static
void
main(String[] args) {
57
/*
啟動(dòng) IE 瀏覽器
*/
58
ieStart();
59
/*
打開百度網(wǎng)址
*/
60
ie.get(baseUrl);
61
62
/*
等待加載
*/
63
ie.manage().timeouts().implicitlyWait(5
, TimeUnit.SECONDS);
64
65
/*
退出并關(guān)閉 IE 瀏覽器
*/
66
ieQuit();
67
}
68
69
/**
70
* 退出并關(guān)閉 IE 瀏覽器
71
*/
72
public
static
void
ieQuit(){
73
ie.close();
74
ie.quit();
75
}
76
}
?若將第 44 - 46 行(忽略瀏覽器設(shè)定的安全域驗(yàn)證)注銷,改為 ie = new?InternetExplorerDriver(); 則運(yùn)行腳本時(shí)無(wú)法通過(guò)瀏覽器設(shè)定的安全域驗(yàn)證,會(huì)提示如下報(bào)錯(cuò)信息:
1
Started InternetExplorerDriver server (32-
bit)
2
2.37.0.0
3
Listening on port 38775
4
Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same
for
all zones. Enable Protected Mode must be set to the same value (enabled or disabled)
for
all zones. (WARNING: The server did not provide any stacktrace information)
5
Command duration or timeout: 1.18
seconds
6
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'
7
System info: host: 'AaronFan-PC', ip: '10.24.68.138', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0'
8
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
9
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
10
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62
)
11
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45
)
12
at java.lang.reflect.Constructor.newInstance(Constructor.java:408
)
13
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204
)
14
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:162
)
15
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599
)
16
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240
)
17
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:225
)
18
at org.openqa.selenium.ie.InternetExplorerDriver.run(InternetExplorerDriver.java:182
)
19
at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:174
)
20
at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:146
)
21
at main.java.aaron.sele.demo.StartBrowerIE.ieStart(StartBrowerIE.java:38
)
22
at main.java.aaron.sele.demo.StartBrowerIE.main(StartBrowerIE.java:47)
?
至此, WebUI 自動(dòng)化功能測(cè)試腳本 第 003 篇-三種瀏覽器(Chrome、Firefox、IE)啟動(dòng)腳本 ?順利完結(jié),希望此文能夠給初學(xué) Selenium 的您一份參考。
最后,非常感謝親的駐足,希望此文能對(duì)親有所幫助。熱烈歡迎親一起探討,共同進(jìn)步。非常感謝! ^_^
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

