package com.yk.userlive.base;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

/**
* @author Seiitsu 2016年9月5日
*/
public class YKWebTestAPI {

private String value = “”;
private boolean flag = true;
private static WebDriver driver = null;

/*
* public void setWebDriver(WebDriver driver) { this.driver = new
* InternetExplorerDriver(); }
*
* public WebDriver getWebDriver() { return this.driver = new
* InternetExplorerDriver(); }
*/

public static WebDriver myDriver(String browser) {

String proxyIpAndPort= “localhost:12104”;
DesiredCapabilities cap = new DesiredCapabilities();
Proxy proxy=new Proxy();
proxy.setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort).setSslProxy(proxyIpAndPort);
cap.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);
cap.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
System.setProperty(“http.nonProxyHosts”, “localhost”);
cap.setCapability(CapabilityType.PROXY, proxy);

if (“firefox”.equals(browser.toLowerCase())) {

// 启动本机firefox
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile(“default”);
// 启动浏览器
driver = new FirefoxDriver(profile);
driver.manage().window().maximize();

} else if (“ie”.equals(browser.toLowerCase())) {

// 关闭保护模式
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capability.setCapability(“ignoreProtectedModeSettings”, true);
// 指定驱动位置,并加载驱动
System.setProperty(“webdriver.ie.driver”, “tools/IEDriverServer64.exe”);
driver = new InternetExplorerDriver(capability);
driver.manage().window().maximize();

} else if (“chrome”.equals(browser.toLowerCase())) {

// 指定驱动位置,并加载驱动
System.setProperty(“webdriver.chrome.driver”, “tools/chromedriver.exe”);
driver = new ChromeDriver();
driver.manage().window().maximize();

} else {

System.out.println(“==============”);
}
return driver;

}

