个人写的一个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. java通过Stream对list集合分组

    java通过Stream对list集合分组 现在有一个List集合,想对该集合中的数据分组处理,想到java8中的stream,就搞来试试,非常给力!例子如下 1 2 3 4 5 6 7 8 9 10 ...

  2. PHP获取指定函数定义在哪个文件中及行号

    当调试开源的代码时,希望查看某个函数的定义,那么就需要定位其位置.特别是有的项目中,函数会有多个地方都有定义,那么如果我想知道当前调用的这个函数是在哪里定义的,可以用下面这个方法. function ...

  3. Caffe常用层参数介绍

    版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/Cheese_pop/article/details/52024980 DATA crop:截取原图像中一个 ...

  4. MECE分析法(Mutually Exclusive Collectively Exhaustive)

    什么是MECE分析法? MECE,是Mutually Exclusive Collectively Exhaustive,中文意思是“相互独立,完全穷尽”. 也就是对于一个重大的议题,能够做到不重叠. ...

  5. Reloading Java Classes 101: Objects, Classes and ClassLoaders Translation

    The original link: http://zeroturnaround.com/rebellabs/reloading-objects-classes-classloaders/ A Bir ...

  6. jquery实现相同事件名称,不同命名空间的调用方法

    <html xmlns="http://www.w3.org/1999/xhtml"> <head>  <title></title> ...

  7. Http请求中Content-Type讲解以及在Spring MVC注解中produce和consumes配置详解

    原文地址:  https://blog.csdn.net/shinebar/article/details/54408020 引言: 在Http请求中,我们每天都在使用Content-type来指定不 ...

  8. 细说Request与Request.Params

    在ASP.NET编程中,有三个比较常见的来自于客户端的数据来源:QueryString, Form, Cookie .我们可以在HttpRequest中访问这三大对象,比如,可以从QueryStrin ...

  9. Improve your code with lint checks

    官方文档 使用 Lint 改进您的代码 [Improve your code with lint checks] 除了[In addition to]测试 Android 应用以确保其符合功能要求[m ...

  10. Mac系统下编译支持Android平台的最新X264编码器

    Mac系统下编译支持Android平台的最新X264编码器 原文来自 http://www.mingjianhua.com,转载请注明出处 1.首先去官网下载最新的x264源代码,解压到任意目录 ht ...