org.uiautomation.ios.wkrdp.RemoteIOSWebDriver Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2012-2013 eBay Software Foundation and ios-driver 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.uiautomation.ios.wkrdp;
import com.google.common.collect.ImmutableList;
import org.json.JSONArray;
import org.json.JSONException;
import org.openqa.selenium.*;
import org.uiautomation.ios.context.BaseWebInspector;
import org.uiautomation.ios.context.WebInspector;
import org.uiautomation.ios.server.DOMContext;
import org.uiautomation.ios.server.RealDevice;
import org.uiautomation.ios.server.ServerSideSession;
import org.uiautomation.ios.server.configuration.Configuration;
import org.uiautomation.ios.wkrdp.command.Network;
import org.uiautomation.ios.wkrdp.command.Page;
import org.uiautomation.ios.wkrdp.internal.RealDeviceProtocolImpl;
import org.uiautomation.ios.wkrdp.internal.SimulatorProtocolImpl;
import org.uiautomation.ios.wkrdp.internal.WebKitRemoteDebugProtocol;
import org.uiautomation.ios.wkrdp.internal.WebKitSyncronizer;
import org.uiautomation.ios.wkrdp.message.WebkitApplication;
import org.uiautomation.ios.wkrdp.message.WebkitDevice;
import org.uiautomation.ios.wkrdp.message.WebkitPage;
import org.uiautomation.ios.wkrdp.model.NodeId;
import org.uiautomation.ios.wkrdp.model.RemoteWebElement;
import org.uiautomation.ios.wkrdp.model.RemoteWebNativeBackedElement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
public class RemoteIOSWebDriver {
public static void main(String[] args) throws Exception {
RemoteIOSWebDriver driver = new RemoteIOSWebDriver(null);
//driver.connect(uiCatalog);
driver.switchTo(driver.getPages().get(0));
driver.get("http://ebay.co.uk/");
RemoteWebElement body = driver.findElementByCssSelector("body");
driver.get("http://google.co.uk/");
body = driver.findElementByCssSelector("body");
driver.stop();
driver = new RemoteIOSWebDriver(null);
//driver.connect(uiCatalog);
driver.switchTo(driver.getPages().get(0));
driver.get("http://ebay.co.uk/");
body = driver.findElementByCssSelector("body");
driver.get("http://google.co.uk/");
body = driver.findElementByCssSelector("body");
}
static String safari = "com.apple.mobilesafari";
static String uiCatalog = "com.yourcompany.UICatalog";
//private SimulatorSession simulator;
//private Object usbProtocol;
private String bundleId;
private WebKitRemoteDebugProtocol protocol;
private WebkitDevice device;
private List applications = new ArrayList();
private final ServerSideSession session;
private final String connectionKey;
private BaseWebInspector currentInspector;
private Map inspectors = new HashMap();
private static final Logger log = Logger.getLogger(RemoteIOSWebDriver.class.getName());
private List pages = new ArrayList();
private final List created = new ArrayList();
private final WebKitSyncronizer sync;
public RemoteIOSWebDriver(ServerSideSession session, ResponseFinder... finders) {
this.session = session;
connectionKey = UUID.randomUUID().toString();
sync = new WebKitSyncronizer(this);
MessageListener notification = new WebKitNotificationListener(this, sync, session);
if (session.getDevice() instanceof RealDevice) {
if (!Configuration.BETA_FEATURE) {
Configuration.off();
}
String uuid = ((RealDevice) session.getDevice()).getUuid();
protocol = new RealDeviceProtocolImpl(uuid,notification, finders);
} else {
protocol = new SimulatorProtocolImpl(notification, finders);
}
protocol.register();
sync.waitForSimToRegister();
sync.waitForSimToSendApps();
if (applications.size() == 1) {
connect(applications.get(0).getBundleId());
} else {
log.warning("session created but application size=" + applications.size());
}
}
public void setPages(List pages) {
this.pages = ImmutableList.copyOf(pages);
}
public List getPages() {
return pages;
}
/*public boolean isConnected() {
log.fine("Applications " + applications.size());
return !applications.isEmpty();
}*/
public void start() {
protocol.start();
}
public void stop() {
protocol.stop();
}
public RemoteWebElement createElement(String reference) {
int pageId = Integer.parseInt(reference.split("_")[0]);
int nodeId = Integer.parseInt(reference.split("_")[1]);
if (currentInspector.getPageIdentifier() != pageId) {
throw new StaleElementReferenceException("Node " + nodeId
+ "is stale.It might still exist, but the "
+ "window with focus has changed.");
}
if (session != null) {
return new RemoteWebNativeBackedElement(new NodeId(nodeId), currentInspector, session);
} else {
return new RemoteWebElement(new NodeId(nodeId), currentInspector);
}
}
public void connect(String bundleId) {
List knownApps = getApplications();
for (WebkitApplication app : knownApps) {
if (bundleId.equals(app.getBundleId())) {
this.bundleId = bundleId;
protocol.connect(bundleId);
sync.waitForSimToSendPages();
switchTo(getPages().get(0));
if (getPages().size() > 1) {
log.warning("Application started, but already have " + getPages().size()
+ " webviews. Connecting to the first one.");
}
return;
}
}
throw new WebDriverException(bundleId + " not in the list " + knownApps
+ ".Either it's not started, or it has no webview to connect to.");
}
public synchronized void setApplications(List applications) {
this.applications = applications;
}
// TODO freynaud return a copy.
public synchronized List getApplications() {
return applications;
}
public void switchTo(String pageId) {
for (WebkitPage p : getPages()) {
if ((p.getPageId() + "").equals(pageId)) {
switchTo(p);
return;
}
}
throw new WebDriverException("no such page " + pageId);
}
public void switchTo(WebkitPage page) {
currentInspector = connect(page);
inspectors.put(page.getPageId(), currentInspector);
}
private BaseWebInspector connect(WebkitPage webkitPage) {
for (WebkitPage page : getPages()) {
if (page.equals(webkitPage)) {
WebInspector
inspector =
new WebInspector(null, webkitPage.getPageId(), protocol, bundleId,
connectionKey, session);
protocol.attachToPage(page.getPageId());
inspector.sendCommand(Page.enablePageEvent());
inspector.sendCommand(Network.enable());
boolean ok = created.add(inspector);
if (ok) {
protocol.addListener(inspector);
}
return inspector;
}
}
throw new WebDriverException("Cannot connect to page " + webkitPage + ".Cannot find it.");
}
public void waitForPageToLoad() {
//currentInspector.waitForPageToLoad();
}
/*public RemoteWebElement getDocument() {
return currentInspector.getDocument();
}*/
public void get(String url) {
currentInspector.get(url);
}
public List getCookies() {
return currentInspector.getCookies();
}
public void deleteCookie(String name, String domain) {
currentInspector.deleteCookie(name, domain);
}
public String getCurrentUrl() {
try {
return currentInspector
.getCurrentUrl(); //To change body of implemented methods use File | Settings | File Templates.
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
return null;
}
}
public String getTitle() {
return currentInspector.getTitle();
}
public int getCurrentPageID() {
return currentInspector.getPageIdentifier();
}
public List findElements(By by) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public RemoteWebElement findElement(By by) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public RemoteWebElement findElementByCssSelector(String cssSelector) throws Exception {
return currentInspector.findElementByCSSSelector(cssSelector);
}
public List findElementsByCssSelector(String cssSelector) {
return currentInspector.findElementsByCSSSelector(cssSelector);
}
public String getPageSource() {
return currentInspector.getPageSource();
}
public Dimension getSize() throws Exception {
return currentInspector.getSize();
}
public void close() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void quit() {
//To change body of implemented methods use File | Settings | File Templates.
}
public List getWindowHandles() {
return getPages();
}
public String getWindowHandle() {
return "" + currentInspector.getPageIdentifier();
}
public int getWindowHandleIndex() {
int pageId = currentInspector.getPageIdentifier();
for (WebkitPage p : getPages()) {
if (p.getPageId() == pageId) {
return getPages().indexOf(p);
}
}
throw new WebDriverException("Cannot find current page.");
}
public int getWindowHandleIndexDifference(String pageId) {
// first, sort pages.
List pages = getPages();
int currentIndex = -1;
for (WebkitPage p : pages) {
if (p.getPageId() == currentInspector.getPageIdentifier()) {
currentIndex = pages.indexOf(p);
}
}
int destination = -1;
for (WebkitPage p : pages) {
if ((p.getPageId() + "").equals(pageId)) {
if (p.isITunesAd()) {
log.warning(
"Trying to switch to an ad. You will need to be on the correct page already to do "
+ "that.If you're not, random error will occur.");
return 0;
} else {
destination = pages.indexOf(p);
}
}
}
return destination - currentIndex;
}
public WebDriver.TargetLocator switchTo() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public void back() throws JSONException {
currentInspector.back();
}
public void forward() {
try {
currentInspector.forward();
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
public void refresh() {
try {
currentInspector.refresh();
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
public WebDriver.Options manage() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Object executeScript(String script, JSONArray args) {
return currentInspector.executeScript(script, args);
}
public Object executeAsyncScript(String script, Object... args) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
// TODO remove.
public RemoteWebElement getDocument() {
return currentInspector.getDocument();
}
public boolean isLoading() {
return !currentInspector.isReady();
}
public DOMContext getContext() {
return currentInspector.getContext();
}
public String getLoadedFlag() {
return currentInspector.getLoadedFlag();
}
public synchronized void setDevice(WebkitDevice device) {
this.device = device;
}
public synchronized WebkitDevice getDevice() {
return device;
}
}