Selenium2学习-005-WebUI自动化实战实例-003-三种浏览器(Chrome、Firefox、IE)启动脚本

此文主要通过 三种浏览器(Chrome、Firefox、IE)启动脚本 功能,进行 Selenium2 三种浏览器启动方法的实战实例讲解。文中所附源代码于 2015-01-18 20:33 亲测通过,敬请亲们阅览。进行编写登录自动化测试脚本,若您直接使用此文所附的源代码运行测试,则需要修改对应 浏览器 或 webdriver 的路径,否则将会引起相应的报错,请知悉。

希望能对初学 Selenium2 WebUI 自动化测试编程的亲们有所帮助。若有不足之处,敬请大神指正,不胜感激!

一、各浏览器 WebDriver 驱动文件下载

二、各浏览器启动脚本

当前使用的 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自动化功能测试脚本:启动 Chrome 浏览器
14  * 
15  * 实现 Chrome 浏览器启动的步骤如下:
16  *   1.设定需要启动的 Chrome 的安装路径
17  *   2.设定 Chrome 对应的 webdriver
18  *   3.启动 Chrome, 并最大化
19  *   4.打开百度
20  *   5.关闭并退出
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;   // 百度首页网址
28 
29     /**
30      * 主方法入口
31      * @param args
32      */
33     public static void main(String[] args) {
34         /* 启动 chrome */
35         chromeStart();
36         /* 打开百度 */
37         cd.get(baseUrl);
38         /* 等待加载 */
39         cd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
40         /* 关闭 chrome */
41         chromeQuit();
42     }
43     
44     /**
45      * Chrome WebDriver 设置, 网址及搜索内容初始化, 打开 Chrome 浏览器
46      */
47     public static void chromeStart(){
48         /* 设定 chrome 启动文件的位置, 若未设定则取默认安装目录的 chrome */
49         System.setProperty("webdriver.chrome.bin", "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
50         /* 设定 chrome webdirver 的位置 */
51         System.setProperty("webdriver.chrome.driver", "C:/Windows/System32/chromedriver.exe");
52         /* 百度首页网址赋值 */
53         baseUrl = "http://www.baidu.com/";
54         /* 启动 chrome 浏览器 */
55         cd = new ChromeDriver();
56         /* 浏览器最大化 */
57         cd.manage().window().maximize();
58     }
59     
60     /**
61      * 关闭并退出 Chrome
62      */
63     public static void chromeQuit(){
64         /* 关闭 chrome */
65         cd.close();
66         /* 退出 chrome */
67         cd.quit();
68     }
69 }
View Code
  • 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自动化功能测试脚本:启动 Firefox 浏览器
14  * 
15  * 实现 Firefox 浏览器启动的步骤如下:
16  *   1.设定需要启动的 Firefox 的安装路径
17  *   2.启动 Firefox, 并最大化
18  *   3.打开百度
19  *   4.关闭并退出 
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;   // 百度首页网址
27 
28     /**
29      * Firefox WebDriver 设置, 网址及搜索内容初始化, 打开 Firefox 浏览器
30      */
31     public static void FirefoxStart(){
32         /* 设定 Firefox 启动文件的位置, 若未设定则取默认安装目录的 FirefoxQuit */
33 //        System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
34         /* 设定 Firefox webdirver 的位置, Selenium 提供了对 Firefox 的原生支持,因而不要设定其 webdriver */
35         
36         /* 百度首页网址赋值 */
37         baseUrl = "http://www.baidu.com/";
38         /* 启动 Firefox 浏览器 */
39         ff = new FirefoxDriver();
40         /* 浏览器最大化 */
41         ff.manage().window().maximize();
42     }
43     
44     /**
45      * @function 测试主入口
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         /* 启动 Firefox */
54         FirefoxStart();
55         /* 打开百度 */
56         ff.get(baseUrl);
57         
58         /* 等待加载 */
59         ff.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
60         
61         /* 关闭 Firefox */
62 //        FirefoxQuit();
63     }
64     
65     /**
66      * 关闭并退出 Firefox
67      */
68     public static void FirefoxQuit(){
69         /* 关闭 Firefox */
70         ff.close();
71         /* 退出 Firefox */
72         ff.quit();
73     }
74 }
View Code
  • 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自动化功能测试脚本:启动  InternetExplorer 浏览器
15  * 
16  * 实现 InternetExplorer 浏览器启动的步骤如下:
17  *   1.设定需要启动的 InternetExplorer 的安装路径
18  *   2.设定 InternetExplorer 对应的 webdriver
19  *   3.设定 InternetExplorerDriver 启动参数
20  *   4.启动 InternetExplorer, 并最大化
21  *   5.打开百度
22  *   6.关闭并退出
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;  // 百度网址
30     
31     /**
32      * 设定系统环境, 启动 IE 浏览器
33      */
34     public static void ieStart(){
35         /* 设定 IE 浏览器启动文件路径 */
36         System.setProperty("webdriver.ie.bin", "C:/Program Files/Internet Explorer/iexplore.exe");
37         /* 设定 IEDriverServer 文件路径 */
38         System.setProperty("webdriver.ie.driver", "c:/windows/system32/IEDriverServer.exe");
39         
40         /* 设定百度网址 */
41         baseUrl = "http://www.baidu.com";
42         
43         /* 设定  InternetExplorerDriver 参数, 忽略安全验证, 忽略后测试脚本将不稳定或难于调试 */
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         /* 启动 IE 浏览器 */
58         ieStart();
59         /* 打开百度网址 */
60         ie.get(baseUrl);
61         
62         /* 等待加载 */
63         ie.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
64         
65         /* 退出并关闭 IE 浏览器 */
66         ieQuit();
67     }
68     
69     /**
70      * 退出并关闭 IE 浏览器
71      */
72     public static void ieQuit(){
73         ie.close();
74         ie.quit();
75     }
76 }
View Code

 若将第 44 - 46 行(忽略浏览器设定的安全域验证)注销,改为 ie = new InternetExplorerDriver(); 则运行脚本时无法通过浏览器设定的安全域验证,会提示如下报错信息:

技术分享
 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)
View Code

 

至此,WebUI 自动化功能测试脚本第 003 篇-三种浏览器(Chrome、Firefox、IE)启动脚本 顺利完结,希望此文能够给初学 Selenium 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。