在Android模拟器

时间:2018-04-13 19:25:44

标签: android google-chrome appium selenium-chromedriver

我正在Chrome上安装Android(v65)的​​网站(7.1.1 API 25)。

Chromedriver版本2.37。 Appium最新桌面版(v1.5.0,显示服务器版本1.7.2)

我需要从设备本身上传图片。 虽然在网络浏览器上我能够使用sendKeys上传图像,但它无法处理android emulator

这就是模拟器中的图像选择页面:Images from the Camera Folder

我使用UiAutomator获取资源ID:“com.android.chrome:id/bitmap_view”和类:“android.widget.ImageView”

我使用以下代码行来尝试查找元素:

(a)

driver.findElement(By.xpath("//android.widget.ImageView[contains(@resource-id,'com.android.chrome:id/bitmap_view')]")).click();

(b)当我在下面使用时,我得到一个空列表

List<WebElement> elements = driver.findElementsByClassName("android.widget.ImageView");

(c)我从Appium Inspector Session获得了这个xpath

MobileElement el8 = (MobileElement) driver.findElementByXPath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.support.v7.widget.Af/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout/android.support.v7.widget.RecyclerView/android.widget.FrameLayout[3]/android.widget.FrameLayout/android.widget.ImageView");
el8.click();

我不知道我错过了什么。

[更新] [UiAutomator showing clickable=false] 2

1 个答案:

答案 0 :(得分:1)

您是否在测试中将上下文更改为 NATIVE_APP

由于您提到了测试chrome,这意味着您的测试使用 WEBVIEW 上下文。但是一旦打开上传文件屏幕,它就是 NATIVE_APP 视图,您需要在开始搜索之前切换到它:

AndroidDriver<MobileElement> driver = new AndroidDriver<>(
            new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get(<your_web_site>);
// do what you need in browser, open Upload files screen

// switch to Native context to search inside it
Set<String> contexts = driver.getContextHandles();
for (String context : contexts) {
    System.out.println(contexts);
    if (context.equals("NATIVE_APP")) {
        driver.context(context);
        break;
    }
}
List<MobileElement> images = driver.findElementsByClassName("android.widget.ImageView");
// click the last image in view if exist
images.stream()
    .reduce((first, second) -> second)
    .orElseThrow(NotFoundException::new)
    .click();