Appium——appium安卓常用操作封装

public static void before() throws Exception {

//找到元素立即执行,找不到一直等待15秒,只要执行一次

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
Thread.sleep(3000);
}

public static void click(By by) {
driver.findElement(by).click();
/* driver.findElement(by.id(by));
driver.findElement(By.name(by));
driver.findElement(By.className(by));
driver.findElement(By.xpath(by));*/
}
public static void click(By by, int index) {
List<WebElement> lis = driver.findElements(by);
WebElement targetEle = lis.get(index);
targetEle.click();
}
public static void sendKeys(By by, String string) {
driver.findElement(by).sendKeys(string);
}
public static void sendKeys(By by, int index, String string) {
List<WebElement> lis = driver.findElements(by);
WebElement targetEle = lis.get(index);
targetEle.sendKeys(string);
}
public static String getText(By by) {
String string=driver.findElement(by).getText();
return string;
}
public static String getText(By by,int index) {
List<WebElement> lis = driver.findElements(by);
WebElement targetEle = lis.get(index);
String string=targetEle.getText();
return string;
}
public static List<WebElement> getElementsByClassAndIndex(String classname,int index){
List<WebElement> lis =null;
lis = driver.findElementsByAndroidUIAutomator("new UiSelector().className("+classname+").index("+index+")");
return lis;
}
public static List<WebElement> getElementsByClassAndIndexAndClickable(String classname,int index){
List<WebElement> lis =null;
lis = driver.findElementsByAndroidUIAutomator("new UiSelector().className("+classname+").index("+index+").clickable(true)");
return lis;
}
/***
* 长按某个元素固定时间
* @param by
* @param index
* @param duration
*/
public static void duringpress(By by, int index, int duration){
TouchAction action1 = new TouchAction(driver);
action1.press((WebElement) driver.findElements(by).get(index)).waitAction(duration);
action1.perform();
}
/***
* 简单的长按,自动放手
* @param by
* @param index
*/
public static void longpress(By by, int index){
TouchAction action = new TouchAction(driver);
WebElement el = (WebElement) driver.findElements(by).get(index);
action.longPress(el).perform();
}
/***
* 通过class输入文本
* @param driver
*/
public static void sendKeysClass(String classname,int index, String para) {
List<WebElement> lis = driver.findElementsByClassName(classname);
WebElement targetEle = lis.get(index);
targetEle.sendKeys(para);
}
/***
* 通过class点击元素
* @param driver
*/
public static void clickClass(String classname,int index) {
List<WebElement> lis = driver.findElementsByClassName(classname);
WebElement targetEle = lis.get(index);
targetEle.click();
}
/***
* 通过class获取文本内容
* @param driver
*/
public static String getTextClass(String classname,int index) {
List<WebElement> lis = driver.findElementsByClassName(classname);
WebElement targetEle = lis.get(index);
return targetEle.getText();

}
/***
* 获取id文本内容
* @param driver
*/
public static String getTextId(String id){
String string=driver.findElementById(id).getText();
return string;
}
/***
* 获取元素的数量,返回string
* @param driver
*/
public static String getIndex(By by){
List<WebElement> lis = driver.findElements(by);
String number=lis.size()+"";
return number;
}
/***
* 获取元素的数量,返回int
* @param driver
*/
public static int getindex(By by){
List<WebElement> lis = driver.findElements(by);
int number=lis.size();
return number;
}
/***
* 断言
*/
public static boolean flag = true;
public static void verifyEquals(Object actual, Object expected){
try {
Assert.assertEquals(actual,expected);
}catch(Error e){
flag = false;
}
}
public static void verifyEquals(Object actual, Object expected,String message){
try {
Assert.assertEquals(actual,expected,message);
System.out.println("检查: "+actual+"正确");
}catch(Error e){
System.out.println("检查: "+actual+"失败"+";"+"实际为: "+expected);
flag = false;
}
}
public static void verifyExist(AndroidDriver<?> driver,String Id){
try{
Assert.assertNotNull(driver.findElementById(Id));
}
catch(Exception e){
flag = false;
}
}
//具体调用
public static void AssertEquals(String actual, String expected){
flag = true;
verifyEquals(expected,actual,"比较2个数是否相等");
Assert.assertTrue(flag);
}
public void AssertEqualsExist(AndroidDriver<?> driver,String Id){
flag = true;
verifyExist(driver,Id);
Assert.assertTrue(flag);
}
/***
* 断言、不存在某元素返回正确
* @param driver
*/
public static void AssertNoExist(By by){
try{
driver.findElement(by);
Assert.assertTrue(false);

}catch(Exception e){

Assert.assertTrue(true);
};
}
public static boolean NoExist(By by){
try{
driver.findElement(by);
return false;

}catch(Exception e){

return true;
}
}
public static boolean Exist(By by){
try{
driver.findElement(by);
return true;

}catch(Exception e){

return false;
}
}
/***
* 断言、存在某元素返回正确
* @param driver
*/
public static void AssertExist(By by){
try{
driver.findElement(by);
Assert.assertTrue(true);

}catch(Exception e){

Assert.assertTrue(false);
};
}
/***
* 断言、存在某些中文
* @param txt
*/
public static void Exist(String txt){
String txt1=txt;
try{
driver.findElement(By.xpath("//android.widget.TextView[@text='"+txt1+"'"+"]"));

Assert.assertTrue(true);
System.out.println("存在:"+txt1+"检查成功");
}catch(Exception e){
Assert.assertTrue(false);
System.out.println("不存在:"+txt1+"检查失败");
};
}
public static void Existdesc(String txt){
String txt1=txt;
try{

driver.findElementByXPath("//android.view.View[contains(@content-desc,'"+txt1+"')]");
Assert.assertTrue(true);
System.out.println("存在:"+txt1+"检查成功");
}catch(Exception e){

Assert.assertTrue(false);
System.out.println("不存在:"+txt1+"检查失败");
};
}
/***
* 绝对坐标 传入长宽的像素点
* @param 宽度从左到右的像素点
* @param 长度从上到下的像素点
*/
public static void swipe(int i,int j,int k,int h,int during){
driver.swipe(i, j, k, h, during);
}
/***
* 清除文本内容
* @param driver
*/
public static void textClear(WebElement element){
//选中输入框
element.click();
//将光标移到最后
// driver.presskeycode();
// driver.sendKeyEvent(123);
//获取字符串长度
String txt = element.getText();
for(int i=0;i<txt.length();i++){
// driver.sendKeyEvent(67);//一个个的删除
}
}
/***
* 四种滑动方式
* @param driver
*/
public static void swipeToUp(int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, during);
}

