Skip to content

Commit

Permalink
Merge pull request #48 from DrMarcII/master
Browse files Browse the repository at this point in the history
Version 0.10.0-pre.1
  • Loading branch information
DrMarcII committed Mar 25, 2015
2 parents ff2ea50 + 37ec737 commit e229b32
Show file tree
Hide file tree
Showing 36 changed files with 490 additions and 382 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ Installing
Now in your Dart code, you can use:

```Dart
import 'package:webdriver/webdriver.dart';
import 'package:webdriver/io.dart';
WebDriver driver = buildDriver(...);
```

Testing
Expand Down
1 change: 1 addition & 0 deletions lib/async_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
library webdriver.async_helpers;

import 'dart:async' show Completer, Future;

import 'package:matcher/matcher.dart' show expect, isNotNull;

const defaultInterval = const Duration(milliseconds: 500);
Expand Down
20 changes: 5 additions & 15 deletions lib/webdriver.dart → lib/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

library webdriver;
library webdriver.core;

import 'dart:async' show Future, Stream, StreamController;
import 'dart:collection' show UnmodifiableMapView;
import 'dart:convert' show JSON, UTF8;
import 'dart:io'
show
ContentType,
HttpClient,
HttpClientRequest,
HttpClientResponse,
HttpHeaders;
import 'dart:async' show Future, Stream;
import 'dart:math' show Point, Rectangle;

import 'package:crypto/crypto.dart' show CryptoUtils;

import 'async_helpers.dart' show Lock, waitFor;
export 'async_helpers.dart' show waitFor;
import 'src/command_processor.dart' show CommandProcessor;

export 'src/exception.dart';

part 'src/alert.dart';
part 'src/capabilities.dart';
part 'src/command_processor.dart';
part 'src/common.dart';
part 'src/exception.dart';
part 'src/keyboard.dart';
part 'src/logs.dart';
part 'src/mouse.dart';
Expand Down
88 changes: 88 additions & 0 deletions lib/html.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
library webdriver.html;

import 'dart:async' show Future;
import 'dart:collection' show UnmodifiableMapView;
import 'dart:convert' show JSON, UTF8;
import 'dart:html' show HttpRequest, ProgressEvent;

import 'package:webdriver/async_helpers.dart' show Lock;
import 'package:webdriver/core.dart' show WebDriver, Capabilities;
import 'package:webdriver/src/command_processor.dart' show CommandProcessor;
import 'package:webdriver/src/exception.dart' show WebDriverException;

export 'package:webdriver/core.dart';

final Uri defaultUri = Uri.parse('http://127.0.0.1:4444/wd/hub/');

/// Creates a WebDriver instance connected to the specified WebDriver server.
Future<WebDriver> createDriver({Uri uri, Map<String, dynamic> desired}) async {
if (uri == null) {
uri = defaultUri;
}

var commandProcessor = new _HtmlCommandProcessor();

if (desired == null) {
desired = Capabilities.empty;
}

var response = await commandProcessor.post(
uri.resolve('session'), {'desiredCapabilities': desired}, value: false);
return new WebDriver(commandProcessor, uri, response['sessionId'],
new UnmodifiableMapView(response['value']));
}

class _HtmlCommandProcessor implements CommandProcessor {
Lock _lock = new Lock();

Future<Object> post(Uri uri, dynamic params, {bool value: true}) =>
_request('POST', uri, params, value);

Future<Object> get(Uri uri, {bool value: true}) =>
_request('GET', uri, null, value);

Future<Object> delete(Uri uri, {bool value: true}) =>
_request('DELETE', uri, null, value);

Future<Object> _request(
String method, Uri uri, dynamic params, bool value) async {
await _lock.acquire();
var sendData = null;
if (params != null && method == 'POST') {
sendData = JSON.encode(params);
}

HttpRequest request;
try {
request = await HttpRequest.request(uri.toString(),
method: method,
requestHeaders: _headers,
responseType: 'json',
sendData: sendData,
mimeType: 'application/json');
} on ProgressEvent catch (e) {
request = e.target;
} finally {
_lock.release();
}
var respBody = request.response;
try {
respBody = JSON.decode(respBody);
} catch (e) {}

if (request.status < 200 ||
request.status > 299 ||
(respBody is Map && respBody['status'] != 0)) {
throw new WebDriverException(
httpStatusCode: request.status,
httpReasonPhrase: request.statusText,
jsonResp: respBody);
}
if (value && respBody is Map) {
return respBody['value'];
}
return respBody;
}

Map<String, String> get _headers => {'Accept': 'application/json',};
}
105 changes: 105 additions & 0 deletions lib/io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
library webdriver.io;

