简体   繁体   English

如何使用appium自动化/测试Android混合应用程序?

[英]how to Automate/test Android hybrid apps using appium?

I trying to automate a hybrid app using the appium. 我试图使用appium自动化混合应用程序。 i had completed the total setup ready and also testing using sample apk file. 我已经完成了总设置,并使用示例apk文件进行了测试。 I facing problem in getting the object properties for my hybrid app. 我在获取混合应用程序的对象属性时遇到问题。 I am not able to inspect ids using Appium inspector or uiautomatorviewer. 我无法使用Appium inspector或uiautomatorviewer检查ID。 It shows only one class for my app . 它只显示我的应用程序的一个类。

i also need to enable WebView debugging, for making setWebContentsDebugging Enabled 我还需要启用WebView调试,以使setWebContentsDebugging启用

to true on the WebView class.can any one help me how to do that? 在WebView类上为true。有人可以帮助我做到这一点吗?

some of the blogs are saying to keep driver.context("web_view"); 一些博客说要保留driver.context(“ web_view”); but i not clear how to get that. 但我不清楚如何获得。 please help to to solve this. 请帮助解决此问题。 thanks. 谢谢。

this is my java class 这是我的java课

    public class myMavenTest {
                private WebDriver driver;
                //i think is not the way to do this.so i comented this.
                /*public void onCreate(){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                         if(0 != (getApplicationInfo().flags = ApplicationInfo.FLAG_DEBUGGABLE)){
                             WebView.setWebContentsDebuggingEnabled(true);
                         }
                    }
                }*/

                @BeforeTest
                public void setUp() throws Exception
                {
                System.out.println("in the setup function");
                    DesiredCapabilities capabilities = new DesiredCapabilities();
                    capabilities.setCapability(CapabilityType.BROWSER_NAME, "");

                           capabilities.setCapability("deviceName","My Android");   
                           capabilities.setCapability("platformVersion","5.1");                             
                           capabilities.setCapability("platformName","Android");
    capabilities.setCapability("appPackage","com.mysoftware.testapp");  
    capabilities.setCapability("appActivity","com.mysoftware.testapp.MainActivity");

                try
                {
                    driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

                    driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
                    //Thread.sleep(10000);
                }
                catch(MalformedURLException e)
                {
                e.printStackTrace();
                }
                }

                @Test
                public void Loginforsample() throws Exception
                {
                    System.out.println("in the login() function");

        //i tried using classname of my app. but it is not recognizing
                    driver.findElement(By.className("ink-dark")).click();
        //After the button clicks need to enter the text
        driver.findElement(By.xpath("//need to find xpath'")).sendKeys("My first Automation");

        //tried using selendroid.apk works fine here.
/*driver.findElement(By.id("io.selendroid.testapp:id/startUserRegistration")).click();*/                


                    Thread.sleep(10000);

                }

                @AfterTest
                public void tearDown() throws InterruptedException
                {
                    Thread.sleep(10000);
                driver.quit();
                }
            }

Use below the sample Hybrid App code which I have written for a combination of WEBView Native View. 在我为WEBView本机视图的组合编写的示例混合应用程序代码下面使用。 Hope it helps for you! 希望对您有帮助!

public class Hybrid_App {

public static void main(String[] args) throws MalformedURLException, InterruptedException{

    DesiredCapabilities capabilities = new DesiredCapabilities();

    capabilities.setCapability("deviceName", "Atom 2x");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("platformVersion", "5.1");

    capabilities.setCapability("appPackage","***YourHydridAppPkg****");
    capabilities.setCapability("appActivity", "****YourlauchableActivity***");

    AndroidDriver driver= new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

    Thread.sleep(4000);

    Set<String> contextHandles = driver.getContextHandles();

    Map<String,String> hashMap= new HashMap<String,String>();

    for(String contextname:contextHandles){

        if(contextname.contains("NATIVE")){

            hashMap.put("native", contextname);

        }else{
            hashMap.put("webview", contextname);
        }
    }

    //native page - Native 
    driver.context(hashMap.get("native"));

    WebDriverWait wait= new WebDriverWait(driver, 50);
    WebElement ele_voucher = wait.until(ExpectedConditions.visibilityOfElementLocated(
            By.xpath("//*[@class='android.view.View'][@index='9'][@content-desc='VOUCHERS']")));

    System.out.println(ele_voucher.isDisplayed());

    Thread.sleep(6000);
    ele_voucher.click();

    Thread.sleep(9000);

    //second page - Native
    driver.context(hashMap.get("native"));

    Thread.sleep(5000);
    driver.findElementByXPath("//*[@class='android.view.View'][@index='17'][@content-desc='REDEEM']").click();

    Thread.sleep(8000);

    System.out.println("----Third page----"+" uname,pwd element");

    //third page - Webview
    driver.context(hashMap.get("webview"));

    Thread.sleep(6000);

    driver.findElementByXPath("//input[@class='x-input-email'][@type='email'][@name='email']").sendKeys("descbatch@gmail.com");

    WebElement ele_pwd = driver.findElementByXPath("//input[@class='x-input-password'][@type='password'][@name='password']");

    ele_pwd.click();
    Thread.sleep(4000);
    ele_pwd.sendKeys("12345");

    Thread.sleep(6000);


    System.out.println("----Third page----"+" Sign in element");

    //fourth page - Native
    driver.context(hashMap.get("native"));

    Thread.sleep(6000);
    driver.findElementByXPath("//*[@class='android.view.View'][@index='69'][@content-desc='SIGN IN']").click();
    Thread.sleep(6000);

    driver.sendKeyEvent(AndroidKeyCode.BACK);


}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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