Android Open Source - andro_auto_framework Web View Handler






From Project

Back to project page andro_auto_framework.

License

The source code is released under:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCT...

If you think the Android project andro_auto_framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.imaginea.botbot.utility;
/*from   ww w  .  jav a  2s .c  om*/
import java.util.ArrayList;
import java.util.Iterator;

import android.util.Log;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import com.jayway.android.robotium.solo.Solo;

public class WebViewHandler {

  // Clicking on link in web view
  public static void clickOnLinkInWebView(WebView browser, String link,
      Solo solo) {
    browser.getSettings().setJavaScriptEnabled(true);

    // Javascript to click on anchor link in web view
    browser.loadUrl("javascript:links=document.getElementsByTagName('a');var linkPresent=false;"
        + "for(var i=0;i<links.length;i++){"
        + "if(links[i].innerHTML=='"
        + link
        + "'){ linkPresent=true;"
        + "var event = document.createEvent('MouseEvents');"
        + "event.initMouseEvent('click', true, window, 1, 0, 0, 0, 0, false, false, false, 1, document.body.parentNode);"
        + "links[i].dispatchEvent(event);"
        + "}"
        + "} if(linkPresent==false){console.log('ConsoleMessage'+'LinkNotPresent');}");

    // Checking if link is not present in web view
    if (isConsoleMessage(browser, "LinkNotPresent", solo)) {
      throw new junit.framework.AssertionFailedError();
    }
    solo.sleep(10000);
  }

  // Checking for text present in web view
  public static boolean isTextPresentInWebView(WebView browser, String text,
      Solo solo) {
    ChromeClient client = ChromeClient.getChromeClient();
    browser.setWebChromeClient(client);
    int myWait = 0;
    browser.getSettings().setJavaScriptEnabled(true);
    client.setFinalText(text);
    // Passing entire html doc to console log
    browser.loadUrl("javascript:console.log('ConsoleMessage'+document.body.innerText);");
    browser.setWebChromeClient(client);
    // waiting for console message for 30 seconds
    while (client.isTextPresent == false) {
      solo.sleep(500);
      myWait++;
      if (myWait > 60)
        return false;
    }
    return client.isTextPresent();
  }

  // returning instance of web view if not present retruning null
  public static WebView getInstanceOfWebView(Solo solo) {
    ArrayList<View> listViews = solo.getViews();
    Iterator<View> iterator = listViews.iterator();
    while (iterator.hasNext()) {
      View view = iterator.next();
      if (view instanceof WebView) {
        return (WebView) view;
      }
    }
    return null;
  }

  // checking for console messages
  static boolean isConsoleMessage(WebView browser, String text, Solo solo) {
    ChromeClient client = ChromeClient.getChromeClient();
    browser.setWebChromeClient(client);
    int myWait = 0;
    browser.getSettings().setJavaScriptEnabled(true);
    client.setFinalText(text);
    browser.setWebChromeClient(client);
    while (client.isTextPresent == false) {
      solo.sleep(500);
      myWait++;
      if (myWait > 60)
        return false;
    }

    return client.isTextPresent();
  }

  // Webchrome client
  static class ChromeClient extends WebChromeClient {

    private String finalText;
    boolean isTextPresent = false;
    static ChromeClient client;
    // Creating singleton object
    private ChromeClient() {
      // TODO Auto-generated constructor stub
    }

    static ChromeClient getChromeClient() {
      if (client == null)
        client = new ChromeClient();
      return client;
    }

    void setFinalText(String text) {
      finalText = text;
      isTextPresent = false;
    }

    @Override
    public boolean onConsoleMessage(ConsoleMessage cmsg) {
      // checking prefix

      if (cmsg.message().startsWith("ConsoleMessage")) {
        String msg = cmsg.message().substring(14); // strip off prefix
        isTextPresent = msg.contains(finalText);

        return true;
      }
      return false;
    }

    boolean isTextPresent() {
      return isTextPresent;
    }

  }

}




Java Source Code List

com.imaginea.botbot.server.converter.RecordEntriesConverter.java
com.imaginea.botbot.server.converter.RecordEntryConverter.java
com.imaginea.botbot.server.converter.RecordSessionConverter.java
com.imaginea.botbot.server.converter.RecordSessionsConverter.java
com.imaginea.botbot.server.converter.UriResolver.java
com.imaginea.botbot.server.jpa.RecordEntry.java
com.imaginea.botbot.server.jpa.RecordSession.java
com.imaginea.botbot.server.service.PersistenceService.java
com.imaginea.botbot.server.service.RecordEntriesResource.java
com.imaginea.botbot.server.service.RecordEntryResource.java
com.imaginea.botbot.server.service.RecordSessionResource.java
com.imaginea.botbot.server.service.RecordSessionsResource.java
com.imaginea.botbot.server.servlet.DownloadCsv.java
com.zutubi.android.junitreport.JUnitReportListener.java
com.zutubi.android.junitreport.JUnitReportTestRunner.java
com.zutubi.android.junitreport.TestKeeper.java
com.zutubi.android.junitreport.TestngReportListener.java
org.imaginea.botbot.CommandTransmitter.java
org.imaginea.botbot.Command.java
org.imaginea.botbot.Convertor.java
org.imaginea.botbot.CustomVisitor.java
org.imaginea.botbot.ListenerAdder.java
org.imaginea.botbot.OnClickListenerTest.java
org.imaginea.botbot.OnItemClickListenerTest.java
org.imaginea.botbot.OnItemSelectedListenerTest.java
org.imaginea.botbot.OnTouchListenerTest.java
org.imaginea.botbot.Recorder.java
org.imaginea.botbot.ServerProperties.java
org.imaginea.botbot.TextListner.java
org.imaginea.botbot.ViewClasses.java
org.imaginea.botbot.api.DefaultProperties.java
org.imaginea.botbot.api.IdentifyByType.java
org.imaginea.botbot.api.TestCSVReader.java
org.imaginea.botbot.api.UsefulFunctions.java
org.imaginea.botbot.common.AndroFrameworkExecutorDataDriven.java
org.imaginea.botbot.common.AndroFrameworkExecutor.java
org.imaginea.botbot.common.BaseClass.java
org.imaginea.botbot.common.BotBotTestRunner.java
org.imaginea.botbot.common.CommandExecutor.java
org.imaginea.botbot.common.Command.java
org.imaginea.botbot.common.DataDrivenTestCase.java
org.imaginea.botbot.common.Prefrences.java
org.imaginea.botbot.common.RobotiumBaseClass.java
org.imaginea.botbot.common.TestCaseGenerator.java
org.imaginea.botbot.filereader.BaseReader.java
org.imaginea.botbot.filereader.FileTypeReader.java
org.imaginea.botbot.filereader.PropertiesReader.java
org.imaginea.botbot.keywords.BaseKeywordDefinitions.java
org.imaginea.botbot.keywords.DynamicExecution.java
org.imaginea.botbot.keywords.IKeywords.java
org.imaginea.botbot.keywords.KeywordCaller.java
org.imaginea.botbot.keywords.NativeDriverKeywordDefinitions.java
org.imaginea.botbot.keywords.RobotiumKeywordDefinition.java
org.imaginea.botbot.utility.DataDrivenDataGenerator.java
org.imaginea.botbot.utility.WebViewHandler.java