import 'dart:async' show Future;
import 'dart:collection' show UnmodifiableMapView;
import 'dart:convert' show JSON, UTF8;
import 'dart:io'
show
ContentType,
HttpClient,
HttpClientRequest,
HttpClientResponse,
HttpHeaders;

import 'package:webdriver/async_helpers.dart' show Lock;
import 'package:webdriver/core.dart' show WebDriver, Capabilities;
import 'package:webdriver/src/command_processor.dart' show CommandProcessor;
import 'package:webdriver/src/exception.dart' show WebDriverException;

export 'package:webdriver/core.dart';

final Uri defaultUri = Uri.parse('http://127.0.0.1:4444/wd/hub/');

/// Creates a WebDriver instance connected to the specified WebDriver server.
Future<WebDriver> createDriver({Uri uri, Map<String, dynamic> desired}) async {
if (uri == null) {
uri = defaultUri;
}

var commandProcessor = new _IOCommandProcessor();

if (desired == null) {
desired = Capabilities.empty;
}

var response = await commandProcessor.post(
uri.resolve('session'), {'desiredCapabilities': desired}, value: false);
return new WebDriver(commandProcessor, uri, response['sessionId'],
new UnmodifiableMapView(response['value']));
}

final ContentType _contentTypeJson =
new ContentType("application", "json", charset: "utf-8");

class _IOCommandProcessor implements CommandProcessor {
final HttpClient client = new HttpClient();

Lock _lock = new Lock();

Future<Object> post(Uri uri, dynamic params, {bool value: true}) async {
await _lock.acquire();
HttpClientRequest request = await client.postUrl(uri);
_setUpRequest(request);
request.headers.contentType = _contentTypeJson;
if (params != null) {
var body = UTF8.encode(JSON.encode(params));
request.contentLength = body.length;
request.add(body);
} else {
request.contentLength = 0;
}
return await _processResponse(await request.close(), value);
}

Future<Object> get(Uri uri, {bool value: true}) async {
await _lock.acquire();
HttpClientRequest request = await client.getUrl(uri);
_setUpRequest(request);
return await _processResponse(await request.close(), value);
}

Future<Object> delete(Uri uri, {bool value: true}) async {
await _lock.acquire();
HttpClientRequest request = await client.deleteUrl(uri);
_setUpRequest(request);
return await _processResponse(await request.close(), value);
}

_processResponse(HttpClientResponse response, bool value) async {
var respBody = await UTF8.decodeStream(response);
_lock.release();
try {
respBody = JSON.decode(respBody);
} catch (e) {}

if (response.statusCode < 200 ||
response.statusCode > 299 ||
(respBody is Map && respBody['status'] != 0)) {
throw new WebDriverException(
httpStatusCode: response.statusCode,
httpReasonPhrase: response.reasonPhrase,
jsonResp: respBody);
}
if (value && respBody is Map) {
return respBody['value'];
}
return respBody;
}

void _setUpRequest(HttpClientRequest request) {
request.followRedirects = false;
request.headers.add(HttpHeaders.ACCEPT, "application/json");
request.headers.add(HttpHeaders.ACCEPT_CHARSET, UTF8.name);
request.headers.add(HttpHeaders.CACHE_CONTROL, "no-cache");
}
}
2 changes: 1 addition & 1 deletion lib/src/alert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

