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

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

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

二、各浏览器启动脚本

当前使用的 Selenium Jar 文件为:selenium-server-standalone-2.42.2.jar

  • Chrome
 /**
* Aaron.ffp Inc.
* Copyright (c) 2004-2015 All Rights Reserved.
*/
package main.java.aaron.sele.demo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; /**
* UI自动化功能测试脚本:启动 Chrome 浏览器
*
* 实现 Chrome 浏览器启动的步骤如下:
* 1.设定需要启动的 Chrome 的安装路径
* 2.设定 Chrome 对应的 webdriver
* 3.启动 Chrome, 并最大化
* 4.打开百度
* 5.关闭并退出
*
* @author Aaron.ffp
* @version $Id: StartBrowerChrome.java, V0.1 2015-1-18 15:07:49 Aaron.ffp Exp $
*/
public class StartBrowerChrome {
private static WebDriver cd;
private static String baseUrl; // 百度首页网址 /**
* 主方法入口
* @param args
*/
public static void main(String[] args) {
/* 启动 chrome */
chromeStart();
/* 打开百度 */
cd.get(baseUrl);
/* 等待加载 */
cd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
/* 关闭 chrome */
chromeQuit();
} /**
* Chrome WebDriver 设置, 网址及搜索内容初始化, 打开 Chrome 浏览器
*/
public static void chromeStart(){
/* 设定 chrome 启动文件的位置, 若未设定则取默认安装目录的 chrome */
System.setProperty("webdriver.chrome.bin", "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
/* 设定 chrome webdirver 的位置 */
System.setProperty("webdriver.chrome.driver", "C:/Windows/System32/chromedriver.exe");
/* 百度首页网址赋值 */
baseUrl = "http://www.baidu.com/";
/* 启动 chrome 浏览器 */
cd = new ChromeDriver();
/* 浏览器最大化 */
cd.manage().window().maximize();
} /**
* 关闭并退出 Chrome
*/
public static void chromeQuit(){
/* 关闭 chrome */
cd.close();
/* 退出 chrome */
cd.quit();
}
}
  • Firefox
 /**
* Aaron.ffp Inc.
* Copyright (c) 2004-2015 All Rights Reserved.
*/
package main.java.aaron.sele.demo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; /**
* UI自动化功能测试脚本:启动 Firefox 浏览器
*
* 实现 Firefox 浏览器启动的步骤如下:
* 1.设定需要启动的 Firefox 的安装路径
* 2.启动 Firefox, 并最大化
* 3.打开百度
* 4.关闭并退出
*
* @author Aaron.ffp
* @version $Id: StartBrowerFirefox.java, V0.1 2015-1-18 15:08:46 Aaron.ffp Exp $
*/
public class StartBrowerFirefox {
private static WebDriver ff;
private static String baseUrl; // 百度首页网址 /**
* Firefox WebDriver 设置, 网址及搜索内容初始化, 打开 Firefox 浏览器
*/
public static void FirefoxStart(){
/* 设定 Firefox 启动文件的位置, 若未设定则取默认安装目录的 FirefoxQuit */
// System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
/* 设定 Firefox webdirver 的位置, Selenium 提供了对 Firefox 的原生支持,因而不要设定其 webdriver */ /* 百度首页网址赋值 */
baseUrl = "http://www.baidu.com/";
/* 启动 Firefox 浏览器 */
ff = new FirefoxDriver();
/* 浏览器最大化 */
ff.manage().window().maximize();
} /**
* @function 测试主入口
* @description
* @author Aaron.ffp
* @version V0.1: main, 2015年1月19日 下午5:26:05 Aaron.ffp Exp $
*
* @param args
*/
public static void main(String[] args) {
/* 启动 Firefox */
FirefoxStart();
/* 打开百度 */
ff.get(baseUrl); /* 等待加载 */
ff.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); /* 关闭 Firefox */
// FirefoxQuit();
} /**
* 关闭并退出 Firefox
*/
public static void FirefoxQuit(){
/* 关闭 Firefox */
ff.close();
/* 退出 Firefox */
ff.quit();
}
}
  • Internet Explorer
 /**
* Aaron.ffp Inc.
* Copyright (c) 2004-2015 All Rights Reserved.
*/
package main.java.aaron.sele.demo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities; /**
* UI自动化功能测试脚本:启动 InternetExplorer 浏览器
*
* 实现 InternetExplorer 浏览器启动的步骤如下:
* 1.设定需要启动的 InternetExplorer 的安装路径
* 2.设定 InternetExplorer 对应的 webdriver
* 3.设定 InternetExplorerDriver 启动参数
* 4.启动 InternetExplorer, 并最大化
* 5.打开百度
* 6.关闭并退出
*
* @author Aaron.ffp
* @version $Id: StartBrowerIE.java, V0.1 2015-1-18 15:12:33 Aaron.ffp Exp $
*/
public class StartBrowerIE {
private static WebDriver ie;
private static String baseUrl; // 百度网址 /**
* 设定系统环境, 启动 IE 浏览器
*/
public static void ieStart(){
/* 设定 IE 浏览器启动文件路径 */
System.setProperty("webdriver.ie.bin", "C:/Program Files/Internet Explorer/iexplore.exe");
/* 设定 IEDriverServer 文件路径 */
System.setProperty("webdriver.ie.driver", "c:/windows/system32/IEDriverServer.exe"); /* 设定百度网址 */
baseUrl = "http://www.baidu.com"; /* 设定 InternetExplorerDriver 参数, 忽略安全验证, 忽略后测试脚本将不稳定或难于调试 */
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
ie = new InternetExplorerDriver(ieCapabilities); /* 浏览器最大化 */
ie.manage().window().maximize();
} /**
*
* @param args
*/
public static void main(String[] args) {
/* 启动 IE 浏览器 */
ieStart();
/* 打开百度网址 */
ie.get(baseUrl); /* 等待加载 */
ie.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); /* 退出并关闭 IE 浏览器 */
ieQuit();
} /**
* 退出并关闭 IE 浏览器
*/
public static void ieQuit(){
ie.close();
ie.quit();
}
}

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

 Started InternetExplorerDriver server (32-bit)
