WebDriverBase class
Base class for all WebDriver request classes. This class wraps up an URL prefix (host, port, and initial path), and provides a client function for doing HTTP requests with JSON payloads.
class WebDriverBase { String _host; int _port; String _path; String _url; String get path => _path; String get url => _url; /** * The default URL for WebDriver remote server is * http://localhost:4444/wd/hub. */ WebDriverBase.fromUrl([this._url = 'http://localhost:4444/wd/hub']) { // Break out the URL components. var re = const RegExp('[^:/]+://([^/]+)(/.*)'); var matches = re.firstMatch(_url); _host = matches[1]; _path = matches[2]; var idx = _host.indexOf(':'); if (idx >= 0) { _port = parseInt(_host.substring(idx+1)); _host = _host.substring(0, idx); } else { _port = 80; } } WebDriverBase([ this._host = 'localhost', this._port = 4444, this._path = '/wd/hub']) { _url = 'http://$_host:$_port$_path'; } /** * Execute a request to the WebDriver server. [http_method] should be * one of 'GET', 'POST', or 'DELETE'. [command] is the text to append * to the base URL path to get the full URL. [params] are the additional * parameters. If a [List] or [Map] they will be posted as JSON parameters. * If a number or string, "/params" is appended to the URL. */ void _serverRequest(String http_method, String command, Completer completer, [List successCodes, Map params, Function customHandler]) { var status = 0; var results = null; var message = null; if (successCodes == null) { successCodes = [ 200, 204 ]; } try { if (params != null && params is List && http_method != 'POST') { throw new Exception( 'The http method called for ${command} is ${http_method} but it has ' 'to be POST if you want to pass the JSON params ' '${JSON.stringify(params)}'); } var path = command; if (params != null && (params is num || params is String)) { path = '$path/$params'; } var client = new HttpClient(); var connection = client.open(http_method, _host, _port, path); connection.onRequest = (r) { r.headers.add(HttpHeaders.ACCEPT, "application/json"); r.headers.add( HttpHeaders.CONTENT_TYPE, 'application/json;charset=UTF-8'); OutputStream s = r.outputStream; if (params != null && params is Map) { s.writeString(JSON.stringify(params)); } s.close(); }; connection.onError = (e) { if (completer != null) { completer.completeException(new WebDriverError(-1, e)); completer = null; } }; connection.followRedirects = false; connection.onResponse = (r) { StringInputStream s = new StringInputStream(r.inputStream); StringBuffer sbuf = new StringBuffer(); s.onData = () { var data = s.read(); if (data != null) { sbuf.add(data); } }; s.onClosed = () { var value = null; results = sbuf.toString().trim(); // For some reason we get a bunch of NULs on the end // of the text and the JSON parser blows up on these, so // strip them. We have to do this the hard way as // replaceAll('\0', '') does not work. // These NULs can be seen in the TCP packet, so it is not // an issue with character encoding; it seems to be a bug // in WebDriver stack. for (var i = results.length; --i >= 0;) { var code = results.charCodeAt(i); if (code != 0) { results = results.substring(0, i+1); break; } } if (successCodes.indexOf(r.statusCode) < 0) { throw 'Unexpected response ${r.statusCode}'; } if (status == 0 && results.length > 0) { // 4xx responses send plain text; others send JSON. if (r.statusCode < 400) { results = JSON.parse(results); status = results['status']; } if (results is Map && (results as Map).containsKey('value')) { value = results['value']; } if (value is Map && value.containsKey('message')) { message = value['message']; } } if (status == 0) { if (customHandler != null) { customHandler(r, value); } else if (completer != null) { completer.complete(value); } } }; }; } catch (e, s) { completer.completeException( new WebDriverError(-1, e), s); completer = null; } } Future _simpleCommand(method, extraPath, [successCodes, params]) { var completer = new Completer(); _serverRequest(method, '${_path}/$extraPath', completer, successCodes, params: params); return completer.future; } Future _get(extraPath, [successCodes]) => _simpleCommand('GET', extraPath, successCodes); Future _post(extraPath, [successCodes, params]) => _simpleCommand('POST', extraPath, successCodes, params); Future _delete(extraPath, [successCodes]) => _simpleCommand('DELETE', extraPath, successCodes); }
Subclasses
WebDriver, WebDriverSession, WebDriverWindow
Constructors
new WebDriverBase([String _host = 'localhost', int _port = 4444, String _path = '/wd/hub']) #
WebDriverBase([ this._host = 'localhost', this._port = 4444, this._path = '/wd/hub']) { _url = 'http://$_host:$_port$_path'; }
new WebDriverBase.fromUrl([String _url = 'http://localhost:4444/wd/hub']) #
The default URL for WebDriver remote server is http://localhost:4444/wd/hub.
WebDriverBase.fromUrl([this._url = 'http://localhost:4444/wd/hub']) { // Break out the URL components. var re = const RegExp('[^:/]+://([^/]+)(/.*)'); var matches = re.firstMatch(_url); _host = matches[1]; _path = matches[2]; var idx = _host.indexOf(':'); if (idx >= 0) { _port = parseInt(_host.substring(idx+1)); _host = _host.substring(0, idx); } else { _port = 80; } }