Switch to Webview

1,668 views
Skip to first unread message

Ahmad Hajou

unread,
May 12, 2014, 4:49:56 AM5/12/14
to appium-...@googlegroups.com
I have a hybrid app using Xamarin, I am trying to switch to the Webview using appium:

capabilities.SetCapability("appium-version", "1.0");
capabilities.SetCapability("platformName", "ANDROID");
capabilities.SetCapability("platformVersion", "4.4"); //also tried 17 here
capabilities.SetCapability("deviceName", "015d2d422304040a");
capabilities.SetCapability("app", appPath);

The problem is that I am getting a Not Implemented exception when switching to Webview:
driver.SwitchTo().Window("WEBVIEW");

Note that the context only contains NATIVE_APP. I am using Android 4.4 (tried both emulator and real device)

Any help is appreciated, this issue is blocking us from using Appium


Jonathan Lipps

unread,
May 12, 2014, 4:55:18 AM5/12/14
to Ahmad Hajou, appium-...@googlegroups.com
A note: deviceName is not the same as udid. deviceName is supposed to be something like "Android Emulator" or "Nexus 7". Right now this doesn't do much of anything on Appium, but it's good to get into the habit of using it correctly.

You specify which device you want to talk to using the 'udid' capability.

Also, in 1.0, you don't use the window handles for webviews anymore. You use the context api. That's the main reason this isn't working.

--
http://appium.io
---
You received this message because you are subscribed to the Google Groups "Appium-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to appium-discus...@googlegroups.com.
Visit this group at http://groups.google.com/group/appium-discuss.
For more options, visit https://groups.google.com/d/optout.

Ahmad Hajou

unread,
May 12, 2014, 5:10:24 AM5/12/14
to appium-...@googlegroups.com, Ahmad Hajou
Thanks Jonathan,

My problem is that when i try to get the available contexts I only get 1, which is "NATIVE_APP".
Any idea why this might happen? Xamarin issue?

Jonathan Lipps

unread,
May 12, 2014, 5:20:08 AM5/12/14
to Ahmad Hajou, appium-...@googlegroups.com
Could also be a timing issue. Are you sure that when the check happens there's a webview? When you get the source of the app, do you see a webview element? Is it a chrome-backed webview?

Ahmad Hajou

unread,
May 12, 2014, 6:00:11 AM5/12/14
to appium-...@googlegroups.com, Ahmad Hajou
I do not think it is a timing issue, since I make sure it is visible, (also tried in debug)

I assume that if I am using android 4.4 then it is chrome-backed webview, is that correct? 
(or do I need to change anything in code)

Any other setting I should check?

Jonathan Lipps

unread,
May 12, 2014, 9:55:38 AM5/12/14
to Ahmad Hajou, appium-...@googlegroups.com
The next thing to check is that when you get the source of the app you see the webview in the source.

Ahmad Hajou

unread,
May 12, 2014, 10:05:46 AM5/12/14
to appium-...@googlegroups.com, Ahmad Hajou
Below is the page source I got, it does contain Webview.

<?xml version="1.0" encoding="UTF-8"?>
<hierarchy rotation="0">
   <android.widget.FrameLayout index="0" text="" resource-id="" class="android.widget.FrameLayout" package="com.mkyong.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][800,1205]">
      <android.widget.LinearLayout index="0" text="" resource-id="" class="android.widget.LinearLayout" package="com.mkyong.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][800,1205]">
         <android.widget.FrameLayout index="0" text="" resource-id="android:id/content" class="android.widget.FrameLayout" package="com.mkyong.android" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,33][800,1205]">
            <android.webkit.WebView index="0" text="" resource-id="com.mkyong.android:id/webView1" class="android.webkit.WebView" package="com.mkyong.android" content-desc="Web View" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="true" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,33][800,1205]" />
         </android.widget.FrameLayout>
      </android.widget.LinearLayout>
   </android.widget.FrameLayout>
</hierarchy>

Note I also tried:
driver.SetContext("WEBVIEW1");
driver.SetContext("WEBVIEW_1");
driver.SetContext("WEBVIEW");
. . .

Jonathan Lipps

unread,
May 12, 2014, 10:10:32 AM5/12/14
to Ahmad Hajou, appium-...@googlegroups.com
Did you run getContexts() first to see what contexts were available?

Ahmad Hajou

unread,
May 12, 2014, 10:16:10 AM5/12/14
to appium-...@googlegroups.com, Ahmad Hajou
Running GetContexts() returns only "NATIVE_APP"

Jonathan Lipps

unread,
May 12, 2014, 10:18:48 AM5/12/14
to Ahmad Hajou, appium-...@googlegroups.com
Not sure then, maybe there's actually a difference in the kind of webview this is. Maybe the apk was built with a lower API level and so it's not actually chrome-backed? Any thoughts @bootstraponline?

