How to enable and use WebView for iOS Automation in Appium

孤者浪人 提交于 2019-12-11 00:43:00

问题


I am trying to automate hybrid app using Appium developed by Ionic2/Angular2/Typescript.

When I am trying to identify element I am able to see element in WebView in Appium Inspector but when I try to identify them using script it is throwing error.

An element could not be identified using given search parameter.

error: Invalid locator strategy: partial link text

I am trying to identify elemnet through Xpath, name, linkText but I am not able to do so.

I also enable webview in capabilities by adding

capabilities.SetCapability("autoWebView", "true");

I am using C# for automating. Can anybody provide the solution?

When I use Xpath it shows An unknown server side error occured while processing the command (Original error: connect ECONNREFUSED)

Above error is displayed when I call var contextNames= driver.Contexts; after setting the above capability. Do I also need to set capability for browser???


回答1:


Two things :

1. Ensure setWebContentsDebuggingEnabled is set to true in your webview code.

2. Before accessing the elements on a webview, switch the context and back after the operations. Something similar to the below code in Java for switching to WEBVIEW :

Set<String> contextNames = driver.getContextHandles();
String setContext = contextNames.toArray()[1].toString();
driver.context(setContext);// set context to WEBVIEW_com.my.package

Please do read the following for What exactly do autoWebview does?

For dot-net client, a similar code goes as :

var contextNames = driver.GetContexts(); //correction to your code 
driver.SetContext(contextNames[1]); // for webview_1, for native_view 0



回答2:


1) First option

Below code will help to handle Native app to webview

public void acceptalert(){
    Set<String> contextNames = idriver.getContextHandles();

    System.out.println(contextNames);

    for (String contextName : contextNames) {
      if (contextName.contains("NATIVE_APP")) {
        Reporter.log("Reaching to Native App", true);
        idriver.context(contextName);
        idriver.findElementByName("Open").click();
        Reporter.log("Clicking Open to naviagte to Native APP", true);
      }
      else{
        Reporter.log("Not found", true);
      }
    }
}

2) Second option :

try {
            //idriver.context("WEBVIEW_2");

            Set<String> contextNames1 = idriver.getContextHandles();
            for (String winHandle : idriver.getWindowHandles())
            {
                if (winHandle.contains("WEBVIEW_24")){
                    ((AppiumDriver) idriver).context(winHandle);
                    System.out.println("Switched to " + winHandle);
                    idriver.switchTo().window(winHandle);
                    idriver.findElementByName("Open").click();
                }

                else if (winHandle.contains("NATIVE_APP")) {
                    ((AppiumDriver) idriver).context(winHandle);
                    idriver.switchTo().window(winHandle);
                    System.out.println("Switched to " + winHandle);
                }
            }

        } catch (Exception e) {

        }



回答3:


Even after using

capabilities.SetCapability("autoWebView", "true");

We need to access proxy to handle webview

Set<String> contextNames = idriver.getContextHandles();

    System.out.println(contextNames);

    for (String contextName : contextNames) {
      if (contextName.contains("NATIVE_APP")) {
        Reporter.log("Reaching to Native App", true);
        idriver.context(contextName);
        idriver.findElementByName("Open").click();
        Reporter.log("Clicking Open to naviagte to Native APP", true);
      }
      else{
        Reporter.log("Not found", true);
      }
    }

or try to ru proxy to handle webview -- just a hack

Ru below on terminal

ios_webkit_debug_proxy -c abad62540cbfc9f2af5c154985420a856e:27753 -d


来源:https://stackoverflow.com/questions/37226537/how-to-enable-and-use-webview-for-ios-automation-in-appium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!