1.在utils包中创建一个AppiumUtil类,这个类是对appium api进行封装的。
代码如下:
package utils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.ITestResult;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.NoSuchContextException;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
/**
* @author young
* @description appium api封装
* */
public class AppiumUtil {
public AppiumDriver<WebElement> driver;
public ITestResult it;
/**定义日志输出对象*/
public static Logger logger = Logger.getLogger(AppiumUtil.class);
/**获取driver
* @throws */
public AppiumDriver<WebElement> getDriver(String url,DesiredCapabilities capabilities,String platform){
if(platform.equalsIgnoreCase("android")){
try {
driver = new AndroidDriver<WebElement>(new URL(url), capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}else if(platform.equalsIgnoreCase("ios")){
try {
driver = new IOSDriver<WebElement> (new URL(url),capabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}else{
}
return driver;
}
/**退出app*/
public void closeApp(String appName){
driver.closeApp();
logger.info(appName+"已经关闭");
}
/**退出移动浏览器*/
public void quit(){
driver.quit();
logger.info("driver已被清理");
}
/**通过By对象 去查找某个元素*/
public WebElement findElement(By by){
return driver.findElement(by);
}
/**
* 通过By对象 去查找一组元素
* */
public List<WebElement> findElements(By by) {
return driver.findElements(by);
}
/**清空元素内容*/
public void clear(By byElement){
WebElement element = findElement(byElement);
element.clear();
logger.info("清空元素:"+getLocatorByElement(element, ">")+"上的内容");
}
/**输入内容*/
public void typeContent(By byElement,String str){
WebElement element = findElement(byElement);
element.sendKeys(str);
logger.info("在元素:"+getLocatorByElement(element, ">")+"输入内容:"+str);
}
/**点击*/
public void click(By byElement){
WebElement element = findElement(byElement);
try{
element.click();
logger.info("点击元素:"+getLocatorByElement(element, ">"));
}catch(Exception e){
logger.error("点击元素:"+getLocatorByElement(element, ">")+"失败", e);
Assert.fail("点击元素:"+getLocatorByElement(element, ">")+"失败", e);
}
}
/**查找一个元素 - appium新增的查找元素方法*/
public WebElement findElement(String locateWay,String locateValue){
WebElement element = null;
switch(locateWay){
case "AccessibilityId":
element = driver.findElementByAccessibilityId(locateValue);
break;
// case "AndroidUIAutomator":
// element = driver.findElementByAndroidUIAutomator(locateValue);
// break;
case "ClassName":
element = driver.findElementByClassName(locateValue);
break;
case "CSS":
element = driver.findElementByCssSelector(locateValue);
break;
case "ID":
element = driver.findElementById(locateValue);
break;
case "LinkText":
element = driver.findElementByLinkText(locateValue);
break;
case "Name":
element = driver.findElementByName(locateValue);
break;
case "PartialLinkText":
element = driver.findElementByPartialLinkText(locateValue);
break;
case "TagName":
element = driver.findElementByTagName(locateValue);
break;
case "Xpath":
element = driver.findElementByXPath(locateValue);
break;
default:
logger.error("定位方式:"+locateWay+"不被支持");
Assert.fail("定位方式:"+locateWay+"不被支持");
}
return element;
}
/**查找一组元素 - appium新增的查找元素方法*/
public List<?> findElements(String locateWay,Strin