您的位置:首页 > 移动开发

appium笔记六:appium常用api二次封装

2018-01-26 13:03 399 查看
appium提供的各种api可以直接拿来用也可以进行二次封装,当然编程厉害的还可以自己修改部分源码。这里仅提供参考,不用千篇一律

/**
* Created by kingwit on 2017/9/5 0005.
* 说明:查找元素、判断元素是否存在
*/

public class ElementMethodObject {

/**
* 判断元素是否存在
* 参数: androiddriver By的对象
* return 布尔值
**/

public boolean isElementPresent(AndroidDriver driver, By by){
try {
if(driver==null){
Log.info("driver 对象为空...请检查代码....");
}
driver.findElement(by);
Log.info("在当前页面找到元素:"+by.toString());
return true;
}catch (NoSuchElementException e){
//e.printStackTrace();
Log.error("在当前页面找不到该元素:"+by.toString());
return false;
}
}

/**
* 等待元素出现 10s超时,找不到返回null 自定义方法等待
* 参数: androiddriver By的对象 ,等待时间
* 找到返回true
**/
public WebElement waitForElementPresent(AndroidDriver driver, By by,int waitSec){
WebElement webElement = null;
for(int sec=0;;sec++){
if(sec >= waitSec) {
try {
throw new NoSuchElementException("超过" + waitSec + "s元素未找到:" + by.toString());
} catch (NoSuchElementException e) {
e.printStackTrace();
}
break;
}
if(isElementPresent(driver,by)) {
webElement = driver.findElement(by);
break;
}
Log.info("
4000
继续尝试查找:"+by.toString());
ThreadSleepMethod.threadSleep(1000);
}
return webElement;
}

/**
* 查找元素 显性等待
* 参数:driver 对象,等待时间,by对象
* return webelment对象
**/
public WebElement WebElementWait(AndroidDriver driver , int waittime, final By by){
WebDriverWait wait = new WebDriverWait(driver, waittime);
WebElement element = wait.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return
d.findElement(by);
}});
return element;
}
}
public class ScreenshotMethodObject {
// public AndroidDriver driver;
public Date data;

/**
* 获取当前测试时间
* 参数:无
* return  返回当前日期 指定格式
**/
private   String getTime(){
SimpleDateFormat data = new SimpleDateFormat("yyyyMMddhhmm");
return data.format(new Date());
}

/**
* 截图(路径写死)
* 参数:driver 对象
* return  无
**/
public void Screenshot(TakesScreenshot drivername,String strName ){
String filename=getTime()+strName + ".jpg";
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(scrFile,new File("d://FTP//AutoTest"+"\\"+filename));
} catch (IOException e) {
Log.error("保存失败");
e.printStackTrace();
}
finally {
Log.info("Screen shot finished, path in "
+"d://ftp");
}
}

/**
* 截图(可自定义路径)
* 参数:截图的图片的名字,和pathName 文件路径
* return  无
**/
public void Screenshot(TakesScreenshot drivername,String pathName,String strName){
String filename=getTime()+strName + ".jpg";
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(scrFile,new File(pathName+"\\"+filename));
} catch (IOException e) {
Log.error("保存失败....");
e.printStackTrace();
}
finally {
Log.info("Screen shot finished, path in "
+ pathName);
}
}

}

/**
* Created by kingwit on 2017/9/5 0005.
* 提供滑动屏幕和元素的方法
*/

