Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
Copyright 2011 Selenium committers
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.openqa.selenium.android.library;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Looper;
import android.provider.Settings;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Closeables;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Beta;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.HasTouchScreen;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.NoSuchFrameException;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Rotatable;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.TouchScreen;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.html5.AppCacheStatus;
import org.openqa.selenium.html5.ApplicationCache;
import org.openqa.selenium.html5.BrowserConnection;
import org.openqa.selenium.html5.LocalStorage;
import org.openqa.selenium.html5.Location;
import org.openqa.selenium.html5.LocationContext;
import org.openqa.selenium.html5.SessionStorage;
import org.openqa.selenium.html5.WebStorage;
import org.openqa.selenium.internal.Base64Encoder;
import org.openqa.selenium.internal.FindsByClassName;
import org.openqa.selenium.internal.FindsByCssSelector;
import org.openqa.selenium.internal.FindsById;
import org.openqa.selenium.internal.FindsByLinkText;
import org.openqa.selenium.internal.FindsByName;
import org.openqa.selenium.internal.FindsByTagName;
import org.openqa.selenium.internal.FindsByXPath;
import org.openqa.selenium.internal.WrapsElement;
import org.openqa.selenium.logging.Logs;
import org.openqa.selenium.remote.ErrorCodes;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
public class AndroidWebDriver implements WebDriver, SearchContext, JavascriptExecutor,
TakesScreenshot, Rotatable, BrowserConnection, HasTouchScreen,
WebStorage, LocationContext, LocationListener, ApplicationCache {
private static final String ELEMENT_KEY = "ELEMENT";
private static final String WINDOW_KEY = "WINDOW";
private static final String STATUS = "status";
private static final String VALUE = "value";
private AndroidWebElement element;
private DomWindow currentWindowOrFrame;
private long implicitWait = 0;
// Maps the element ID to the AndroidWebElement
private Map store;
private AndroidTouchScreen touchScreen;
private AndroidNavigation navigation;
private AndroidOptions options;
private AndroidLocalStorage localStorage;
private AndroidSessionStorage sessionStorage;
private AndroidTargetLocator targetLocator;
private AndroidFindBy findBy;
private AndroidLogs logs;
// Use for control redirect, contains the last url loaded (updated after each redirect)
private volatile String lastUrlLoaded;
private SessionCookieManager sessionCookieManager;
private ViewAdapter view;
private WebDriverViewManager viewManager;
private final Object syncObject = new Object();
private volatile boolean pageDoneLoading;
private NetworkStateHandler networkHandler;
private Activity activity;
private volatile boolean editAreaHasFocus;
private volatile String result;
private volatile boolean resultReady;
// Timeouts in milliseconds
private static final long LOADING_TIMEOUT = 30000L;
private static final long START_LOADING_TIMEOUT = 700L;
static final long RESPONSE_TIMEOUT = 10000L;
private static final long FOCUS_TIMEOUT = 1000L;
private static final long POLLING_INTERVAL = 50L;
static final long UI_TIMEOUT = 3000L;
private boolean acceptSslCerts;
private volatile boolean pageStartedLoading;
private boolean done = false;
private Supplier locManagerSupplier;
private String locationProvider;
private JavascriptResultNotifier notifier = new JavascriptResultNotifier() {
public void notifyResultReady(String updated) {
synchronized (syncObject) {
result = updated;
resultReady = true;
syncObject.notify();
}
}
};
private AndroidWebElement getOrCreateWebElement(String id) {
if (store.get(id) != null) {
return store.get(id);
} else {
AndroidWebElement toReturn = new AndroidWebElement(this, id);
store.put(id, toReturn);
return toReturn;
}
}
public void setAcceptSslCerts(boolean accept) {
acceptSslCerts = accept;
}
public boolean getAcceptSslCerts() {
return acceptSslCerts;
}
private void initDriverState() {
store = Maps.newHashMap();
findBy = new AndroidFindBy();
currentWindowOrFrame = new DomWindow("");
store = Maps.newHashMap();
touchScreen = new AndroidTouchScreen(this);
navigation = new AndroidNavigation();
options = new AndroidOptions();
element = getOrCreateWebElement("");
localStorage = new AndroidLocalStorage(this);
sessionStorage = new AndroidSessionStorage(this);
targetLocator = new AndroidTargetLocator();
viewManager = new WebDriverViewManager();
logs = new AndroidLogs();
Looper.prepare();
try {
locationProvider = LocationManager.GPS_PROVIDER;
final LocationManager locManager =
(LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
locManagerSupplier = new Supplier() {
public LocationManager get() {
return locManager;
}
};
locManager.addTestProvider(locationProvider,
true, true, true, true, true, true, true, 0, 5);
locManager.setTestProviderEnabled(locationProvider, true);
locManager.requestLocationUpdates(locationProvider, 0, 0, this);
} catch (SecurityException e) {
// Devices require manually setting up to allow location, 99% of tests don't need location,
// ignore the relevant exception here
locManagerSupplier = new Supplier() {
public LocationManager get() {
throw new IllegalStateException(
"The permission to ALLOW_MOCK_LOCATION needs to be set on your android device, " +
"but currently is not. Cannot perform location actions without this permission.");
}
};
}
}
private void initCookiesState() {
// Needs to be called before CookieMAnager::getInstance()
CookieSyncManager.createInstance(activity);
sessionCookieManager = new SessionCookieManager();
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
}
/**
* Use this contructor to use WebDriver with a WebView that has the same settings as
* the Android browser.
*
* @param activity the activity context where the WebView will be created.
*/
public AndroidWebDriver(Activity activity) {
this.activity = activity;
initDriverState();
ChromeClientWrapper chromeWrapper = new ChromeClientWrapper("android.webkit.WebChromeClient",
new DefaultChromeClient());
ViewClientWrapper viewClientWrapper = new ViewClientWrapper("android.webkit.WebViewClient",
new DefaultViewClient());
WebDriverView wdview = new WebDriverView(this, new DefaultWebViewFactory(),
viewClientWrapper, chromeWrapper, null);
// Create a new view and delete existing windows.
newWebView( /*Delete existing windows*/true, wdview);
initCookiesState();
networkHandler = new NetworkStateHandler(activity, view);
}
/**
* Use this constructor to use WebDriver with a custom view.
*
* @param activity the activity context where the view will be displayed.
* @param viewFactory a implementation of the ViewFactory interface. WebDriver will
* use this creation mechanism to create views when needed (e.g. when clicking on a link
* that opens a new window).
* @param viewClient the ViewClientWrapper used by the custom WebView.
* @param chromeClient the ChromeClientWrapper used by the custom WebView.
*/
public AndroidWebDriver(Activity activity, ViewFactory viewFactory,
ViewClientWrapper viewClient, ChromeClientWrapper chromeClient) {
this.activity = activity;
initDriverState();
WebDriverView wdview = new WebDriverView(this, viewFactory, viewClient, chromeClient,
null);
newWebView(/*Delete existing windows*/true, wdview);
initCookiesState();
networkHandler = new NetworkStateHandler(activity, view);
}
/**
* Use this constructor to use WebDriver with a custom view and a custom
* View.OnFocusChangeListener for that view..
*
* @param activity the activity context where the view will be displayed.
* @param viewFactory a implementation of the ViewFactory interface. WebDriver will
* use this creation mechanism to create views when needed (e.g. when clicking on a link
* that opens a new window).
* @param viewClient the ViewClientWrapper used by the custom WebView.
* @param chromeClient the ChromeClientWrapper used by the custom WebView.
* @param focusListener the listener used by the view that will be created by the viewFactory.
*/
public AndroidWebDriver(Activity activity, ViewFactory viewFactory,
ViewClientWrapper viewClient, ChromeClientWrapper chromeClient,
View.OnFocusChangeListener focusListener) {
this.activity = activity;
initDriverState();
WebDriverView wdview = new WebDriverView(this, viewFactory, viewClient, chromeClient,
focusListener);
newWebView(/*Delete existing windows*/true, wdview);
initCookiesState();
networkHandler = new NetworkStateHandler(activity, view);
}
String getLastUrlLoaded() {
return lastUrlLoaded;
}
void setLastUrlLoaded(String url) {
this.lastUrlLoaded = url;
}
void setEditAreaHasFocus(boolean focused) {
editAreaHasFocus = focused;
}
boolean getEditAreaHasFocus() {
return editAreaHasFocus;
}
void resetPageIsLoading() {
pageStartedLoading = false;
pageDoneLoading = false;
}
void notifyPageStartedLoading() {
synchronized (syncObject) {
pageStartedLoading = true;
pageDoneLoading = false;
syncObject.notify();
}
}
void notifyPageDoneLoading() {
synchronized (syncObject) {
pageDoneLoading = true;
syncObject.notify();
}
}
void waitForPageToLoad() {
synchronized (syncObject) {
long timeout = System.currentTimeMillis() + START_LOADING_TIMEOUT;
while (!pageStartedLoading && (System.currentTimeMillis() < timeout)) {
try {
syncObject.wait(POLLING_INTERVAL);
} catch (InterruptedException e) {
throw new RuntimeException();
}
}
long end = System.currentTimeMillis() + LOADING_TIMEOUT;
while (!pageDoneLoading && pageStartedLoading && (System.currentTimeMillis() < end)) {
try {
syncObject.wait(LOADING_TIMEOUT);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
void waitUntilEditAreaHasFocus() {
long timeout = System.currentTimeMillis() + FOCUS_TIMEOUT;
while (!editAreaHasFocus && (System.currentTimeMillis() < timeout)) {
try {
Thread.sleep(POLLING_INTERVAL);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public WebView getWebView() {
if (view.getUnderlyingView() instanceof WebView) {
return (WebView) view.getUnderlyingView();
}
throw new WebDriverException("This WebDriver instance is not using a WebView!");
}
public Object getView() {
return view.getUnderlyingView();
}
void newWebView(boolean newDriver, final WebDriverView wdview) {
// If we are requesting a new driver, then close all
// existing window before opening a new one.
if (newDriver) {
quit();
}
long start = System.currentTimeMillis();
long end = start + UI_TIMEOUT;
done = false;
activity.runOnUiThread(new Runnable() {
public void run() {
synchronized (syncObject) {
final ViewAdapter newView = wdview.create();
view = newView;
viewManager.addView(view);
activity.setContentView((View) view.getUnderlyingView());
done = true;
syncObject.notify();
}
}
});
waitForDone(end, UI_TIMEOUT, "Failed to create WebView.");
}
private void waitForDone(long end, long timeout, String error) {
synchronized (syncObject) {
while (!done && System.currentTimeMillis() < end) {
try {
syncObject.wait(timeout);
} catch (InterruptedException e) {
throw new WebDriverException(error, e);
}
}
}
}
WebDriverViewManager getViewManager() {
return viewManager;
}
public Activity getActivity() {
return activity;
}
public String getCurrentUrl() {
if (view == null) {
throw new WebDriverException("No open windows.");
}
done = false;
long end = System.currentTimeMillis() + UI_TIMEOUT;
final String[] url = new String[1];
activity.runOnUiThread(new Runnable() {
public void run() {
synchronized (syncObject) {
url[0] = view.getUrl();
done = true;
syncObject.notify();
}
}
});
waitForDone(end, UI_TIMEOUT, "Failed to get current url.");
return url[0];
}
public String getTitle() {
if (view == null) {
throw new WebDriverException("No open windows.");
}
long end = System.currentTimeMillis() + UI_TIMEOUT;
final String[] title = new String[1];
done = false;
activity.runOnUiThread(new Runnable() {
public void run() {
synchronized (syncObject) {
title[0] = view.getTitle();
done = true;
syncObject.notify();
}
}
});
waitForDone(end, UI_TIMEOUT, "Failed to get title");
return title[0];
}
public void get(String url) {
navigation.to(url);
}
public String getPageSource() {
return (String) executeScript(
"return (new XMLSerializer()).serializeToString(document.documentElement);");
}
public void close() {
if (view == null) {
throw new WebDriverException("No open windows.");
}
// Dispose of existing alerts (if any) for this view.
AlertManager.removeAlertForView(view);
done = false;
long end = System.currentTimeMillis() + RESPONSE_TIMEOUT;
activity.runOnUiThread(new Runnable() {
public void run() {
synchronized (syncObject) {
view.destroy();
viewManager.removeView(view);
done = true;
syncObject.notify();
}
}
});
waitForDone(end, RESPONSE_TIMEOUT, "Failed to close window.");
view = null;
}
public void quit() {
AlertManager.removeAllAlerts();
activity.runOnUiThread(new Runnable() {
public void run() {
viewManager.closeAll();
view = null;
}
});
}
public WebElement findElement(By by) {
long start = System.currentTimeMillis();
while (true) {
try {
return by.findElement(findBy);
} catch (NoSuchElementException e) {
if (System.currentTimeMillis() - start > implicitWait) {
throw e;
}
sleepQuietly(100);
}
}
}
public List findElements(By by) {
long start = System.currentTimeMillis();
List found = by.findElements(findBy);
while (found.isEmpty() && (System.currentTimeMillis() - start <= implicitWait)) {
sleepQuietly(100);
found = by.findElements(findBy);
}
return found;
}
public AppCacheStatus getStatus() {
Long scriptRes = (Long) executeRawScript("(" + AndroidAtoms.GET_APPCACHE_STATUS.getValue() + ")()");
return AppCacheStatus.getEnum(scriptRes.intValue());
}
private class AndroidFindBy implements SearchContext, FindsByTagName, FindsById,
FindsByLinkText, FindsByName, FindsByXPath, FindsByCssSelector, FindsByClassName {
public WebElement findElement(By by) {
long start = System.currentTimeMillis();
while (true) {
try {
return by.findElement(findBy);
} catch (NoSuchElementException e) {
if (System.currentTimeMillis() - start > implicitWait) {
throw e;
}
sleepQuietly(100);
}
}
}
public List findElements(By by) {
long start = System.currentTimeMillis();
List found = by.findElements(findBy);
while (found.isEmpty() && (System.currentTimeMillis() - start <= implicitWait)) {
sleepQuietly(100);
found = by.findElements(this);
}
return found;
}
public WebElement findElementByLinkText(String using) {
return element.getFinder().findElementByLinkText(using);
}
public List findElementsByLinkText(String using) {
return element.getFinder().findElementsByLinkText(using);
}
public WebElement findElementById(String id) {
return element.getFinder().findElementById(id);
}
public List findElementsById(String id) {
return findElementsByXPath("//*[@id='" + id + "']");
}
public WebElement findElementByName(String using) {
return element.getFinder().findElementByName(using);
}
public List findElementsByName(String using) {
return element.getFinder().findElementsByName(using);
}
public WebElement findElementByTagName(String using) {
return element.getFinder().findElementByTagName(using);
}
public List findElementsByTagName(String using) {
return element.getFinder().findElementsByTagName(using);
}
public WebElement findElementByXPath(String using) {
return element.getFinder().findElementByXPath(using);
}
public List findElementsByXPath(String using) {
return element.getFinder().findElementsByXPath(using);
}
public WebElement findElementByPartialLinkText(String using) {
return element.getFinder().findElementByPartialLinkText(using);
}
public List findElementsByPartialLinkText(String using) {
return element.getFinder().findElementsByPartialLinkText(using);
}
public WebElement findElementByCssSelector(String using) {
return element.getFinder().findElementByCssSelector(using);
}
public List findElementsByCssSelector(String using) {
return element.getFinder().findElementsByCssSelector(using);
}
public WebElement findElementByClassName(String using) {
return element.getFinder().findElementByClassName(using);
}
public List findElementsByClassName(String using) {
return element.getFinder().findElementsByClassName(using);
}
}
private static void sleepQuietly(long ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException cause) {
Thread.currentThread().interrupt();
throw new WebDriverException(cause);
}
}
public Set getWindowHandles() {
return viewManager.getAllHandles();
}
public String getWindowHandle() {
String r = viewManager.getWindowHandle(view);
if (r == null) {
throw new WebDriverException("FATAL ERROR HANDLE IS NULL");
}
return r;
}
public TargetLocator switchTo() {
return targetLocator;
}
public LocalStorage getLocalStorage() {
return localStorage;
}
public SessionStorage getSessionStorage() {
return sessionStorage;
}
private class AndroidTargetLocator implements TargetLocator {
public WebElement activeElement() {
return (WebElement) executeRawScript("(" + AndroidAtoms.ACTIVE_ELEMENT.getValue() + ")()");
}
public WebDriver defaultContent() {
executeRawScript("(" + AndroidAtoms.DEFAULT_CONTENT.getValue() + ")()");
return AndroidWebDriver.this;
}
public WebDriver frame(int index) {
DomWindow window = (DomWindow) executeRawScript(
"(" + AndroidAtoms.FRAME_BY_INDEX.getValue() + ")(" + index + ")");
if (window == null) {
throw new NoSuchFrameException("Frame with index '" + index + "' does not exists.");
}
currentWindowOrFrame = window;
return AndroidWebDriver.this;
}
public WebDriver frame(String frameNameOrId) {
DomWindow window = (DomWindow) executeRawScript(
"(" + AndroidAtoms.FRAME_BY_ID_OR_NAME.getValue() + ")('" + frameNameOrId + "')");
if (window == null) {
throw new NoSuchFrameException("Frame with ID or name '" + frameNameOrId
+ "' does not exists.");
}
currentWindowOrFrame = window;
return AndroidWebDriver.this;
}
public WebDriver frame(WebElement frameElement) {
DomWindow window = (DomWindow) executeScript("return arguments[0].contentWindow;",
((AndroidWebElement) ((WrapsElement) frameElement).getWrappedElement()));
if (window == null) {
throw new NoSuchFrameException("Frame does not exists.");
}
currentWindowOrFrame = window;
return AndroidWebDriver.this;
}
public WebDriver window(final String nameOrHandle) {
final boolean[] shouldhTrow = new boolean[1];
shouldhTrow[0] = false;
done = false;
long end = System.currentTimeMillis() + RESPONSE_TIMEOUT;
activity.runOnUiThread(new Runnable() {
public void run() {
synchronized (syncObject) {
ViewAdapter v = viewManager.getView(nameOrHandle);
if (v != null) {
view = v;
} else {
// Can't throw an exception in the UI thread
// Or the App crashes
shouldhTrow[0] = true;
}
activity.setContentView((View) view.getUnderlyingView());
done = true;
syncObject.notify();
}
}
});
waitForDone(end, RESPONSE_TIMEOUT, "Failed to switch to window: " + nameOrHandle);
if (shouldhTrow[0]) {
throw new NoSuchWindowException(
"Window '" + nameOrHandle + "' does not exist.");
}
return AndroidWebDriver.this;
}
public Alert alert() {
if (view == null) {
// An alert may have popped up when the window was closed.
// If there is an alert, just return it.
throw new WebDriverException("Asked for an alert without a window context. " +
"switchTo().window(...) first.");
}
Alert foundAlert = AlertManager.getAlertForView(view);
if (foundAlert == null) {
throw new NoAlertPresentException("No alert in current view.");
}
return foundAlert;
}
}
public Navigation navigate() {
return navigation;
}
public boolean isJavascriptEnabled() {
return true;
}
public Object executeScript(String script, Object... args) {
return injectJavascript(script, false, args);
}
public Object executeAsyncScript(String script, Object... args) {
throw new UnsupportedOperationException("This is feature will be implemented soon!");
}
/**
* Converts the arguments passed to a JavaScript friendly format.
*
* @param args The arguments to convert.
* @return Comma separated Strings containing the arguments.
*/
private String convertToJsArgs(final Object... args) {
StringBuilder toReturn = new StringBuilder();
int length = args.length;
for (int i = 0; i < length; i++) {
toReturn.append((i > 0) ? "," : "");
if (args[i] instanceof List>) {
toReturn.append("[");
List