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("繼續嘗試查找:"+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";
}
}