appium java 时间控件_Android应用开发之AS+Appium+Java+Win自动化测试之Appium的Java测试脚本封装(Android测试)...

本文将带你了解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频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值