Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
fu-qiang_lv
Member
0 Kudos

Selendroid is a test automation framework which drives off the UI of Android native and hybrid applications (apps) and the mobile web. Tests are written using the Selenium 2 client API - that's it!

Selendroid: Selenium for Android  


When try to automation Cordova app(cordova 3.6.x or above) with Selendroid, it failed with error ClassCastException. The root cause of this issue is the cordova has changed some code for webview. It will convert the WebviewClient to CordovaWebViewClient, so when Selendroid try to set the WebViewClient to Webview, it will throw exception and crashed.

The code in CordovaWebView:


    @Override
    public void setWebViewClient(WebViewClient client) {
        this.viewClient = (CordovaWebViewClient)client;
        super.setWebViewClient(client);
    }
    @Override
    public void setWebChromeClient(WebChromeClient client) {
        this.chromeClient = (CordovaChromeClient)client;
        super.setWebChromeClient(client);
    }

I found a workaround for this issue. I tried to build the selendroid-server to use CordovaChromeClient and override onJsAlert instead of SelendroidWebChromeClient.

First, create a new class and extends CordovaChromeClient. In this class override onJsAlert with SelendroidWebChromeClient's onJsAlert method.

for example:


public class SelfWebChromeClient extends CordovaChromeClient{
   
     @Override
     public boolean onJsAlert(WebView view, String url, String message, JsResult jsResult) {
             //code copied from SelendroidWebDriver
     }
     ......
}

Then check the view type, if it is Cordova web view, create a new SelfWebChromeClient:


if (view instanceof CordovaWebView) {
     chromeClient = new SelfWebChromeClient();
} else {
     chromeClient = new SelendroidWebChromeClient();
}


Before build the Selendroid library, you need to add the Cordova library to maven local repository, then add the cordova library to the project as a runtime dependency.

How to add jar to maven local repository: Maven – Guide to installing 3rd party JARs


mvn install:install-file -Dfile=path-to-your-artifact-jar \


       -DgroupId=your.groupId \


       -DartifactId=your-artifactId \


       -Dversion=version \


       -Dpackaging=jar