2.37.0.0
Listening on port 38775
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)
Command duration or timeout: 1.18 seconds
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'
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'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:162)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:225)
at org.openqa.selenium.ie.InternetExplorerDriver.run(InternetExplorerDriver.java:182)
at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:174)
at org.openqa.selenium.ie.InternetExplorerDriver.<init>(InternetExplorerDriver.java:146)
at main.java.aaron.sele.demo.StartBrowerIE.ieStart(StartBrowerIE.java:38)
at main.java.aaron.sele.demo.StartBrowerIE.main(StartBrowerIE.java:47)

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

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

Selenium2学习-005-WebUI自动化实战实例-003-三种浏览器(Chrome、Firefox、IE)启动脚本源代码的更多相关文章

  1. Selenium2学习-009-WebUI自动化实战实例-007-Selenium 8种元素定位实战实例源代码(百度首页搜索录入框及登录链接)

    此 文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,通过 ID.name.xpath.cssSelector.linkText.className.partialLinkTe ...

  2. Selenium2学习-001-Selenium2 WebUI自动化Java开发 Windows 环境配置

    此文主要介绍 Selenium2 WebUI自动化Java开发 Windows 环境配置,供各位亲们参考,若有不足之处,敬请各位大神指正,非常感谢! 所需软件列表如下所示: 所属分类 具体名称 备注 ...

  3. Selenium2学习-007-WebUI自动化实战实例-005-解决 Firefox 版本不兼容:org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary

    此文主要讲述 Java 运行 Selenium 脚本时,因 Friefox 浏览器版本与 selenium-server-standalone-x.xx.x.jar 不兼容引起的 org.openqa ...

  4. Selenium2学习-035-WebUI自动化实战实例-033-页面快照截图应用之三 -- 区域截图(专业版)

    之前有写过两篇博文讲述了 WebUI 自动化测试脚本中常用的截图方法,敬请参阅如下所示链接: 浏览器显示区域截图 浏览器指定区域截图 那么当需要截取的区域不在浏览器显示窗口范围之内时,之前的方法显然无 ...

  5. Selenium2学习-027-WebUI自动化实战实例-025-JavaScript 在 Selenium 自动化中的应用实例之三(页面滚屏,模拟鼠标拖动滚动条)

    日常的 Web UI 自动化测试过程中,get 或 navigate 到指定的页面后,若想截图的元素或者指定区域范围不在浏览器的显示区域内,则通过截屏则无法获取相应的信息,反而浪费了无畏的图片服务器资 ...

  6. Selenium2学习-039-WebUI自动化实战实例-文件上传下载

    通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...

  7. Selenium2学习-018-WebUI自动化实战实例-016-自动化脚本编写过程中的登录验证码问题

    日常的 Web 网站开发的过程中,为提升登录安全或防止用户通过脚本进行黄牛操作(宇宙最贵铁皮天朝魔都的机动车牌照竞拍中),很多网站在登录的时候,添加了验证码验证,而且验证码的实现越来越复杂,对其进行脚 ...

  8. Selenium2学习-016-WebUI自动化实战实例-014-Selenium 窗口选择

    在日常的 WebUI 自动化测试脚本编写过程中,经常需要打开新的页面,或者在多个打开的页面之间进行切换,以对页面元素进行相应的操作,以模拟用户的行为,实现 UI 的自动化测试.在过往的时间中,经常有初 ...

  9. Selenium2学习-014-WebUI自动化实战实例-012-Selenium 操作下拉列表实例-div+{js|jquery}

    之前已经讲过了 Selenium 操作 Select 实现的下拉列表:Selenium2学习-010-WebUI自动化实战实例-008-Selenium 操作下拉列表实例-Select,但是在实际的日 ...

  10. Selenium2学习-010-WebUI自动化实战实例-008-Selenium 操作下拉列表实例-Select

    此文主要讲述用 Java 编写 Selenium 自动化测试脚本编写过程中,对下拉列表框 Select 的操作. 下拉列表是 Web UI 自动化测试过程中使用率非常高的,通常有两种形式的下拉列表,一 ...