public class SwipeMethodObject {

public AndroidDriver driver;

public SwipeMethodObject(AndroidDriver driver){
this.driver = driver;
}

/**
* 获取屏幕的尺寸
* 参数:driver对象
* return  屏幕宽和高
**/
public int[] getAppScreen(AndroidDriver driver){
int[] appWidthAndHeight;
int widthScreen=driver.manage().window().getSize().getWidth();
int heightScreen = driver.manage().window().getSize().getHeight();
appWidthAndHeight  = new int[]{widthScreen,heightScreen };
return appWidthAndHeight;
}

/**
* 向上滑动
* 参数:duration 持续时间毫秒单位即滑动速度,num滑动次数
* return  无
**/
public void slideUP(AndroidDriver driver,int duration,int num) {
int startX = this.getAppScreen(driver)[0]/2;
int startY = this.getAppScreen(driver)[1]*4/5;
int endY = this.getAppScreen(driver)[1]/5;
try{
for(int i=0; i<num; i++){
driver.swipe(startX, startY, startX, endY, duration);
ThreadSleepMethod.threadSleep(1000);
}
Log.info("滑动成功...");

}catch (Exception e){
Log.error("滑动失败");
e.printStackTrace();
}

}

/**
* 向下滑动
* 参数:duration 持续时间毫秒单位即滑动速度,num滑动次数
* return  无
**/
public void slideDown(AndroidDriver driver,int duration,int num) {

int startX = this.getAppScreen(driver)[0]/2;
int startY = this.getAppScreen(driver)[1]/5;
int endY = this.getAppScreen(driver)[1]*4/5;
try{
for (int i=0; i<num; i++){
driver.swipe(startX, startY, startX, endY, duration);
ThreadSleepMethod.threadSleep(1000);
}
Log.info("滑动成功...");
}catch (Exception e){
Log.info("滑动失败...");
e.printStackTrace();
}

}

/**
* 向左滑动
* 参数:duration 持续时间毫秒单位即滑动速度,num滑动次数
* return  无
**/
public void slideLeft(AndroidDriver driver,int duration,int num) {

int startX = this.getAppScreen(driver)[0]*4/5;
int endX = this.getAppScreen(driver)[0]/5;
int startY = this.getAppScreen(driver)[1]/2;
try{
for (int i=0; i<num; i++){
driver.swipe(startX, startY, endX, startY, duration);
ThreadSleepMethod.threadSleep(1000);
}
Log.info("滑动成功...");
}catch (Exception e){
Log.info("滑动失败...");
e.printStackTrace();
}
}

/**
* 向右滑动
* 参数:duration 持续时间毫秒单位即滑动速度,num滑动次数
* return  无
**/
public void slideRight(AndroidDriver driver,int duration,int num) {

int startX = this.getAppScreen(driver)[0]/5;
int endX = this.getAppScreen(driver)[0]*4/5;
int startY = this.getAppScreen(driver)[1]/2;
try{
for (int i=0; i<num; i++){
driver.swipe(startX, startY, endX, startY, duration);
Thread.sleep(1000);
}
Log.info("滑动成功...");
}catch (Exception e){
Log.error("滑动失败...");
e.printStackTrace();
}
}

/**
* 滑动指定元素
* 参数:duration 持续时间毫秒单位即滑动速度,num滑动次数
* return  无
**/
public void slidingElement(WebElement element,String direction,int duration, int num){
//XY为元素起点坐标
int x = element.getLocation().getX();
int y = element.getLocation().getY();
//获取元素高和宽
int elementWidth = element.getSize().getWidth();
int elementHeight = element.getSize().getHeight();
switch (direction){
case "Left":
int starX =x+elementWidth*3/4;
int endX =x+elementWidth/4;
int startY =elementHeight/2+y;
int endY =elementHeight/2+y;
for(int i=0; i<num; i++){
driver.swipe(starX, startY, x, endY, duration);
}
break;
case "Right":
int rRtarX =x+elementWidth/4;
int rEndX =x+elementWidth*3/4;
int RStartY =elementHeight/2+y;
int REndY =elementHeight/2+y;
for(int i=0; i<num; i++){
driver.swipe(rRtarX, RStartY, rEndX, REndY, duration);
}
break;
default:break;

}

}

/**
* 手势解锁九宫格
* 0 1 2 3 4 5 6 7 8
*/
public void swipeToUnlock(AndroidDriver driver, WebElement lockImageView, int[] path) {
TouchAction touchAction = new TouchAction(driver);
List<WebElement> lockItems = lockImageView.findElements(By.className("android.view.View"));
for (int i = 0; i < path.length; i++) {
if (i == 0) {
touchAction.press(lockItems.get(path[i])).moveTo(lockItems.get(path[i]));
} else {
touchAction.moveTo(lockItems.get(path[i]));
}
}
touchAction.release();
touchAction.perform();
}

}
/**
* Created by kingwit on 2017/9/5 0005.
* 该settext 方法可忽略,不用封装
*/

