Skip to content

Utilize switch expressions #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 8 additions & 18 deletions lib/src/common/by.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)';
}

Expand Down
19 changes: 8 additions & 11 deletions lib/src/common/exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object?>;

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());
Expand Down
26 changes: 20 additions & 6 deletions lib/src/common/request.dart
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
15 changes: 5 additions & 10 deletions lib/src/common/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
37 changes: 14 additions & 23 deletions lib/src/handler/w3c/element_finder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> _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};
}
Expand Down
20 changes: 7 additions & 13 deletions lib/src/request/async_io_request_client.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -19,19 +19,13 @@ class AsyncIoRequestClient extends AsyncRequestClient {
@override
Future<WebDriverResponse> 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);
Expand Down
14 changes: 6 additions & 8 deletions lib/src/request/sync_http_request_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion lib/support/stdio_stepper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class StdioStepper implements Stepper {
case 'help':
case 'h':
_printUsage();
break;
case 'disable':
case 'd':
enabled = false;
Expand Down
56 changes: 24 additions & 32 deletions test/configs/common_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> getCapabilities(WebDriverSpec spec) {
switch (spec) {
case WebDriverSpec.W3c:
return Capabilities.firefox;
case WebDriverSpec.JsonWire:
final capabilities = Capabilities.chrome;
final env = Platform.environment;
Map<String, Object?> getCapabilities(WebDriverSpec spec) => switch (spec) {
WebDriverSpec.W3c => Capabilities.firefox,
WebDriverSpec.JsonWire => () {
final capabilities = Capabilities.chrome;
final env = Platform.environment;

final chromeOptions = <String, dynamic>{};
final chromeOptions = <String, Object?>{};

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 <String, dynamic>{};
}
}
if (chromeOptions.isNotEmpty) {
capabilities[Capabilities.chromeOptions] = chromeOptions;
}
return capabilities;
}(),
_ => <String, Object?>{}
};