Skip to content

Commit d080ebf

Browse files
authored
Update a number of APIs to return Uint8List (#323)
Specifically: captureScreenshotAsList, captureElementScreenshotAsList Also changed some internal storage to use Uint8List and ByteBuffer And removed some superfluous casts
1 parent 0400c06 commit d080ebf

File tree

11 files changed

+23
-23
lines changed

11 files changed

+23
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Require Dart 3.4 and add a dependency on `package:web`.
44
* Ensure HTTP clients are closed if creating a session fails.
5+
* Update functions that return `List<int>` to return `Uint8List`.
56

67
## 3.1.0
78

lib/src/async/web_driver.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import 'dart:async';
1616
import 'dart:convert';
17+
import 'dart:typed_data';
1718

1819
import '../../sync_core.dart' as sync_core;
1920
import '../common/by.dart';
@@ -203,13 +204,13 @@ class WebDriver implements SearchContext {
203204
_handler.core.parseScreenshotResponse);
204205

205206
/// Take a screenshot of the current page as PNG as list of uint8.
206-
Future<List<int>> captureScreenshotAsList() async {
207+
Future<Uint8List> captureScreenshotAsList() async {
207208
final base64Encoded = captureScreenshotAsBase64();
208209
return base64.decode(await base64Encoded);
209210
}
210211

211212
/// Take a screenshot of the specified element as PNG as list of uint8.
212-
Future<List<int>> captureElementScreenshotAsList(WebElement element) async {
213+
Future<Uint8List> captureElementScreenshotAsList(WebElement element) async {
213214
final base64Encoded = captureElementScreenshotAsBase64(element);
214215
return base64.decode(await base64Encoded);
215216
}

lib/src/common/zip.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Archive {
3333
/// This class represents a file in an archive.
3434
class ArchiveFile {
3535
final String name;
36-
final List<int> content;
36+
final Uint8List content;
3737

3838
ArchiveFile(this.name, this.content);
3939

lib/src/request/async_io_request_client.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'dart:convert';
33
import 'dart:io' show ContentType, HttpClient, HttpClientRequest, HttpHeaders;
44

55
import '../../support/async.dart';
6-
76
import '../common/request.dart';
87
import '../common/request_client.dart';
98

@@ -52,7 +51,7 @@ class AsyncIoRequestClient extends AsyncRequestClient {
5251
final response = await httpRequest.close();
5352

5453
return WebDriverResponse(response.statusCode, response.reasonPhrase,
55-
await utf8.decodeStream(response.cast<List<int>>()));
54+
await utf8.decodeStream(response));
5655
} finally {
5756
_lock.release();
5857
}

lib/src/sync/web_driver.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
import 'dart:convert' show base64;
16+
import 'dart:typed_data';
1617

1718
import '../../async_core.dart' as async_core;
1819
import '../common/by.dart';
@@ -213,13 +214,13 @@ class WebDriver implements SearchContext {
213214
_handler.core.parseScreenshotResponse);
214215

215216
/// Take a screenshot of the current page as PNG as list of uint8.
216-
List<int> captureScreenshotAsList() {
217+
Uint8List captureScreenshotAsList() {
217218
final base64Encoded = captureScreenshotAsBase64();
218219
return base64.decode(base64Encoded);
219220
}
220221

221222
/// Take a screenshot of the specified element as PNG as list of uint8.
222-
List<int> captureElementScreenshotAsList(WebElement element) {
223+
Uint8List captureElementScreenshotAsList(WebElement element) {
223224
final base64Encoded = captureElementScreenshotAsBase64(element);
224225
return base64.decode(base64Encoded);
225226
}

lib/support/firefox_profile.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
import 'dart:collection';
16-
import 'dart:convert' show LineSplitter, base64;
16+
import 'dart:convert' show LineSplitter, base64, utf8;
1717
import 'dart:io' as io;
1818

1919
import 'package:path/path.dart' as path;
@@ -237,11 +237,11 @@ class FirefoxProfile {
237237
}
238238

239239
final prefsJsContent =
240-
prefs.map((option) => option.asPrefString).join('\n').codeUnits;
240+
utf8.encode(prefs.map((option) => option.asPrefString).join('\n'));
241241
archive.addFile(ArchiveFile('prefs.js', prefsJsContent));
242242

243243
final userJsContent =
244-
userPrefs.map((option) => option.asPrefString).join('\n').codeUnits;
244+
utf8.encode(userPrefs.map((option) => option.asPrefString).join('\n'));
245245
archive.addFile(ArchiveFile('user.js', userJsContent));
246246

247247
final zipData = ZipEncoder.encode(archive);

lib/support/stdio_stepper.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import 'dart:async' show StreamController;
1616
import 'dart:convert' show Encoding, json;
1717
import 'dart:io' show Stdin, exit, stdin, systemEncoding;
18+
import 'dart:typed_data';
1819

1920
import '../src/async/stepper.dart';
2021

@@ -82,7 +83,7 @@ class LineReader {
8283
static const lf = 10;
8384

8485
bool _crPrevious = false;
85-
final _bytes = <int>[];
86+
final _bytes = BytesBuilder();
8687
final _controller = StreamController<String>.broadcast();
8788

8889
final Encoding encoding;
@@ -113,10 +114,9 @@ class LineReader {
113114
}
114115
_crPrevious = byte == cr;
115116
if (byte == cr || byte == lf) {
116-
_controller.add(encoding.decode(_bytes));
117-
_bytes.clear();
117+
_controller.add(encoding.decode(_bytes.takeBytes()));
118118
} else {
119-
_bytes.add(byte);
119+
_bytes.addByte(byte);
120120
}
121121
}
122122

test/configs/async_io_config.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Future<void> createTestServerAndGoToTestPage(WebDriver driver) async {
5858
request.response
5959
..statusCode = HttpStatus.ok
6060
..headers.set('Content-type', 'text/html');
61-
file.openRead().cast<List<int>>().pipe(request.response);
61+
file.openRead().pipe(request.response);
6262
} else {
6363
request.response
6464
..statusCode = HttpStatus.notFound

test/configs/sync_io_config.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Future<void> _runServer(SendPort send) async {
6060
request.response
6161
..statusCode = HttpStatus.ok
6262
..headers.set('Content-type', 'text/html');
63-
file.openRead().cast<List<int>>().pipe(request.response);
63+
file.openRead().pipe(request.response);
6464
} else {
6565
request.response
6666
..statusCode = HttpStatus.notFound

test/support/firefox_profile_test.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ void main() {
141141

142142
final prefs = FirefoxProfile.loadPrefsFile(MockFile(
143143
String.fromCharCodes(
144-
zipArchive.files.firstWhere((f) => f.name == 'user.js').content
145-
as List<int>,
144+
zipArchive.files.firstWhere((f) => f.name == 'user.js').content,
146145
),
147146
));
148147
expect(
@@ -179,8 +178,7 @@ void main() {
179178
final prefs = FirefoxProfile.loadPrefsFile(
180179
MockFile(
181180
String.fromCharCodes(
182-
zipArchive.files.firstWhere((f) => f.name == 'user.js').content
183-
as List<int>,
181+
zipArchive.files.firstWhere((f) => f.name == 'user.js').content,
184182
),
185183
),
186184
);

0 commit comments

Comments
 (0)