Ahmad Hajou

unread,
May 12, 2014, 10:28:49 AM5/12/14
to appium-...@googlegroups.com, Ahmad Hajou
Hi Jonathan,

I just tried again with rebuilding the APK to target level 19.

Still same error.

:(

bootstrap online

unread,
May 12, 2014, 10:36:49 AM5/12/14
to Ahmad Hajou, appium-...@googlegroups.com
I think I'd need the apk to investigate further. If the app uses the
standard webview then by default on API 19 it should just work.

Jonathan Lipps

unread,
May 12, 2014, 10:37:40 AM5/12/14
to bootstrap online, Ahmad Hajou, appium-...@googlegroups.com
Try to connect to it using Chrome remote debugger

Ahmad Hajou

unread,
May 12, 2014, 11:08:35 AM5/12/14
to appium-...@googlegroups.com, bootstrap online, Ahmad Hajou
I rebuild my demo app to add the WebView.SetWebContentsDebuggingEnabled(true); and now I can switch to the WEBVIEW.
Thanks @Jonathan

However I still cannot do that on my production app, @bootstraponline can you take a look at the APK, where shall I send it? (my colleagues think I should not post the APK here on a public forum)

Ahmad Hajou

unread,
May 12, 2014, 11:17:00 AM5/12/14
to appium-...@googlegroups.com, bootstrap online, Ahmad Hajou
The new error I am getting:
{"An unknown server-side error occurred while processing the command. (Original error: Did not get session redirect from Chromedriver)"}

bootstrap online

unread,
May 12, 2014, 11:19:30 AM5/12/14
to Ahmad Hajou, appium-...@googlegroups.com
You can send it to: ma...@bootstraponline.com

bootstrap online

unread,
May 12, 2014, 6:08:58 PM5/12/14
to Ahmad Hajou, appium-...@googlegroups.com
I tested the apk and had no problem automating the webview.

# example of automating inputs in webview mode
set_context 'WEBVIEW_1'
inputs = xpaths('//input')
inputs[0].type 'domain'
inputs[1].type 'username'
inputs[2].type 'password'

The webview can sometimes be automated from the native context as well.
set_current_context 'NATIVE_APP'
textfield(1).type 'domain'
textfield(2).type 'username'
textfield(3).type 'password'
find_exact('sign in').click

The problem there is it doesn't seem to load until the textfield in
the webview is clicked so using the webview context is more reliable.

Ahmad Hajou

unread,
May 14, 2014, 2:38:56 AM5/14/14
to appium-...@googlegroups.com
Thanks @bootstraponline and @Jonathan , it seems I can only switch context to WEBVIEW if I press on any of the textboxes.

I will look more into this to see if I can do that programatically from inside the app.

Thanks again for all the help

bootstrap online

unread,
May 14, 2014, 9:32:49 AM5/14/14
to Ahmad Hajou, appium-...@googlegroups.com
I was able to switch to the webview as soon as the app started without
having to press on a textbox. I tested using API 19.
Message has been deleted

Jeremy Plunt

unread,
Aug 8, 2014, 8:05:22 AM8/8/14
to appium-...@googlegroups.com, aha...@gmail.com
You wrote you could switch to the webview mode. Could you give a hint how to do this if I start the java junit test with appium for webapps?
What kind of methods/commands you send after the capabilities or did you set a special capability?

I use a opensuse linux, appium 1.2.0, java and nexus 7 hardware device.

cheers

bootstrap online

unread,
Aug 8, 2014, 8:57:47 AM8/8/14
to Jeremy Plunt, appium-...@googlegroups.com, Ahmad Hajou
set_context is how you change the context from native to webview.
Alternatively sometimes uiautomator lets you view webview elements
from within the native context (it's a bit flaky).

Jeremy Plunt

unread,
Aug 8, 2014, 9:24:59 AM8/8/14
to appium-...@googlegroups.com, jerem...@gmail.com, aha...@gmail.com
bootstrapoline, thanks for quick:

i tried
using: driver.context("WEBVIEW");
     org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (Original error: Did not get session redirect from Chromedriver) 
     (WARNING: The server did not provide any stacktrace information)
     Command duration or timeout: 1.16 seconds
     Build info: version: '2.42.2', revision: '6a6995d31c7c56c340d6f45a76976d43506cd6cc', time: '2014-06-03 10:52:47'
  
using: driver.switchTo().window("WEBVIEW");
     org.openqa.selenium.WebDriverException: Not yet implemented. Please help us:
     http://appium.io/get-involved.html (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 8 milliseconds
     Build info: version: '2.42.2', revision:  '6a6995d31c7c56c340d6f45a76976d43506cd6cc', time: '2014-06-03 10:52:47'


my general question is, can I use appium on an opensuse linux machine to automate tests on a cordova/phonegap apk webApp?

In the following I send you the junit example code I use to get it running on the webApp, but it fails.
May I ask you to take a lock?

************************************************
import io.appium.java_client.AppiumDriver;
import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

/*
 */
public class AndroidTestCaseGaming {
    private AppiumDriver driver;

    @Before
    public void setUp() throws Exception {
        // set up appium
        File classpathRoot = new File(System.getProperty("user.dir"));
        File appDir = new File(classpathRoot, "../../development/appsUtest/StepFolio");
        File app = new File(appDir, "stepfolio.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
//        capabilities.setCapability("automationName","Selendroid");
        capabilities.setCapability("deviceName","Nexus 7");
        capabilities.setCapability("device", "selendroid");
        capabilities.setCapability("platformVersion", "4.4");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("app", app.getAbsolutePath());
        capabilities.setCapability("appPackage", "de.ergovia.stepfolio");
        capabilities.setCapability("appActivity", ".stepfolio");
        driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
       //driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

    @Test
    public void addContactTest() throws InterruptedException{
   

        Thread.sleep(10000);
        //System.out.println(driver.getPageSource());

        driver.switchTo().window("WEBVIEW");
        //driver.switchTo().context("WEBVIEW");
        //driver.context("WEBVIEW");
        
    //WebElement el = driver.findElement(By.id("de.ergovia.stepfolio:id/ember521"));
    //System.out.println(el);
        //el.click();
   
    }
}

************************************************

Jeremy Plunt

unread,
Aug 8, 2014, 9:32:38 AM8/8/14
to appium-...@googlegroups.com, jerem...@gmail.com, aha...@gmail.com
Here the debug-Infos from the appium-server:

debug: Appium request initiated at /wd/hub/session/1849fbc5-9b0e-476a-9864-64885a831bfe/context
debug: Request received with params: {"name":"WEBVIEW"}
debug: Getting a list of available webviews
debug: executing: "/opt/adt-bundle-linux-x86_64-20140321/sdk/platform-tools/adb" -s 09142cfb shell "cat /proc/net/unix"
debug: WEBVIEW_9140 mapped to pid 9140
debug: Getting process name for webview
debug: executing: "/opt/adt-bundle-linux-x86_64-20140321/sdk/platform-tools/adb" -s 09142cfb shell "ps"
debug: Parsed pid: 9140 pkg: de.ergovia.stepfolio
debug: from: u0_a130,9140,179,1068948,123428,ffffffff,00000000,R,de.ergovia.stepfolio
debug: returning process name: de.ergovia.stepfolio
debug: Available contexts: 
debug: ["WEBVIEW_de.ergovia.stepfolio"]
debug: Available contexts: NATIVE_APP,WEBVIEW_de.ergovia.stepfolio
debug: Connecting to chrome-backed webview
debug: Creating Chrome session
debug: Ensuring Chromedriver exists
debug: Killing any old chromedrivers, running: ps -e | grep /home/ERGOVIA/eckhardtc/.linuxbrew/lib/node_modules/appium/build/chromedriver/linux/chromedriver | grep -v grep |grep -e '--port=9515$' | awk '{ print $1 }' | xargs kill -15
debug: No old chromedrivers seemed to exist
debug: Spawning chromedriver with: /home/ERGOVIA/eckhardtc/.linuxbrew/lib/node_modules/appium/build/chromedriver/linux/chromedriver
debug: [CHROMEDRIVER] Starting ChromeDriver (v2.9.248304) on port 9515
debug: Making http request with opts: {"url":"http://127.0.0.1:9515/wd/hub/session","method":"POST","json":{"sessionId":null,"desiredCapabilities":{"chromeOptions":{"androidPackage":"de.ergovia.stepfolio","androidUseRunningApp":true,"androidDeviceSerial":"09142cfb"}}}}
debug: [CHROMEDRIVER STDERR] [0,826][WARNING]: PAC support disabled because there is no system implementation
error: Chromedriver create session did not work. Status was 200 and body was {"sessionId":"8353f7115cda6be2d7356089aca0859e","status":13,"value":{"message":"unknown error: Chrome version must be >= 31.0.1650.59\n  (Driver info: chromedriver=2.9.248304,platform=Linux 3.11.10-17-desktop x86_64)"}}
debug: Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command. (Original error: Did not get session redirect from Chromedriver)","origValue":"Did not get session redirect from Chromedriver"},"sessionId":"1849fbc5-9b0e-476a-9864-64885a831bfe"}

bootstrap online

unread,
Aug 8, 2014, 11:08:23 AM8/8/14
to Jeremy Plunt, appium-...@googlegroups.com, Ahmad Hajou
Yes, you can automate that app on Android using a Linux operating
system. In terms of this specific problem, I'm not sure. I recommend
testing out the sample code.

https://github.com/appium/appium/tree/master/sample-code
Reply all
Reply to author
Forward
0 new messages