/**
* 根据name定位元素并输入内容
*
* @param name
* @param value
*/
public boolean inputByName(String name, String value) {
if (flag) {
try {
driver.findElement(By.name(name)).sendKeys(value);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据classname定位元素并点击
*
* @param name
*/
public boolean clickByClassName(String name) {
if (flag) {
try {
driver.findElement(By.className(name)).click();;
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据linkText定位元素并点击
*
* @param name
*/
public boolean clickByLinkText(String name) {
if (flag) {
try {
driver.findElement(By.linkText(name)).click();;
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 执行js方法
*
* @param js
*/
public boolean excuteJS(String js) {
if (flag) {
try {
((JavascriptExecutor) driver).executeScript(js);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据id定位元素并输入内容
*
* @param id
* @param value
*/
public boolean inputById(String id, String value) {
if (flag) {
try {
driver.findElement(By.id(id)).sendKeys(value);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据xpath定位元素并输入内容
*
* @param xpath
* @param value
*/
public boolean inputByXpath(String xpath, String value) {
if (flag) {
try {
driver.findElement(By.xpath(xpath)).sendKeys(value);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据css定位元素并输入内容
*
* @param css
* @param value
*/
public boolean inputByCss(String css, String value) {
if (flag) {
try {
driver.findElement(By.cssSelector(css)).sendKeys(value);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

public boolean clickBindCard(String cardNo) {
flag = false;
String value = “”;
int i = 0;
if (!driver.findElement(By.id(“r_x”)).isDisplayed()) {
for (i = 0; i < 5; i++) {
value = driver.findElement(By.id(“t_b_” + i)).getText();
if (value.contains(cardNo)) {
flag = true;
System.out.println(“t_b_” + i);
driver.findElement(By.id(“sel_img_” + i)).click();
}
}
} else {
while (driver.findElement(By.id(“r_x”)).isDisplayed()) {
System.out.println(“i>>” + i);
if (i > 0 && i % 5 == 0) {
driver.findElement(By.id(“r_x”)).click();
}
value = driver.findElement(By.id(“t_b_” + i)).getText();
System.out.println(value + “>>” + value.contains(cardNo));
if (value.contains(cardNo)) {
flag = true;
System.out.println(“t_b_” + i);
driver.findElement(By.id(“sel_img_” + i)).click();
break;
}
i++;
}
}
return flag;
}

/**
* 根据id定位元素并点击
*
* @param id
*/
public boolean clickById(String id) {
if (flag) {
try {
if (id.startsWith(“sel_img_”) && !id.contains(“sel_img_ct”)) {
int i = Integer.parseInt(id.substring(8, id.length()));
int j = 4;
while (i > 3) {
driver.findElement(By.id(“sel_img_” + j)).click();
i -= 4;
j += 4;
}
}
driver.findElement(By.id(id)).click();
System.out.println(“click element by id>>” + id);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据xpath定位元素并点击
*
* @param xpath
*/
public boolean clickByXpath(String xpath) {
if (flag) {
try {
driver.findElement(By.xpath(xpath)).click();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据css定位元素并点击
*
* @param css
*/
public boolean clickByCss(String css) {
if (flag) {
try {
driver.findElement(By.cssSelector(css)).click();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据id定位元素并连续点击
*
* @param id
*/
public boolean clickById(String id, int count) {
if (flag) {
try {
if (id.startsWith(“sel_img_”) && !id.contains(“sel_img_ct”)) {
int i = Integer.parseInt(id.substring(8, id.length()));
int j = 4;
while (i > 3) {
driver.findElement(By.id(“sel_img_” + j)).click();
i -= 4;
j += 4;
}
}
for (int i = 0; i < count; i++) {
driver.findElement(By.id(id)).click();
}
System.out.println(“click element by id>>” + id);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据xpath定位元素并连续点击
*
* @param xpath
*/
public boolean clickByXpath(String xpath, int count) {
if (flag) {
try {
for (int i = 0; i < count; i++) {
driver.findElement(By.xpath(xpath)).click();
}
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据css定位元素并连续点击
*
* @param css
*/
public boolean clickByCss(String css, int count) {
if (flag) {
try {
for (int i = 0; i < count; i++) {
driver.findElement(By.cssSelector(css)).click();
}
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据xpath定位元素获取值
*
* @param xpath
* @return
*/
public String getValueByXpath(String xpath) {
if (flag) {
value = driver.findElement(By.xpath(xpath)).getText();
System.out.println();
return value;
} else {
System.out.println(“flag is false, function is not excuted”);
return null;
}
}

/**
* 根据id定位元素获取值
*
* @param id
* @return
*/
public String getValueById(String id) {
if (flag) {
value = driver.findElement(By.id(id)).getText();
System.out.println(value);
return value;
} else {
System.out.println(“flag is false, function is not excuted”);
return null;
}
}

/**
* 根据css定位元素获取值
*
* @param css
* @return
*/
public String getValueByCss(String css) {
if (flag) {
value = driver.findElement(By.cssSelector(css)).getText();
System.out.println(value);
return value;
} else {
System.out.println(“flag is false, function is not excuted”);
return null;
}
}

/**
* 根据id定位元素并清空值
*
* @param id
*/
public boolean clearInputValueById(String id) {
if (flag) {
try {
driver.findElement(By.id(id)).clear();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据xpath定位元素并清空值
*
* @param xpath
*/
public boolean clearInputValueByXpath(String xpath) {
if (flag) {
try {
driver.findElement(By.xpath(xpath)).clear();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据css定位元素并清空值
*
* @param css
*/
public boolean clearInputValueByCss(String css) {
if (flag) {
try {
driver.findElement(By.cssSelector(css)).clear();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 获取网页的title值
*
* @return
*/
public String getTitle() {
if (flag) {
return driver.getTitle();
} else {
System.out.println(“flag is false, function is not excuted”);
return null;
}
}

/**
* 切换到frame框
*
* @param frameName
*/
public boolean switchToFrame(String frameName) {
if (flag) {
try {
driver.switchTo().frame(frameName);
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据id定位元素并获取元素的显示状态
*
* @param id
* @return boolean
*/
public boolean getDisplayStatById(String id) {
if (flag) {
return driver.findElement(By.id(id)).isDisplayed();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据xpath定位元素并获取元素的显示状态
*
* @param xpath
* @return
*/
public boolean getDisplayStatByXpath(String xpath) {
if (flag) {
return driver.findElement(By.xpath(xpath)).isDisplayed();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据css定位元素并获取元素的显示状态
*
* @param css
* @return
*/
public boolean getDisplayStatByCss(String css) {
if (flag) {
return driver.findElement(By.cssSelector(css)).isDisplayed();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据id定位元素并获取元素的可写状态
*
* @param id
* @return
*/
public boolean getEnableStatById(String id) {
if (flag) {
return driver.findElement(By.id(id)).isEnabled();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据xpath定位元素并获取元素的可写状态
*
* @param xpath
* @return
*/
public boolean getEnableStatByXpath(String xpath) {
if (flag) {
return driver.findElement(By.xpath(xpath)).isEnabled();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据css定位元素并获取元素的可写状态
*
* @param css
* @return
*/
public boolean getEnableStatByCss(String css) {
if (flag) {
return driver.findElement(By.cssSelector(css)).isEnabled();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}

}

/**
* 根据id定位元素并获取元素的选中状态
*
* @param id
* @return
*/
public boolean getSelectStatById(String id) {
if (flag) {
return driver.findElement(By.id(id)).isSelected();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据xpath定位元素并获取元素的选中状态
*
* @param xpath
* @return
*/
public boolean getSelectStatByXpath(String xpath) {
if (flag) {
return driver.findElement(By.xpath(xpath)).isSelected();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 根据css定位元素并获取元素的选中状态
*
* @param css
* @return
*/
public boolean getSelectStatByCss(String css) {
if (flag) {
return driver.findElement(By.cssSelector(css)).isSelected();
} else {
System.out.println(“flag is false, function is not excuted”);
return false;
}
}

/**
* 获取当前焦点所在页面元素的属性值(name,value,id,src等等)
*
* @param attribute
* @return
*/
public String getFocusAttributeValue(String attribute) {
try {
Thread.sleep(333);
} catch (Exception e) {
e.printStackTrace();
}
value = driver.switchTo().activeElement().getAttribute(attribute);
System.out.println(“The focus Element’s ” + attribute + “attribute value is>>” + value);
return value;
}

/**
* 打开网页链接
*
* @param pageUrl
*/
public boolean openPage(String pageUrl) {
try {
driver.get(pageUrl);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}

/**
* 进入测试,打开浏览器,输入网址,打开网页
*
* @param remoteUrl
* 远程服务器地址
* @param pageUrl
* 测试页面地址
*/
public boolean startTest(String remoteUrl, String pageUrl) {
try {
try {
driver = new RemoteWebDriver(new URL(remoteUrl), DesiredCapabilities.firefox());
} catch (MalformedURLException e) {
e.printStackTrace();
}
driver.get(pageUrl);
return true;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
}

/**
* 进入测试,打开浏览器,输入网址,打开网页
*
* @param explore
* 调用的浏览器,需要启动不同的server,如:firefox,需要运行selenium-server-standalone-
* 2.33.0.jar。IE,则需运行IEDriverServer.exe
*
* @param remoteUrl
* 远程服务器地址
* @param pageUrl
* 测试页面地址
*/
public boolean startTest(String explore, String remoteUrl, String pageUrl) {
try {
try {
if (“f”.equals(explore)) {
System.out.println(“firefox”);
driver = new RemoteWebDriver(new URL(remoteUrl), DesiredCapabilities.firefox());
} else if (“ie”.equals(explore)) {
System.out.println(“internet explorer”);
DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
driver = new RemoteWebDriver(new URL(remoteUrl), cap);
} else {
System.out.println(“firefox”);
driver = new RemoteWebDriver(new URL(remoteUrl), DesiredCapabilities.firefox());
}
} catch (Exception e) {
e.printStackTrace();
}
driver.get(pageUrl);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}

/**
* 设置定位页面元素的超时时间
*
* @param second
* @return
*/
public boolean setTimeOut(int second) {
try {
driver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}

/**
* 结束测试,关闭浏览器
*/
public boolean endTest() {
try {
driver.quit();
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}

/**
* 休息间隔,单位毫秒
*
* @param i
* @return
*/
public boolean sleep(int i) {
try {
Thread.sleep(i);
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}

/**
* 根据id等待对应的页面元素出现
*
* @param id
* @return
*/
public boolean waitForElementById(String id) {
try {
driver.findElement(By.id(id));
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}

/**
* 根据css等待对应的页面元素出现
*
* @param css
* @return
*/
public boolean waitForElementByCss(String css) {
try {
driver.findElement(By.cssSelector(css));
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}

/**
* 根据xpath等待对应的页面元素出现
*
* @param xpath
* @return
*/
public boolean waitForElementByXpath(String xpath) {
try {
driver.findElement(By.xpath(xpath));
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
}

转载请注明:《封装常用的selenium方法--http://www.testinn.pub/testinn/55

封装常用的selenium方法的更多相关文章

  1. 《手把手教你》系列基础篇(九十七)-java+ selenium自动化测试-框架设计篇-Selenium方法的二次封装和页面基类(详解教程)

    1.简介 上一篇宏哥介绍了如何设计支持不同浏览器测试,宏哥的方法就是通过来切换配置文件设置的浏览器名称的值,来确定启动什么浏览器进行脚本测试.宏哥将这个叫做浏览器引擎类.这个类负责获取浏览器类型和启动 ...

  2. 封装常用的js(Base.js)——【01】理解库,获取节点,连缀,

    封装常用的js(Base.js)——[01]理解库,获取节点,连缀,  youjobit07 2014-10-10 15:32:59 前言:       现如今有太多优秀的开源javascript库, ...

  3. 【终结版】C#常用函数和方法集汇总

    C#里面的常用的函数和方法非常重要,然而做题的时候会经常忘记这些封装好的方法,所以我总结一下 C#常用函数和方法集. [1]C#操作字符串的常用使用方法 在 C# 中,您可以使用字符数组来表示字符串, ...

  4. Robotframework + Appium 之常用元素定位方法

    任何自动化测试,其实手动测试也是一样的,元素定位(目标定位)是首要任务,这是最直接的测试对象呀! 好了,废话不多说,又到了元素定位啦,之前我们已经介绍过selenium及appium常用的定位方法,下 ...

  5. 第190天:js---String常用属性和方法(最全)

    String常用属性和方法 一.string对象构造函数 /*string对象构造函数*/ console.log('字符串即对象');//字符串即对象 //传统方式 - 背后会自动将其转换成对象 / ...

  6. 原生js封装的一些jquery方法

    用js封装一些常用的jquery方法 记录一下 hasClass:判断是否有class function hasClass(ele, cls) { if (!ele || !cls) return f ...

  7. SERVLET类常用接口及方法

    SERVLET类常用接口及方法 2011-09-09 16:14:43    [size=xx-small]SERVLET类常用接口及方法2007年04月05日 星期四 04:46 P.M.基本类和接 ...

  8. javascript的函数、创建对象、封装、属性和方法、继承

    转自原文javascript的函数.创建对象.封装.属性和方法.继承 一,function 从一开始接触到js就感觉好灵活,每个人的写法都不一样,比如一个function就有N种写法 如:functi ...

  9. 基于appium的常用元素定位方法

    一.元素定位工具 app应用的元素使用的是控件定位,不同于web网页,web网页定位元素通常使用的是F12工具,那么在app当中我们则要借助其它的工具来辅助定位. 1.uiautomatorviewe ...

随机推荐

  1. web 表单,脚本验证

    1.不能含有中文 var obj = document.form1.txtName.value; if(/.*[\u4e00-\u9fa5]+.*$/.test(obj)) { alert(" ...

  2. JBoss 系列十八:使用JGroups构建块RpcDispatcher构建群组通信应用

    内容概要 本部分说明JGroups构建块接口RpcDispatcher,具体提供一个简单示例来说明如何使用JGroups构建块RpcDispatcher构建群组通信应用. 示例描述 类似Message ...

  3. ubuntu 改动 ls 下的文件夹颜色

    ubuntu 下, ls 显示的文件夹的颜色,怎么说呢,看起来太费劲了. 于是想着改动成easy识别的颜色. 于是搜索了一下. 这里列举三个搜到的教程吧. 简单说我按这上面的方法做了,然后都失败了. ...

  4. 【VBA编程】08.数组

    [数组简介]数组其实就是一组相同类型的数据的有序集合,其形象表示就像线性表.在存储数据的时候,首先在内存中分配一个连续的存储空间,将各个元素按顺序存放在连续的存储单元格中.[定义静态数组]Dim 数据 ...

  5. @Resource或者@Autowired作用/Spring中@Autowired注解、@Resource注解的区别

    @Resource或者@Autowired作用不用写set get就能注入,当然,前提是你已经开启了注解功能. spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定 ...

  6. Chrome应用技巧之颜色拾取

    之前在Chrome应用店找了个插件实现拾色功能.并且很不理想.不知道是不是曾经Chrome自带的开发工具没提供到拾色功能还是我没发现.今天无意中发现Chomer自带的开发工具可拾色,请看以下的GIF动 ...

  7. Nodejs 命令行交互神奇 yargs

    传送门: # example https://github.com/yargs/yargs/blob/master/docs/examples.md # 官网 http://yargs.js.org/ ...

  8. Mongodb更新数组$pull修饰符

    http://blog.csdn.net/yaomingyang/article/details/78701643 一.$pull修饰符会删除掉数组中符合条件的元素,使用的格式是: { $pull:  ...

  9. POJ-1318(list.sort()输出不为字典序,map才是按字典序排列)

    #include<iostream> #include<string> #include<list> #include<map> #include< ...

  10. Openresty + nginx-upload-module支持文件上传

    0. 说明 这种方式其实复杂,麻烦!建议通过这个方式搭建Openresty文件上传和下载服务器:http://www.cnblogs.com/lujiango/p/9056680.html 1. 包下 ...