Appium Appium 显示等待,设置超时时间但是不起作用,麻烦大家帮看看

Pastel · 2016年12月30日 · 最后由 Pastel 回复于 2016年12月30日 · 1805 次阅读

在网上查找了等待控件的方法,自己去实践了一下,
在使用 WebDriverWait 进行显示等待时设置超时时间为 5 秒,但是并 5s 内没有出现控件时并没有抛出异常
麻烦大家帮看看,谢谢啦
问题描述:
问题 1、App 端是自己写的小 Demo,TextView 做了延迟 60s 显示,60s 后延迟显示坐标经纬度
WebDriverWait 设置了 5 秒的超时时间,理论上应该是会超时抛异常的,但是我发现并没有,不知道是为什么
问题 2、WebDriverWait 设置的超时和全局设置的隐示超时时间(driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);)有什么关系?
代码如下:

public class GPStest {

    public static void main(String[] args) throws InterruptedException {
        DesiredCapabilities capabilities = CapabilitiesTest
                .getAndroidCapabilities();
        AndroidDriver<WebElement> driver = null;
        try {
            driver = new AndroidDriver<WebElement>(new URL(
                    Capabilities.getUrl()), capabilities);
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

        printCurrentTime(1);
        waitForVisible(driver,
                By.xpath("//android.widget.TextView[contains(@text,'纬度')]"), 5);
        printCurrentTime(6);
        driver.quit();
    }

    public static void waitForVisible(WebDriver driver, final By by,
            int waitTime) {
        driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        WebDriverWait wait = new WebDriverWait(driver, waitTime);
        try {
            printCurrentTime(2);
            wait.until(ExpectedConditions.visibilityOfElementLocated(by));
            System.out.println("find");
            printCurrentTime(3);
        } catch (Exception e) {
            System.out.println("not find");
        }
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    }

    public static void printCurrentTime(int count) {
        System.out.print("count=" + count + "###");
        Date nowTime = new Date(System.currentTimeMillis());
        SimpleDateFormat sdFormatter = new SimpleDateFormat(
                "yyyy-MM-dd:hh-mm-ss");
        System.out.println(sdFormatter.format(nowTime));
    }
}

控制台信息显示如下:
count=1###2016-12-30:11-12-42
count=2###2016-12-30:11-12-47
find
count=3###2016-12-30:11-13-52
count=6###2016-12-30:11-13-52
Demo App 界面如下:

环境信息:
windows 7
Appium 版本:windows 安装包版本 1.4.16.1
Appium 客户端版本:java 4.1.2

共收到 5 条回复 时间 点赞
恒温 内容不符合版规屏蔽此话题 12月30日 12:23

@Lihuazhang 已经把图片删除掉了,用的文本形式的
第一次发帖请见谅,谢谢啦

Pastel 关闭了讨论 12月30日 12:56
恒温 重新开启了讨论 12月30日 13:00

将定位方式用的 By 类换成 MobileBy,可以尝试下。。

#5 楼 @jingjing0506 By.id 是可以的,By.xpath 不行,具体为什么不知道
我也是按照你提示的说试一了下 MobileBy,随手就改了一下用 id 查找就好用了,以为是 MobileBy 的原因
再试的时候发现用 MobileBy.path 也是不行的

#6 楼 @Pastel 我这边尝试用了下 xpath,是可以的。。看下是不是你的 XPath 定位有问题吧。

#7 楼 @jingjing0506 xpath 应该没问题,因为之前都能找到控件了,你看我帖子里面写得 1 分钟都没有抛异常,提示找到了控件

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册