public static void swipeToDown(int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during);

}

public static void swipeToLeft(int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width * 8 / 10, height / 2, width / 10, height / 2, during);

}

public static void swipeToRight(int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
driver.swipe(width / 4, height / 2, width * 3 / 4, height / 2, during);

}
/***
* 截图到\Appium_Java\Demo目录下
* @param driver
*/
public static void snapShot(TakesScreenshot drivername, String filename) {

String currentPath = System.getProperty("user.dir");
File scrFile = drivername.getScreenshotAs(OutputType.FILE);

try {
System.out.println("save snapshot path is:" + currentPath + "/"
+ filename);
FileUtils
.copyFile(scrFile, new File(currentPath + "\\" + filename));
} catch (IOException e) {
System.out.println("Can't save screenshot");
e.printStackTrace();
} finally {
System.out.println("screen shot finished, it's in " + currentPath
+ " folder");
}
}
/***
* 切换WEB页面查找元素
* @param driver
*/
public static void switchtoWeb(){
try {
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName);
if(contextName.contains("WEBVIEW")||contextName.contains("webview")){
System.out.println(contextName);
driver.context(contextName);
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
/***
* 重写点击、输入、获取文本、find
* @param driver
*/
public static WebElement element(By by) {
WebElement element = driver.findElement(by);
return element;
}
public static WebElement elements(By by, int index) {
List<WebElement> lis = driver.findElements(by);
WebElement element = lis.get(index);
return element;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值