个人写的一个selenium的base类,应该所有使用selenium的同事都会使用到:

package com.hx.baserunner;

import static java.io.File.separator;

import java.io.File;
import java.io.FileInputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.UnreachableBrowserException;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters; import com.hx.dataproviders.ExcelDataProivderLoginSheet;
import com.hx.utility.HostUtil; public class BaseSeleniumDriver { public WebDriver driver=null;
public String proxyserver, browser, hubUrl;
private static final Logger log=Logger.getLogger(BaseSeleniumDriver.class); @BeforeSuite
//@Parameters({ "excelpath"})
public void beforeSuite() throws Exception{
// Properties p = new Properties();
// FileInputStream conf = new FileInputStream(configfile);
// p.load(conf); String excelpath=System.getProperty("user.dir")+"\\resources\\TestData.xls";
log.debug("excel path is "+excelpath);
String hostname=HostUtil.getFQDN();
log.debug("the running host is :"+hostname);
Map<String,String> mapdata=ExcelDataProivderLoginSheet.getSpecifySheet(excelpath,hostname);
// hubUrl = p.getProperty("hubUrl");
// hubUrl="http://127.0.0.1:4444/wd/hub";
// browser = p.getProperty("browser");
browser=mapdata.get("Browser_Type").trim().toLowerCase();
// testUrl = p.getProperty("testUrl");
// log.info("The Page URL is:"+testUrl);
proxyserver=mapdata.get("proxy_url").trim();
log.debug("the browser type is :"+browser);
log.debug("the remote run host hub is :"+hubUrl);
log.debug("the browser's proxy server is :"+proxyserver); DesiredCapabilities capability=new DesiredCapabilities();
//common settings
capability.setCapability("cssSelectorsEnabled", true);
capability.setCapability("takesScreenshot", true);
capability.setCapability("javascriptEnabled", true);
capability.setCapability("ACCEPT_SSL_CERTS", true);
capability.setBrowserName(browser);
//proxy settings
// if(!proxyserver.equals(""))
// {
// log.debug(" the current proxy is not null ,we will set the proxy server for this host,proxy server is :"+proxyserver);
// capability.setCapability(CapabilityType.PROXY, new Proxy().setHttpProxy(proxyserver));
// capability.setCapability(CapabilityType.PROXY, new Proxy().setNoProxy("localhost"));
// log.debug("the proxy had been set correctly now ");
// } //use different browser
if (hubUrl == null || hubUrl.trim().isEmpty())
{
log.debug("the blow testing is for the local server testing");
// if no hubUrl specified, run the tests on localhost
if (browser == null || browser.trim().isEmpty()) {
// if no browser specified, use IE
//capability =DesiredCapabilities.internetExplorer();
log.debug(" the browser we used is IE ");
capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capability.setCapability("ignoreProtectedModeSettings",true);
capability.setCapability("enablePersistentHover", false); //prevent frozen
driver = new InternetExplorerDriver(capability);
log.debug("Start the IE driver now ");
}
else {
if (browser.trim().equalsIgnoreCase("firefox")) {
FirefoxProfile p = new FirefoxProfile();
p.setPreference("webdriver.log.file", "log/firefox_startup.log");
driver = new FirefoxDriver(capability);
log.debug("Start the firefox driver now ");
} else if (browser.trim().equalsIgnoreCase("chrome")) {
driver = new ChromeDriver(capability);
log.debug("start the chrome driver now ");
} else {
driver = new InternetExplorerDriver(capability);
log.debug("start the IE driver now ");
}
}
} else {
log.debug("we will run the remote host for the testing ");
// DesiredCapabilities capability=null; if (browser.toLowerCase().trim().equals("ie"))
{
//frozen windows
capability =DesiredCapabilities.internetExplorer();
log.debug(" the browser we used is IE ");
capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capability.setCapability("ignoreProtectedModeSettings",true);
capability.setCapability("enablePersistentHover", false); //prevent frozen }
else if(browser.toLowerCase().trim().equals("firefox"))
{
capability =DesiredCapabilities.firefox();
log.debug("the browser we used is firefox");
//if need the proxy
}
else
{
capability =DesiredCapabilities.chrome();
log.debug("the browser we used is none");
} //driver = new RemoteWebDriver(new URL(hubUrl), capability);
driver = new RemoteWebDriver(capability);
Capabilities actualCapabilities = ((RemoteWebDriver) driver).getCapabilities(); } //the driver need to wait time
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@AfterMethod
public void afterMethod(ITestResult result, ITestContext context) throws Exception { Throwable t = result.getThrowable();
log.debug("the throwable object is :"+t);
//if the testNG met error or exception
if ((!result.isSuccess())||t instanceof WebDriverException || t instanceof AssertionError) {
log.error("WebDriverException or Assert Exception");
// get filename
Calendar cal = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
// concat prefix with current time and return String filename = result.getTestClass().getName() + "."
+ result.getMethod().getMethodName() + "."
+ sf.format(cal.getTime()) + ".png";
log.debug("we met the error ,we will generate a screenshot file for this error, file name is "+filename);
// WebDriver augmentedDriver = new Augmenter().augment(driver);
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String path=new File(".").getAbsolutePath();
String screenshotpath=path.substring(0, path.length()-1);
// create a new file
FileUtils.copyFile(scrFile, new File(screenshotpath+"log"+separator + filename));
log.debug("the screenshot file in this file path:"+screenshotpath+"log"+separator + filename);
Reporter.setCurrentTestResult(result);
//Reporter.log("<a href=\"" + filename + "\">Screenshot</a>");
}
else
{
log.debug("This test method is working fine ,we marked it as passed");
}
} @AfterSuite
public void afterSuite() { driver.quit();
log.debug("quit the driver now ");
} }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Selenium 的基础框架类的更多相关文章

  1. 基于Java+Selenium的WebUI自动化测试框架(十三)-----基础页面类BasePage(Excel)

    前面,我们讲了如何使用POI进行Excel的“按需读取”.根据前面我们写的BasePageX,我们可以很轻松的写出来基于这个“按需读取”的BasePage. package webui.xUtils; ...

  2. 基于Java+Selenium的WebUI自动化测试框架(九)-----基础页面类(BasePage)

    上篇我们写了java读取xml文件的类,实现了可以从xml文件读取元素的方式.那么,接下来我们需要考虑一个问题.我们拿了这些元素之后怎么去操作呢? 先来看看我们手工测试的时候是怎么进行的. 双击浏览器 ...

  3. HibernateCRUD基础框架(1)-实体类

    HibernateCRUD基础框架包括3篇文章,主要讲述整个CRUD基础框架的思路. 第1篇:讲述最基本的实体类,这些实体类是对SQL语言中的一些概念的封装. 第2篇:在这些实体类的基础上,开发一个& ...

  4. Java并发基础框架AbstractQueuedSynchronizer初探(ReentrantLock的实现分析)

    AbstractQueuedSynchronizer是实现Java并发类库的一个基础框架,Java中的各种锁(RenentrantLock, ReentrantReadWriteLock)以及同步工具 ...

  5. Master-Slave通用基础框架

    一.设计目的 设计出一个通用的Master-Slave基础框架,然后可以基于这个框架来实现特定的业务需求,比如实现多节点并行计算.分布式处理等. 二.设计理念 基于经典的命令模式,Master和Sla ...

  6. 一个简单的、面向对象的javascript基础框架

    如果以后公司再能让我独立做一套新的完整系统,那么我肯定会为这个系统再写一个前端框架,那么我到底该如何写这个框架呢? 在我以前的博客里我给大家展示了一个我自己写的框架,由于当时时间很紧张,做之前几乎没有 ...

  7. iOS基础框架的搭建/国际化操作

    1.基础框架的搭建 1.1 pod引入常用的第三方类库 1.2 创建基础文件夹结构/目录结构 Resource———存放声音/图片/xib/storyboard 等资源文件 Define——宏定义, ...

  8. 准备.Net转前端开发-WPF界面框架那些事,搭建基础框架

    题外话 最近都没怎么写博客,主要是最近在看WPF方面的书<wpf-4-unleashed.pdf>,挑了比较重要的几个章节学习了下WPF基础技术.另外,也把这本书推荐给目前正在从事WPF开 ...

  9. Objective-c 基础框架(初学者-总结)

    一个框架其实就是一个软件包,它包含了多个类.Mac 操作系统提供了几十个框架,主要帮助开发者快速的在Mac 系统上开发应用程序.其中包括一些基础框架,就是为所有程序开发提供基础的框架,其中几个常用的类 ...

随机推荐

  1. [Web 前端] Jquery 复制元素,并修改属性, 追加到另一个元素后面

    cp from  : https://blog.csdn.net/cooledi/article/details/52813668 jquery 复制元素,并修改属性 $('#ID').clone() ...

  2. Node.js SDK与fabric链码交互开发

    1.本篇背景 前面已经对链码开发作了比较详细的介绍,并且对官方提供的 fabcar 链码进行了解读,本篇将介绍如何使用 Node.js SDK 与区块链网络中的链码进行交互. 本篇内容基本来自官方 H ...

  3. 脚本中export不起作用的原因分析

    #!bin/bash export PATH=$PATH:/usr/lib/java/jre export PATH=$PATH:/usr/lib/java/bin ---path 结果发现直接运行. ...

  4. [Link]Gearman分布式任务处理系统

    http://blog.csdn.net/jiao_fuyou/article/category/1745977 http://www.cnblogs.com/cocowool/archive/201 ...

  5. [转]PHP之APC缓存详细介绍(学习整理)

    From : http://www.2cto.com/kf/201210/160140.html 1.APC缓存简介APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存 ...

  6. iOS:仿写探探App动画

    一.简单介绍 探探动画比较新颖,这也是它在众多交友软件中火热的一个特色.实现这种动画的方式可以有两种方式实现: 1.使用转场动画实现  2.使用CollectionView自定义布局实现, 此处我提供 ...

  7. iOS开发--知识点总结

    1 .全局变量,变量名前加下划线.和系统一致. 2 . nil指针为空   @“”字符串为空 (内容为空)       ==  判断内存地址   基本变量    对于一些基本类型 可以使用==来判断, ...

  8. 创想三维:5款最好用的免费3D建模软件【转】

    虽然网上有需要现成的免费三维模型,但对于许多人而言,3D打印机最吸引他们之处是可以设计创造完全属于自己的模型.问题是,现代专业级CAD软件大多价格高昂,例如Solidworks或Zbrush这样的程序 ...

  9. AVL树原理及实现 +B树

    1. AVL定义 AVL树是一种改进版的搜索二叉树.对于一般的搜索二叉树而言,如果数据恰好是按照从小到大的顺序或者从大到小的顺序插入的,那么搜索二叉树就对退化成链表,这个时候查找,插入和删除的时间都会 ...

  10. 浅谈Kmeans聚类

    http://www.cnblogs.com/easymind223/archive/2012/10/30/2747178.html 聚类分析是一种静态数据分析方法,常被用于机器学习,模式识别,数据挖 ...