随机推荐

  1. TYVJ P1038/P1039 忠诚 标签:线段树

    做题记录:2016-08-12 16:30:14 //P1038 描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记k次账,由于管家聪明能干,因而管家 ...

  2. sqlserver常用日期、时间函数和格式

    Sql Server中常用的日期与时间函数1.  当前系统日期.时间    select getdate() 2. dateadd  在向指定日期加上一段时间的基础上,返回新的 datetime 值  ...

  3. 关于UIWebView的总结

    关于UIWebView的总结 前言 今天参加了 Adobe 和 CSDN 组织的一个关于 PhoneGap 的开发讲座 ,而 PhoneGap 在 iOS 设备上的实现就是通过 UIWebView 控 ...

  4. SQL优化之【类型转换】

    DBA的日常工作中SQL优化占大半的时间,通常都是SQL语句性能问题或者schema设计有问题,最近遇到一个类型转换的问题,所以在这里分享一下,废话不多说了,直接建表进行测试. mysql), key ...

  5. VS - 实用技巧

    将Javascript 左边花括号置于新行 工具-选项-文本编辑器-格式设置-新行-全勾 开启调试自动窗口 || 监视变量 VS2012以下版本 CTRL + D + Q 其他版本 调试-窗口-自动窗 ...

  6. [转]超详细图解:自己架设NuGet服务器

    本文转自:http://diaosbook.com/Post/2012/12/15/setup-private-nuget-server 超详细图解:自己架设NuGet服务器 汪宇杰          ...

  7. [转]用Linq取CheckBoxList選取項目的值

    本文转自:http://www.dotblogs.com.tw/hatelove/archive/2011/11/17/linq-checkboxlist-items-selected-values. ...

  8. 新浪SAEStorage图片上传的demo和说明

    <?php if(isset($_POST[up])){ $s2 =new SaeStorage();//实例化 $name =$_FILES['myfile']['name'];//上传到服务 ...

  9. 使用Android Studio和Genymotion模拟器搭建Andriod开发环境

    一.Android Studio下载 1.打开http://www.android.com/ 2.依照下图步骤打开下载页面 a.在页脚部分点击“App Developer Resources” b.点 ...

  10. oracle sql日期比较

    oracle sql日期比较:在今天之前: select * from up_date where update < to_date('2007-09-07 00:00:00','yyyy-mm ...