Skip to content

Migrate async_html from dart:html to package:web #315

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 2 commits into from
Feb 22, 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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
sdk: [3.1, stable, dev]
sdk: [3.4, stable, dev]
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
Expand All @@ -66,7 +66,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
sdk: [3.1, stable, dev]
sdk: [3.4, stable, dev]
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- uses: dart-lang/setup-dart@e630b99d28a3b71860378cafdc2a067c71107f94
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.2.0-wip

* Require Dart 3.4 and add a dependency on `package:web`.

## 3.1.0

* Add a `reason` argument to `Clock.waitFor`.
Expand Down
6 changes: 3 additions & 3 deletions lib/async_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final Uri defaultUri = Uri.parse('http://127.0.0.1:4444/wd/hub/');

/// Creates a new async WebDriver using [AsyncXhrRequestClient].
///
/// This will bring in dependency on `dart:html`.
/// This will bring in a dependency on the Dart web platform.
/// Note: WebDriver endpoints will be constructed using [resolve] against
/// [uri]. Therefore, if [uri] does not end with a trailing slash, the
/// last path component will be dropped.
Expand All @@ -45,7 +45,7 @@ Future<core.WebDriver> createDriver(
/// Creates an async WebDriver from existing session using
/// [AsyncXhrRequestClient].
///
/// This will bring in dependency on `dart:html`.
/// This will bring in a dependency on the Dart web platform.
/// Note: WebDriver endpoints will be constructed using [resolve] against
/// [uri]. Therefore, if [uri] does not end with a trailing slash, the
/// last path component will be dropped.
Expand All @@ -61,7 +61,7 @@ Future<core.WebDriver> fromExistingSession(String sessionId,
/// [capabilities]) has to be given. Because otherwise, making a call to
/// WebDriver server will make this function async.
///
/// This will bring in dependency on `dart:html`.
/// This will bring in a dependency on the Dart web platform.
/// Note: WebDriver endpoints will be constructed using [resolve] against
/// [uri]. Therefore, if [uri] does not end with a trailing slash, the
/// last path component will be dropped.
Expand Down
41 changes: 20 additions & 21 deletions lib/src/request/async_xhr_request_client.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import 'dart:async';
import 'dart:html';

import 'package:web/web.dart' as web;

import '../../support/async.dart';

import '../common/request.dart';
import '../common/request_client.dart';

/// Async request client using dart:html package.
/// Async request client using `dart:js_interop` through `package:web`.
///
/// On the low level, it's using XMLHttpRequest object (XHR).
class AsyncXhrRequestClient extends AsyncRequestClient {
Expand All @@ -20,33 +21,31 @@ class AsyncXhrRequestClient extends AsyncRequestClient {
Future<WebDriverResponse> sendRaw(WebDriverRequest request) async {
await _lock.acquire();

final headers = {
'Accept': 'application/json',
};

headers.addAll(_headers);
if (request.body != null && request.body!.isNotEmpty) {
headers['Content-Type'] ??= 'application/json';
}

HttpRequest httpRequest;

web.XMLHttpRequest httpRequest;
try {
httpRequest = await HttpRequest.request(resolve(request.uri!).toString(),
method: request.method!.name,
requestHeaders: headers,
sendData: request.body,
mimeType: 'application/json');
} on ProgressEvent catch (e) {
httpRequest = e.target as HttpRequest;
// ignore: deprecated_member_use
httpRequest = await web.HttpRequest.request(
resolve(request.uri!).toString(),
method: request.method!.name,
requestHeaders: {
..._headers,
'Accept': 'application/json',
if (request.body?.isNotEmpty ?? false)
'Content-Type': 'application/json',
},
sendData: request.body,
mimeType: 'application/json',
);
} on web.ProgressEvent catch (e) {
httpRequest = e.target as web.XMLHttpRequest;
} finally {
_lock.release();
}

return WebDriverResponse(
httpRequest.status,
httpRequest.statusText,
httpRequest.response as String?,
httpRequest.responseText,
);
}

Expand Down
5 changes: 3 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
name: webdriver
version: 3.1.0
version: 3.2.0-wip
description: >-
Provides WebDriver bindings for Dart. Supports WebDriver JSON interface and
W3C spec. Requires the use of WebDriver remote server.
repository: https://github.com/google/webdriver.dart

environment:
sdk: ^3.1.0
sdk: ^3.4.0

dependencies:
matcher: ^0.12.10
path: ^1.8.0
stack_trace: ^1.10.0
sync_http: ^0.3.0
web: ^1.1.0

dev_dependencies:
archive: ^3.3.0
Expand Down
Loading