本文将带你了解Android应用开发AS+Appium+Java+Win自动化测试之Appium的Java测试脚本封装(Android测试),希望本文对大家学Android有所帮助。
一、为什么需要封装?
封装的本意就是为了方便、简洁。
二、Android的显式等待封装
1. AndroidDriverWait.java
package com.example.base;
/**
* Created by LITP on 2016/9/8.
*/
import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Clock;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Sleeper;
import org.openqa.selenium.support.ui.SystemClock;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.android.AndroidDriver;
public class AndroidDriverWait extends FluentWait {
public final static long DEFAULT_SLEEP_TIMEOUT = 500;
private final WebDriver driver;
public AndroidDriverWait(AndroidDriver driver, long timeOutInSeconds) {
this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, DEFAULT_SLEEP_TIMEOUT);
}
public AndroidDriverWait(AndroidDriver driver, long timeOutInSeconds, long sleepInMillis) {
this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER, timeOutInSeconds, sleepInMillis);
}
public AndroidDriverWait(AndroidDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds,
long sleepTimeOut) {
super(driver, clock, sleeper);
withTimeout(timeOutInSeconds, TimeUnit.SECONDS);
pollingEvery(sleepTimeOut, TimeUnit.MILLISECONDS);
ignoring(NotFoundException.class);
this.driver = driver;
}
@Override
protected RuntimeException timeoutException(String message, Throwable lastException) {
TimeoutException ex = new TimeoutException(message, lastException);
ex.addInfo(WebDriverException.DRIVER_INFO, driver.getClass().getName());
if (driver instanceof RemoteWebDriver) {
RemoteWebDriver remote = (RemoteWebDriver) driver;
if (remote.getSessionId() != null) {
ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());
}
if (remote.getCapabilities() != null) {
ex.addInfo("Capabilities", remote.getCapabilities().toString());
}
}
throw ex;
}
}
2.ExpectedCondition.java
接口
package com.example.base;
import com.google.common.base.Function;
import io.appium.java_client.android.AndroidDriver;
/**
* Created by LITP on 2016/9/8.
*/
public interface ExpectedCondition extends Function {}
3. 使用
/**
* 显示等待,等待Id对应的控件出现time秒,一出现马上返回,time秒不出现也返回
*/
public AndroidElement waitAuto(By by, int time) {
try {
return new AndroidDriverWait(driver, time)
.until(new ExpectedCondition() {
@Override
public AndroidElement apply(AndroidDriver androidDriver) {
return (AndroidElement) androidDriver.findElements(by);
}
});
} catch (TimeoutException e) {
Assert.fail("查找元素超时!! " + time + " 秒之后还没找到元素 [" + by.toString() + "]", e);
return null;
}
}
三、Assert断言的封装
封装了 出现错误输出了异常信息但不终止程序的运行,会继续往下执行。
1.Assertion.java
package com.example.base;
import org.testng.Assert;
import java.util.ArrayList;
import java.util.List;
/**
* Created by LITP on 2016/9/21.
*/
public class Assertion {
public static boolean flag = true; //是否有错误
public static List errors = new ArrayList<>(); //错误集合
/**
* 验证值是否相等
* @param actual 第一个值
* @param expected 要对比的值
*/
public static void verifyEquals(Object actual, Object expected){
try{
Assert.assertEquals(actual, expected);
}catch(Error e){
errors.add(e);
flag = false;
}
}
/**
* 验证值是否相等
* @param actual 第一个值
* @param expected 要对比的值
* @param message 出错时候的提示消息
*/
public static void verifyEquals(Object actual, Object expected, String message){
try{
Assert.assertEquals(actual, expected, message);
}catch(Error e){
errors.add(e);
flag = false;
}
}
}
2.AssertionListener.java
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!