Selenium - WebDriver 小结(1)
public class Base {
SimpleDateFormat formatterTime = new SimpleDateFormat("yyyyMMdd_hhmmssa");
SimpleDateFormat formatterDate = new SimpleDateFormat("yyyyMMdd");
private Calendar now() {
Calendar now = Calendar.getInstance();
return now;
}
public static Map<String, String> initialProperties(String properityName) {
String key = "";
String value = "";
Map<String, String> map = null;
ResourceBundle bound = PropertyResourceBundle.getBundle(properityName, Locale.ENGLISH);
Enumeration<String> enumeration = bound.getKeys();
if (enumeration != null) {
map = new HashMap<String, String>();
while (enumeration.hasMoreElements()) {
key = (String) enumeration.nextElement();
value = bound.getString(key);
map.put(key, value);
}
}
return map;
}
public static WebDriver setUp_IEDriver() throws Exception {
System.setProperty("webdriver.ie.driver", "./IEDriverServer.exe");
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver = new InternetExplorerDriver(capabilities);
driver.manage().window().maximize();
return driver;
}
public WebDriver setUp_ChromeDriver(){
System.setProperty("webdriver.chrome.driver", "./chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
return driver;
}
public WebDriver setUp_FirefoxDriver(){
System.setProperty("webdriver.ie.driver", "C:/Program Files/Mozilla Firefox");
WebDriver driver = new FirefoxDriver();
return driver;
}
public static boolean isElementPresent(WebDriver driver, By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
e.printStackTrace();
return false;
}
}
public void screenshot(WebDriver driver, String path){
File folder = new File(path);
try {
if(!folder.exists()){
folder.mkdirs();
}
Dimension window_size = driver.manage().window().getSize();
// BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
BufferedImage image = new Robot().createScreenCapture(new Rectangle(window_size.getWidth(), window_size.getHeight()));
String screenshotName = path+formatterTime.format(now().getTime())+".png";
ImageIO.write(image, "png", new File(screenshotName));
System.out.println("Screenshot "+screenshotName+" has been saved to " + path);
} catch (HeadlessException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void pageshot(WebDriver driver, String path){
File folder = new File(path);
try {
if(!folder.exists()){
folder.mkdirs();
}
String screenshotName = path+formatterTime.format(now().getTime())+".png";
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File(screenshotName));
System.out.println("Screenshot "+screenshotName+" has been saved to " + path);
} catch (HeadlessException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean windowStatus(WebDriver driver, final String window_status){
WebDriverWait wait = new WebDriverWait(driver, 120);
boolean windowStatus = wait.until(new ExpectedCondition<Boolean>(){
public Boolean apply(WebDriver driver){
JavascriptExecutor js = (JavascriptExecutor) driver;
return js.executeScript("return window.status").toString().trim().equals(window_status);
}});
return windowStatus;
}
public void waitForElementLoadByXpath(WebDriver driver, final String xpath){
WebDriverWait wait = new WebDriverWait(driver, 300);
wait.until(new ExpectedCondition<WebElement>(){
public WebElement apply(WebDriver driver) {
WebElement element = driver.findElement(By.xpath(xpath));
return element;
}
});
}
public void waitForPageLoad(WebDriver driver){
WebDriverWait wait = new WebDriverWait(driver, 300);
wait.until(new ExpectedCondition<Boolean>(){
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").toString().equalsIgnoreCase("complete");
}
});
}
public static void waitForDivLoad(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript("return document.getElementById('ctl00_ContentPlaceHolder1_ClientProgress').style.display").toString().equalsIgnoreCase("block");
}
});
}
}
Selenium - WebDriver 小结(1)的更多相关文章
- 基于python的Selenium使用小结
之前介绍过基于Unittest和TestNG自动化测试框架,然而基于Web端的测试的基础框架是需要Selenium做主要支撑的,这里边给大家介绍下Web测试核心之基于Python的Selenium 一 ...
- 【转载】Selenium WebDriver的简单操作说明
转载自:http://blog.csdn.net/xiao190128/article/details/49784121 1.打开一个测试浏览器 对浏览器进行操作首先需要打开一个浏览器,接下来才能对浏 ...
- Selenium WebDriver的简单操作说明
[From] http://blog.csdn.net/xiao190128/article/details/49784121 1.打开一个测试浏览器 对浏览器进行操作首先需要打开一个浏览器,接下来才 ...
- selenium webdriver学习(二)————对浏览器的简单操作(转载JARVI)
selenium webdriver学习(二)————对浏览器的简单操作 博客分类: Selenium-webdriver selenium webdriver对浏览器的简单操作 打开一个测试浏览 ...
- Selenium WebDriver Code
Selenium WebDriver 用于模拟浏览器的功能,可以做网站测试用,也可以用来做crawler.我是用eclipse开发的,导入selenium-server-standalone-***. ...
- 使用httpclient 调用selenium webdriver
结合上次研究的selenium webdriver potocol ,自己写http request调用remote driver代替selenium API selenium web driver ...
- selenium webdriver 右键另存为下载文件(结合robot and autoIt)
首先感谢Lakshay Sharma 大神的指导 最近一直在研究selenium webdriver右键菜单,发现selenium webdriver 无法操作浏览器右键菜单,如图 如果我想右键另存为 ...
- Selenium Webdriver java 积累一
Selenium Webdriver 学习: http://jarvi.iteye.com/category/203994 https://github.com/easonhan007/webdriv ...
- Selenium的PO模式(Page Object Model)|(Selenium Webdriver For Python)
研究Selenium + python 自动化测试有近两个月了,不能说非常熟练,起码对selenium自动化的执行有了深入的认识. 从最初无结构的代码,到类的使用,方法封装,从原始函数 ...
随机推荐
- js判断主流浏览器类型和版本号
如今的互联网中,浏览器可以说是太多太多了,但是大部分都是换壳不换心,基本上主流的浏览器还是火狐,谷歌,IE,safrai这几种比较常见,所以在我们的开发中,有时候需要遇到判断用户正在使用什么浏览器以及 ...
- Codevs 1171 潜伏者 2009年NOIP全国联赛提高组
1171 潜伏者 2009年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description [问题描述] R 国和S 国正陷 ...
- 九度OJ 1433 FatMouse -- 贪心算法
题目地址:http://ac.jobdu.com/problem.php?pid=1433 题目描述: FatMouse prepared M pounds of cat food, ready to ...
- Headfirst设计模式的C++实现——命令模式(Command)
先看如果不用命令模式的实现: light.h #ifndef _LIGHT_H_ #define _LIGHT_H #include <iostream> class LIGHT { pu ...
- 项目中logger、message错误信息的配置
申明:在一个项目中必不可少的是Logger和错误信息的配置,现在给出在我们常用的处理方法. —.创建一个ConfigUtils类和他对应的rah.properties文件和Test测试类 Config ...
- spark-shell - 三个引号,让脚本阅读更开心
spark-shell中可以直接编写SQL语句从数据源中加载数据. 可以利用scala语言中的多行字符串(三个引号)让SQL语句结构清晰更易于阅读. 示例: sqlContext.sql(" ...
- Docker之配置Centos_ssh
写Dockerfile配置文件 #DockerfileFROM centos:6 #以下命令用在什么镜像中MAINTAINER cuizhipeng <cuizhipeng@126.com&g ...
- DIV+CSS 网页布局之:两列布局
1.宽度自适应两列布局 两列布局可以使用浮动来完成,左列设置左浮动,右列设置右浮动,这样就省的再设置外边距了. 当元素使用了浮动之后,会对周围的元素造成影响,那么就需要清除浮动,通常使用两种方法.可以 ...
- 简易ORM(基于注解)
这是从我们现有项目做的一定的改进准备做成IDE插件 类似getter和setter的生成 1.定义实体类 通过注解说明其表名和字段名(SOURCE类型的注解 不需要运行时使用)@TableName(& ...
- SortedList的用法
1.SortedList定义 System.Collections.SortedList类表示键/值对的集合,这些键值对按键排序并可按照键和索引访问.SortedList 在内部维护两个数组以存储列表 ...