-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from DrMarcII/master
Version 0.10.0-pre.1
- Loading branch information
Showing
36 changed files
with
490 additions
and
382 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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',}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
} |
Oops, something went wrong.