【问题标题】:Not able to locate webelements of hybrid app using appium android in webview无法在 webview 中使用 appium android 定位混合应用程序的 webelements
【发布时间】:2019-12-08 07:35:26
【问题描述】:

我从开发者那里得到了 apk,它是一个离子应用程序。我可以安装 apk 并成功启动主屏幕,切换到 webview 但它无法识别通过 uiautomoviewer 获得的 id。

每次我收到错误“错误定位元素”。尝试点击任意位置,但无法在文本字段中输入任何值或点击显示、隐藏、忘记密码链接。

这是我的脚本:

    package com.prachi.tests;

    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Set;
    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.By;
    import org.openqa.selenium.remote.DesiredCapabilities;

    import io.appium.java_client.android.AndroidDriver;
    import io.appium.java_client.android.AndroidElement;
    import io.appium.java_client.remote.MobileCapabilityType;

    public class login1 {

        public static void main(String[] args) throws MalformedURLException, InterruptedException {
            // TODO Auto-generated method stub

            AndroidDriver<AndroidElement> driver = Capabilities();
            driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

            System.out.println("Page Source: " + driver.getPageSource());

            Thread.sleep(20000);

            Set<String> context = driver.getContextHandles();
            Thread.sleep(20000);

            for (String Contextname : context) {
                System.out.println(Contextname);
            }
            driver.context("WEBVIEW_com.lodgistics.connect");

            driver.findElement(By.id("user_login")).sendKeys("prachi.j");
            driver.findElement(By.id("user_password")).sendKeys("lodgistics123");
            driver.hideKeyboard();
            driver.findElement(By.id("Sign In")).click();

            // driver.findElement(By.id("lbl_forgotpass"));
            // driver.findElement(By.id("lbl_createacc"));    
        }

        public static AndroidDriver<AndroidElement> Capabilities() throws MalformedURLException {
            // TODO Auto-generated method stub

            AndroidDriver<AndroidElement> driver;

            File fs = new File("src/test/resources/app-debug (1).apk");
            DesiredCapabilities cap = new DesiredCapabilities();
            cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus_5");

            cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
            cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "uiautomator2");
            driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);

            return driver;

        }
    }

所有 id 都通过 uiautomatorviewer 识别,但在我运行脚本时无法定位。 我还尝试在登录屏幕本身之前切换到 Webview 模式。

我在截图中粘贴了console

【问题讨论】:

  • 您是否尝试过 chrome 远程调试器进行检查? chrome://inspect/#devices
  • 你是在真机还是模拟器上运行这个测试?当我在安装了 Play 商店的情况下运行模拟器时,我从来没有设法与 webview 进行交互,不知道这是否有帮助。
  • 您的 DOM 转储并未显示这些元素甚至存在,因此此代码永远不会找到这些元素。您是否在 webview 中启用了调试(开发人员必须为您执行此操作)?

标签: java android appium hybrid


【解决方案1】:

看来,你错了:

driver.context("WEBVIEW_com.lodgistics.connect");

使用下面的代码:

Set<String> context = driver.getContextHandles();
driver.context(context.toArray()[1]);

或者:

driver.context("WEBVIEW_1");

Docs

【讨论】:

    【解决方案2】:

    由于某种原因,By.id() 停止为我开发新的 Chrome 网页视图。

    尝试使用By.xpath("//*[@id='yourID']

    如果这样做没有帮助,请尝试添加以下功能:

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setExperimentalOption("androidPackage", com.android.chrome);
    chromeOptions.setExperimentalOption("androidActivity", org.chromium.chrome.browser.customtabs.CustomTabActivity);
    chromeOptions.setExperimentalOption("androidUseRunningApp", true);
    chromeOptions.addArguments("no-sandbox");
    cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
    cap.setCapability(MobileCapabilityType.BROWSER_NAME, "");
    

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 1970-01-01
      • 2015-12-20
      • 2018-04-18
      • 2019-05-01
      • 1970-01-01
      • 2018-02-05
      • 2016-11-16
      • 2020-01-20
      相关资源
      最近更新 更多