public class TextMethodObject {

/**
* Created by Administrator on 2017/7/28 0028.
* Text输入
* 参数: WebElement 对象和text内容
**/
public void setText(WebElement elementEdit, String text){
elementEdit.sendKeys(text);
}

/**
* Created by Administrator on 2017/7/28 0028.
* Text获取
* 参数: WebElement 对象和text内容
**/
public String  getText(WebElement elementEdit){
return elementEdit.getText();
}

/**
* Created by Administrator on 2017/7/28 0028.
* 清除输入
* 参数: WebElement
**/
public void clearText(WebElement elementEdit){
elementEdit.clear();
}

/**
* Created by Administrator on 2017/7/28 0028.
* 长按操作
* 参数: WebElement 对象driver
**/
public void longPressByID(AndroidDriver driver, WebElement items){
TouchAction tAction=new TouchAction(driver);
tAction.longPress(items).perform();

}

/**
* toast 获取
* 参数:
* 暂未验证,需要升级appium
**/
public  boolean getToast(AndroidDriver driver, String toast) {
try {
final WebDriverWait wait = new WebDriverWait(driver, 1);
if (wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[contains(@text,'"+toast+"')]")))!=null) {
Log.info("找到了toast");
return true;
}
return false;
} catch (Exception e) {
Log.error("找不到toast:" + toast);
return false;
}
}

}


/**
* Created by kingwit on 2017/9/5 0005.
* 线程休眠函数
*/

public class ThreadSleepMethod {

public static void threadSleep(int sleepSecond){
try{
Thread.sleep(sleepSecond);
}catch (Exception e){
e.printStackTrace();
}

}
}


/**
* Created by kingwit on 2017/9/5 0005.
*  定义logger父类,所有类打印log调用此方法
*  可选择log写入文件
*/

import java.io.File;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.logging.LogRecord;
public class Log {
public static Logger log=Logger.getLogger("Mylog");
public static FileHandler myfileHandler =null;
static int i=0;

/**
* 写入日志 info 级别
* 参数: 无
**/
public static void  info(String msg)  {
log.setLevel(Level.INFO);
if(i==0){
getFileHandler();
i++;
System.out.print("-----");
}
log.info("[yuntuTVTest] "+getDataTime() +" :"+msg);

}

/**
* 写入日志 严重 级别
* 参数: 无
**/
public static void  error(String msg)  {
log.setLevel(Level.INFO);
if(i==0){
getFileHandler();
i++;
System.out.print("-----");
}
log.severe("[yuntuTVTest] "+getDataTime() + " :"+msg);
}

/**
* 格式化当前日期
* 参数: 无
* return 格式化后的日期
**/
public static String getDataTime(){
String dataTimeString = null;
SimpleDateFormat date=new SimpleDateFormat("<yyyy-MM-dd HH:mm:ss> ");
dataTimeString=date.format(new Date());
return dataTimeString;
}

/**
* 创建唯一FileHandler对象
* 参数: 无
* return 无
**/
public static void  getFileHandler(){
String path="d://FTP//AutoTest//test.log/";
File file=new File(path);
if(file.exists() && myfileHandler == null){
file.delete();
}
if(!file.exists() && myfileHandler == null){
try {
myfileHandler = new FileHandler("d://FTP//AutoTest//test.log/");
myfileHandler.setLevel(Level.INFO);
myfileHandler.setFormatter(new myFormatter()); //格式化loggr,即去掉XML格式
log.addHandler(myfileHandler);  //输出到文件  这个必须写在这里,只能调用一次。在其他地方调用会打印几次
} catch (IOException e) {
myfileHandler = null;
e.printStackTrace();
}
}else {
return;
}

}
}

//重写formater抽象方法,Formatter是抽象类
class myFormatter extends Formatter {

@Override
public String format(LogRecord record) {
// TODO Auto-generated method stub
return record.getLevel() + " : " + record.getMessage()+"\r\n";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: