本文的主题是基于Selenium Server,使用 Java 语言编写网页交互流程,

实现多浏览器(IE Firefox Chrome)兼容性测试,为使用纪要。

Selenium

  Selenium是一套浏览器自动化测试工具(http://www.seleniumhq.org/),

其中Selenium IDE为火狐的一个插件,此插件可以实现火狐浏览器上用户操作的录制和回放功能,

但是录制结果不能再其他浏览器上验证。

  幸好其可以导出 JUnit框架等的测试代码,让其可以再其他浏览器上执行。

JUnit

  是一个单元测试框架,官网见下:

 https://github.com/junit-team/junit

  博客园上有使用介绍博文:

  http://www.cnblogs.com/mengdd/archive/2013/03/26/2983565.html

 准备:

  1、下载eclipse, 其自带junit。 下载地址 www.Eclipse.org

  2、下载Selenium Server 和 Java Client Driver, 下载地址 http://docs.seleniumhq.org/download/

  http://selenium-release.storage.googleapis.com/2.41/selenium-server-standalone-2.41.0.jar

  http://selenium-release.storage.googleapis.com/2.41/selenium-java-2.41.0.zip

  3、下载IE Driver,

  http://selenium-release.storage.googleapis.com/2.41/IEDriverServer_Win32_2.41.0.zip

  4、下载Chrome Driver,

  http://chromedriver.storage.googleapis.com/index.html

  http://chromedriver.storage.googleapis.com/2.10/chromedriver_win32.zip

 安装配置:

  1、启动eclipse,

    创建SeleniumTest工程,

    创建包 com.selenium.test.junitcode,

    创建类 seleniumTestJunit.java

    将 Selenium IDE导出的JUnit代码放到此类中,

      此代码中只支持Firefox,后面给出代码支持 IE 和 Chrome。

  2、选择工程, 右键, Build Path -》 Add Library,选择 Junit, 点击next finish。

  3、选择工程, 右键, Build Path -》 Config Build Path,Library 标签页, 点击 Add External Jars,

    添加Selenium Server 和 Java Client Driver

  4、IE 和 Chrome Driver解压到目录:

    C:\\Documents and Settings\\Administrator\\桌面\\seleniumtest\\

 运行:

  点击开始按钮,依次执行指令,本例中为打开 Baidu页,搜索“母亲节”, 分别执行 firefox chrome 和 IE。

其中,testCases为填充测试用例函数。

如下对于每个浏览器都执行下面类似三句,有重复语句,扩展性也不好;

本想使用数组容纳函数, 循环执行, 可惜Java不支持,请Java大侠指点。

      prepareFirefoxDriver();
testCases();
testover();
package com.selenium.test.junitcode;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit; import org.junit.*; import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*; import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select; public class seleniumTestJunit {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer(); @Before
public void setUp() throws Exception {
baseUrl = "http://www.baidu.com";
} private void prepareIEDriver(){
/* IE driver */
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\seleniumtest\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} private void prepareFirefoxDriver(){
/* firefox driver */
driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} private void prepareChromeDriver(){
File file = new File("C:\\Documents and Settings\\Administrator\\桌面\\seleniumtest\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
} private void testCases() throws Exception {
driver.get(baseUrl);
Thread.sleep(1000);
driver.findElement(By.id("kw1")).clear();
driver.findElement(By.id("kw1")).sendKeys("母亲节");
Thread.sleep(1000);
driver.findElement(By.id("su1")).click();
Thread.sleep(3000);
} public void testover() throws Exception {
driver.close();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
} @Test
public void testTt() throws Exception { prepareFirefoxDriver();
testCases();
testover(); prepareChromeDriver();
testCases();
testover(); prepareIEDriver();
testCases();
testover();
} @After
public void tearDown() throws Exception { }
}

附录问题查证记录:

如果IE运行中遇到如下打印, 请退出360安全卫士:

org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. IELaunchURL() returned HRESULT  ('参数不正确。') for URL 'http://localhost:48104/' (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 5.84 seconds

附录支持Selenium IDE导出JUnit代码:

package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select; public class Seleniumbaidutest {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer(); @Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.baidu.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
} @Test
public void testSeleniumbaidu() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("su")).click();
driver.findElement(By.id("su1")).click();
driver.findElement(By.id("kw1")).clear();
driver.findElement(By.id("kw1")).sendKeys("母亲节");
driver.findElement(By.id("su1")).click();
} @After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
} private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
} private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
} private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}

利用selenium Server实现IE firefox 和 chrome兼容性测试的更多相关文章

  1. Selenium 设置浏览器下载 Firefox 和Chrome

    当我们在使用Selenium运行自动化测试时,偶尔需要用到下载功能,但浏览器的下载可能会弹出下载窗口,或者下载路径不是我们想要保存的位置,所以在通过Selenium启动浏览器时需要做相关的设置,将使这 ...

  2. python selenium自动化测试之路(1)--分层测试概念、selenium工具介绍

    1.分层自动化测试概念 传统的自动化市场更关注产品UI层的自动化测试,而分层的自动化测试倡导产品开发的不同阶段都需要自动化测试 大多公司与研发团队其实是忽略了单元测试与集成测试阶段的自动化测试工作,所 ...

  3. [Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图

    前两篇文章介绍了安装,此篇文章算是一个简单的进阶应用吧!它是在Windows下通过Selenium+Python实现自动访问Firefox和Chrome并实现搜索截图的功能.        [Pyth ...

  4. 【转】利用 selenium 的 webdrive 驱动 headless chrome

    1.参考 使用 headless chrome进行测试 2.概念 Headless模式解决了什么问题: 自动化工具例如 selenium 利用有头浏览器进行测试,面临效率和稳定性的影响,所以出现了 H ...

  5. [Python爬虫] Selenium自己主动訪问Firefox和Chrome并实现搜索截图

    前两篇文章介绍了安装.此篇文章算是一个简单的进阶应用吧.它是在Windows下通过Selenium+Python实现自己主动訪问Firefox和Chrome并实现搜索截图的功能. [Python爬虫] ...

  6. Selenium+Python:下载文件(Firefox 和 Chrome)

    引自  https://blog.csdn.net/Momorrine/article/details/79794146 1.      环境 操作系统 Win10 IDE Eclipse (Oxyg ...

  7. 【python爬虫】利用selenium和Chrome浏览器进行自动化网页搜索与浏览

    功能简介:利用利用selenium和Chrome浏览器,让其自动打开百度页面,并设置为每页显示50条,接着在百度的搜索框中输入selenium,进行查询.然后再打开的页面中选中“Selenium - ...

  8. 重装系统后,重新搭建Selenium Server+Firefox环境

    摘要:搭建Selenium自动化测试环境其实是非常简单的事情,在态度上我们不要把它当成难事:折腾起来是很愉快的,自然就成功了. 下面把这次安装的过程记录下来,一来是加深印象,二来可以给大家提供参考. ...

  9. 利用Selenium自动化web测试

    简介: Selenium 是一个没有正式指导手册的开源项目,这让测试人员的问题调查很费时间.本文为基于 Selenium 1.0(发布于 2009 年 6 月)的测试期间的常见问题提供最佳实践. 简介 ...

随机推荐

  1. windows下的mongodb分片配置

    1. 分片服务器设置mongod -port 10001 -dbpath=F:/DbSoft/mongodb/rs_data/master -directoryperdb --shardsvr -re ...

  2. PHP 中和 HTTP 相关的函数及使用

    ① get_headers 方法:取得服务器响应一个 HTTP 请求所发送的所有标头 例如: <?php $httpinfo = get_headers('http://www.baidu.co ...

  3. linux 防火墙设置

    防火墙的基本操作命令: 查询防火墙状态:[root@localhost ~]# service iptables status<回车> 停止防火墙:[root@localhost ~]# ...

  4. 【翻译】Kinect v2程序设计(C++) BodyIndex篇

    通过Kinect SDK v2预览版,取得BodyIndex(人体区域)的方法和示例代码. 上一节,介绍了从Kinect v2预览版用Kinect SDK v2预览版获取Depth数据的方法.   这 ...

  5. 我的Ubuntu系统配置所作的备份记录如下

    Ubuntu无法关机解决办法 说明:如果不成功请参考一下文章最后的内容,也许会有帮助. 其实不止在ubuntu里面,fedora里面我也遇到了这个问题,就是电脑可以重启,但是不能直接关机,否则就一直停 ...

  6. 关于APP接口设计

    最近一段时间一直在做APP接口,总结一下APP接口开发过程中的注意事项: 1.效率:接口访问速度 APP有别于WEB服务,对服务器端要求是比较严格的,在移动端有限的带宽条件下,要求接口响应速度要快,所 ...

  7. JqueryMobile 跳转问题

    解决办法: 禁止ajxa跳转有两种情况: 1.禁止局部ajax跳转 2.禁止全局ajax跳转    对于#1只需要在a标签中添加下面的属性: data-ajax=“false” 有时我们要用正常的ht ...

  8. Centos下安装Scrapy

    Scrapy是一个开源的机遇twisted框架的python的单机爬虫,该爬虫实际上包含大多数网页抓取的工具包,用于爬虫下载端以及抽取端. 安装环境: centos5.4 python2.7.3 安装 ...

  9. 【转】基于laravel制作APP接口(API)

    这篇文章主要介绍了基于laravel制作APP接口(API)的相关资料,需要的朋友可以参考下 前期准备 前言,为什么做以及要做个啥本人姓小名白,不折不扣编程届小白一名,但是自从大一那会儿接触到编程这件 ...

  10. 函数式编程Map()&Reduce()

    .forEach():每个元素都调用指定函数,可传三个参数:数组元素丶元素索引丶数组本身丶 , , , , , , , ]; a.forEach(function(v,i,a){a[i]=v+;}); ...