part of webdriver;
part of webdriver.core;

/// A JavaScript alert(), confirm(), or prompt() dialog
class Alert extends _WebDriverBase {
Expand Down
15 changes: 1 addition & 14 deletions lib/src/capabilities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

part of webdriver;
part of webdriver.core;

class Capabilities {
static const String browserName = "browserName";
Expand Down Expand Up @@ -55,26 +55,13 @@ class Capabilities {

class Browser {
static const String firefox = "firefox";
static const String firefox2 = "firefox2";
static const String firefox3 = "firefox3";
static const String firefoxProxy = "firefoxproxy";
static const String firefoxChrome = "firefoxchrome";
static const String googleChrome = "googlechrome";
static const String safari = "safari";
static const String opera = "opera";
static const String iexplore = "iexplore";
static const String iexploreProxy = "iexploreproxy";
static const String safariProxy = "safariproxy";
static const String chrome = "chrome";
static const String konqueror = "konqueror";
static const String mock = "mock";
static const String ieHta = "iehta";
static const String android = "android";
static const String htmlUnit = "htmlunit";
static const String ie = "internet explorer";
static const String iphone = "iPhone";
static const String ipad = "iPad";
static const String phantomJS = "phantomjs";
}

class BrowserPlatform {
Expand Down
83 changes: 7 additions & 76 deletions lib/src/command_processor.dart
Original file line number Diff line number Diff line change
@@ -1,81 +1,12 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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.
library webdriver.command_processor;

part of webdriver;
import 'dart:async';

final ContentType _contentTypeJson =
new ContentType("application", "json", charset: "utf-8");
/// Interface for HTTP access.
abstract class CommandProcessor {
Future<Object> post(Uri uri, dynamic params, {bool value: true});

class _CommandProcessor {
final HttpClient client = new HttpClient();
Future<Object> get(Uri uri, {bool value: true});

Lock _lock = new Lock();

Future<Object> post(Uri uri, dynamic params, {bool value: true}) async {
await _lock.acquire();
HttpClientRequest request = await client.postUrl(uri);
_setUpRequest(request);
request.headers.contentType = _contentTypeJson;
if (params != null) {
var body = UTF8.encode(JSON.encode(params));
request.contentLength = body.length;
request.add(body);
} else {
request.contentLength = 0;
}
return await _processResponse(await request.close(), value);
}

Future<Object> get(Uri uri, {bool value: true}) async {
await _lock.acquire();
HttpClientRequest request = await client.getUrl(uri);
_setUpRequest(request);
return await _processResponse(await request.close(), value);
}

Future<Object> delete(Uri uri, {bool value: true}) async {
await _lock.acquire();
HttpClientRequest request = await client.deleteUrl(uri);
_setUpRequest(request);
return await _processResponse(await request.close(), value);
}

_processResponse(HttpClientResponse response, bool value) async {
var respBody = await UTF8.decodeStream(response);
_lock.release();
try {
respBody = JSON.decode(respBody);
} catch (e) {}

if (response.statusCode < 200 ||
response.statusCode > 299 ||
(respBody is Map && respBody['status'] != 0)) {
throw new WebDriverException(
httpStatusCode: response.statusCode,
httpReasonPhrase: response.reasonPhrase,
jsonResp: respBody);
}
if (value && respBody is Map) {
return respBody['value'];
}
return respBody;
}

void _setUpRequest(HttpClientRequest request) {
request.followRedirects = false;
request.headers.add(HttpHeaders.ACCEPT, "application/json");
request.headers.add(HttpHeaders.ACCEPT_CHARSET, UTF8.name);
request.headers.add(HttpHeaders.CACHE_CONTROL, "no-cache");
}
Future<Object> delete(Uri uri, {bool value: true});
}
Loading

0 comments on commit e229b32

Please sign in to comment.