简体   繁体   中英

Not able to locate webelements of hybrid app using appium android in webview

I got the apk from developer, it is an ionic app. I could install apk and launch home screen successfully, switched to webview but it didn't identify ids got through uiautomoviewer.

Every time I get error 'error locating elements'. Tried clicking anywhere was passed but can not enter any value in text fields or click on Show, Hide, Forgotpassword links.

Here is my script:

    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;

        }
    }

All ids were identified through uiautomatorviewer but not able to locate when I run the script. I also tried switching to Webview mode before login screen itself.

I have pasted console in screenshot.

1

It seem like, you wrong at the line:

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

Use the bellow code:

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

Or:

driver.context("WEBVIEW_1");

Docs

For some reason By.id() stopped working on new Chrome webview for me.

Try using By.xpath("//*[@id='yourID']

If that doesn't help try adding following capabilities:

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, "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM