diff --git a/analysis_options.yaml b/analysis_options.yaml index e91d53d..97e1b77 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -25,6 +25,8 @@ linter: - prefer_const_declarations - prefer_expression_function_bodies - prefer_final_locals + - unnecessary_breaks + - use_enums - use_if_null_to_convert_nulls_to_bools - use_raw_strings - use_string_buffers diff --git a/lib/src/common/by.dart b/lib/src/common/by.dart index 84a9eee..a122c3a 100644 --- a/lib/src/common/by.dart +++ b/lib/src/common/by.dart @@ -35,24 +35,14 @@ class By { @override String toString() { - var constructor = using; - switch (using) { - case 'link text': - constructor = 'linkText'; - break; - case 'partial link text': - constructor = 'partialLinkText'; - break; - case 'tag name': - constructor = 'tagName'; - break; - case 'class name': - constructor = 'className'; - break; - case 'css selector': - constructor = 'cssSelector'; - break; - } + final constructor = switch (using) { + 'link text' => 'linkText', + 'partial link text' => 'partialLinkText', + 'tag name' => 'tagName', + 'class name' => 'className', + 'css selector' => 'cssSelector', + _ => using + }; return 'By.$constructor($value)'; } diff --git a/lib/src/common/exception.dart b/lib/src/common/exception.dart index 31ae937..f2dde60 100644 --- a/lib/src/common/exception.dart +++ b/lib/src/common/exception.dart @@ -255,25 +255,22 @@ WebDriverException getExceptionFromJsonWireResponse( WebDriverException getExceptionFromW3cResponse({ int? httpStatusCode, String? httpReasonPhrase, - dynamic jsonResp, + Object? jsonResp, }) { if (jsonResp is Map && jsonResp.keys.contains('value')) { final value = jsonResp['value'] as Map; - switch (value['error']) { - case 'invalid argument': - return InvalidArgumentException( + return switch (value['error']) { + 'invalid argument' => InvalidArgumentException( httpStatusCode, value['message'] as String?, - ); - case 'no such element': - return NoSuchElementException( + ), + 'no such element' => NoSuchElementException( httpStatusCode, value['message'] as String?, - ); - default: - return WebDriverException(httpStatusCode, value['message'] as String?); - } + ), + _ => WebDriverException(httpStatusCode, value['message'] as String?), + }; } return InvalidResponseException(httpStatusCode, jsonResp.toString()); diff --git a/lib/src/common/request.dart b/lib/src/common/request.dart index f10931c..2048120 100644 --- a/lib/src/common/request.dart +++ b/lib/src/common/request.dart @@ -1,16 +1,30 @@ +// Copyright 2019 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. + import 'dart:convert'; -class HttpMethod { +enum HttpMethod { + httpGet('GET'), + httpPost('POST'), + httpDelete('DELETE'); + final String name; - const HttpMethod._(this.name); + const HttpMethod(this.name); @override String toString() => 'HttpMethod.$name'; - - static const httpGet = HttpMethod._('GET'); - static const httpPost = HttpMethod._('POST'); - static const httpDelete = HttpMethod._('DELETE'); } /// Request data to send to WebDriver. diff --git a/lib/src/common/utils.dart b/lib/src/common/utils.dart index 27d856b..8b7e5d3 100644 --- a/lib/src/common/utils.dart +++ b/lib/src/common/utils.dart @@ -4,13 +4,8 @@ import '../handler/w3c_handler.dart'; import 'spec.dart'; import 'webdriver_handler.dart'; -WebDriverHandler getHandler(WebDriverSpec spec) { - switch (spec) { - case WebDriverSpec.JsonWire: - return JsonWireWebDriverHandler(); - case WebDriverSpec.W3c: - return W3cWebDriverHandler(); - case WebDriverSpec.Auto: - return InferWebDriverHandler(); - } -} +WebDriverHandler getHandler(WebDriverSpec spec) => switch (spec) { + WebDriverSpec.JsonWire => JsonWireWebDriverHandler(), + WebDriverSpec.W3c => W3cWebDriverHandler(), + WebDriverSpec.Auto => InferWebDriverHandler(), + }; diff --git a/lib/src/handler/w3c/element_finder.dart b/lib/src/handler/w3c/element_finder.dart index a52a921..c60f862 100644 --- a/lib/src/handler/w3c/element_finder.dart +++ b/lib/src/handler/w3c/element_finder.dart @@ -9,31 +9,22 @@ class W3cElementFinder extends ElementFinder { /// In principle, W3C spec implementations should be nearly the same as /// the existing JSON wire spec. In practice compliance is uneven. Map _byToJson(By by) { - String using; - String value; + final (using, value) = switch (by.using) { + // This doesn't exist in the W3C spec. + 'id' => ('css selector', '#${by.value}'), + + // This doesn't exist in the W3C spec. + 'name' => ('css selector', '[name=${by.value}]'), + + // This is in the W3C spec, but not in geckodriver. + 'tag name' => ('css selector', by.value), + + // This doesn't exist in the W3C spec. + 'class name' => ('css selector', '.${by.value}'), - switch (by.using) { - case 'id': // This doesn't exist in the W3C spec. - using = 'css selector'; - value = '#${by.value}'; - break; - case 'name': // This doesn't exist in the W3C spec. - using = 'css selector'; - value = '[name=${by.value}]'; - break; - case 'tag name': // This is in the W3C spec, but not in geckodriver. - using = 'css selector'; - value = by.value; - break; - case 'class name': // This doesn't exist in the W3C spec. - using = 'css selector'; - value = '.${by.value}'; - break; // xpath, css selector, link text, partial link text, seem fine. - default: - using = by.using; - value = by.value; - } + _ => (by.using, by.value), + }; return {'using': using, 'value': value}; } diff --git a/lib/src/request/async_io_request_client.dart b/lib/src/request/async_io_request_client.dart index 627e160..854f26c 100644 --- a/lib/src/request/async_io_request_client.dart +++ b/lib/src/request/async_io_request_client.dart @@ -1,6 +1,6 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:io' show ContentType, HttpClient, HttpClientRequest, HttpHeaders; +import 'dart:io' show ContentType, HttpClient, HttpHeaders; import '../../support/async.dart'; import '../common/request.dart'; @@ -19,19 +19,13 @@ class AsyncIoRequestClient extends AsyncRequestClient { @override Future sendRaw(WebDriverRequest request) async { await _lock.acquire(); - late HttpClientRequest httpRequest; - switch (request.method) { - case HttpMethod.httpGet: - httpRequest = await client.getUrl(resolve(request.uri!)); - break; - case HttpMethod.httpPost: - httpRequest = await client.postUrl(resolve(request.uri!)); - break; - case HttpMethod.httpDelete: - httpRequest = await client.deleteUrl(resolve(request.uri!)); - break; - } + final requestUri = resolve(request.uri!); + final httpRequest = switch (request.method!) { + HttpMethod.httpGet => await client.getUrl(requestUri), + HttpMethod.httpPost => await client.postUrl(requestUri), + HttpMethod.httpDelete => await client.deleteUrl(requestUri) + }; httpRequest.followRedirects = true; _headers.forEach(httpRequest.headers.add); diff --git a/lib/src/request/sync_http_request_client.dart b/lib/src/request/sync_http_request_client.dart index 2ab596b..b22ca49 100644 --- a/lib/src/request/sync_http_request_client.dart +++ b/lib/src/request/sync_http_request_client.dart @@ -13,20 +13,18 @@ class SyncHttpRequestClient extends SyncRequestClient { @override WebDriverResponse sendRaw(WebDriverRequest request) { - late SyncHttpClientRequest httpRequest; - switch (request.method) { + final requestUri = resolve(request.uri!); + final SyncHttpClientRequest httpRequest; + switch (request.method!) { case HttpMethod.httpGet: - httpRequest = SyncHttpClient.getUrl(resolve(request.uri!)); - break; + httpRequest = SyncHttpClient.getUrl(requestUri); case HttpMethod.httpPost: - httpRequest = SyncHttpClient.postUrl(resolve(request.uri!)); + httpRequest = SyncHttpClient.postUrl(requestUri); httpRequest.headers.contentType = ContentType('application', 'json', charset: 'utf-8'); httpRequest.write(request.body); - break; case HttpMethod.httpDelete: - httpRequest = SyncHttpClient.deleteUrl(resolve(request.uri!)); - break; + httpRequest = SyncHttpClient.deleteUrl(requestUri); } _headers.forEach(httpRequest.headers.add); diff --git a/lib/support/stdio_stepper.dart b/lib/support/stdio_stepper.dart index 25371e2..74ae4c9 100644 --- a/lib/support/stdio_stepper.dart +++ b/lib/support/stdio_stepper.dart @@ -51,7 +51,6 @@ class StdioStepper implements Stepper { case 'help': case 'h': _printUsage(); - break; case 'disable': case 'd': enabled = false; diff --git a/test/configs/common_config.dart b/test/configs/common_config.dart index ce542ba..659ad6c 100644 --- a/test/configs/common_config.dart +++ b/test/configs/common_config.dart @@ -35,40 +35,32 @@ String get testHostname => '127.0.0.1'; String get testHomePath => path.absolute('test'); -Uri? getWebDriverUri(WebDriverSpec spec) { - switch (spec) { - case WebDriverSpec.W3c: - return _defaultFirefoxUri; - case WebDriverSpec.JsonWire: - return _defaultChromeUri; - default: - return null; - } -} +Uri? getWebDriverUri(WebDriverSpec spec) => switch (spec) { + WebDriverSpec.W3c => _defaultFirefoxUri, + WebDriverSpec.JsonWire => _defaultChromeUri, + _ => null, + }; -Map getCapabilities(WebDriverSpec spec) { - switch (spec) { - case WebDriverSpec.W3c: - return Capabilities.firefox; - case WebDriverSpec.JsonWire: - final capabilities = Capabilities.chrome; - final env = Platform.environment; +Map getCapabilities(WebDriverSpec spec) => switch (spec) { + WebDriverSpec.W3c => Capabilities.firefox, + WebDriverSpec.JsonWire => () { + final capabilities = Capabilities.chrome; + final env = Platform.environment; - final chromeOptions = {}; + final chromeOptions = {}; - if (env['CHROMEDRIVER_BINARY'] != null) { - chromeOptions['binary'] = env['CHROMEDRIVER_BINARY']; - } + if (env['CHROMEDRIVER_BINARY'] != null) { + chromeOptions['binary'] = env['CHROMEDRIVER_BINARY']; + } - if (env['CHROMEDRIVER_ARGS'] != null) { - chromeOptions['args'] = env['CHROMEDRIVER_ARGS']!.split(' '); - } + if (env['CHROMEDRIVER_ARGS'] != null) { + chromeOptions['args'] = env['CHROMEDRIVER_ARGS']!.split(' '); + } - if (chromeOptions.isNotEmpty) { - capabilities[Capabilities.chromeOptions] = chromeOptions; - } - return capabilities; - default: - return {}; - } -} + if (chromeOptions.isNotEmpty) { + capabilities[Capabilities.chromeOptions] = chromeOptions; + } + return capabilities; + }(), + _ => {} + };