-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathasync_core.dart
137 lines (117 loc) · 4.81 KB
/
async_core.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2015 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:async' show Future;
import 'dart:collection' show UnmodifiableMapView;
import 'src/async/web_driver.dart';
import 'src/common/capabilities.dart';
import 'src/common/request_client.dart';
import 'src/common/session.dart';
import 'src/common/spec.dart';
import 'src/common/utils.dart';
export 'package:webdriver/src/async/alert.dart';
export 'package:webdriver/src/async/common.dart';
export 'package:webdriver/src/async/cookies.dart';
export 'package:webdriver/src/async/keyboard.dart';
export 'package:webdriver/src/async/logs.dart';
export 'package:webdriver/src/async/mouse.dart';
export 'package:webdriver/src/async/target_locator.dart';
export 'package:webdriver/src/async/timeouts.dart';
export 'package:webdriver/src/async/web_driver.dart';
export 'package:webdriver/src/async/web_element.dart';
export 'package:webdriver/src/async/window.dart';
export 'package:webdriver/src/common/by.dart';
export 'package:webdriver/src/common/capabilities.dart';
export 'package:webdriver/src/common/command_event.dart';
export 'package:webdriver/src/common/cookie.dart';
export 'package:webdriver/src/common/exception.dart';
export 'package:webdriver/src/common/log.dart';
export 'package:webdriver/src/common/mouse.dart';
export 'package:webdriver/src/common/spec.dart';
final Uri defaultUri = Uri.parse('http://127.0.0.1:4444/wd/hub/');
/// Creates a new async [WebDriver].
///
/// This is intended for internal use! Please use [createDriver] from
/// `async_io.dart` or `async_html.dart`.
Future<WebDriver> createDriver(
AsyncRequestClient Function(Uri prefix) createRequestClient,
{Uri? uri,
Map<String, dynamic>? desired,
WebDriverSpec spec = WebDriverSpec.Auto}) async {
uri ??= defaultUri;
// This client's prefix at root, it has no session prefix in it.
final client = createRequestClient(uri);
final handler = getHandler(spec);
final SessionInfo session;
try {
session = await client.send(
handler.session.buildCreateRequest(desired: desired),
handler.session.parseCreateResponse);
} finally {
client.close();
}
if (session.spec != WebDriverSpec.JsonWire &&
session.spec != WebDriverSpec.W3c) {
throw 'Unexpected spec: ${session.spec}';
}
return WebDriver(uri, session.id, UnmodifiableMapView(session.capabilities!),
createRequestClient(uri.resolve('session/${session.id}/')), session.spec);
}
/// Creates an async [WebDriver] from existing session.
///
/// This is intended for internal use! Please use [fromExistingSession] from
/// `async_io.dart` or `async_html.dart`.
Future<WebDriver> fromExistingSession(
AsyncRequestClient Function(Uri prefix) createRequestClient,
String sessionId,
{Uri? uri,
WebDriverSpec spec = WebDriverSpec.Auto}) async {
uri ??= defaultUri;
// This client's prefix at root, it has no session prefix in it.
final client = createRequestClient(uri);
final handler = getHandler(spec);
final SessionInfo session;
try {
session = await client.send(handler.session.buildInfoRequest(sessionId),
(response) => handler.session.parseInfoResponse(response, sessionId));
} finally {
client.close();
}
if (session.spec != WebDriverSpec.JsonWire &&
session.spec != WebDriverSpec.W3c) {
throw 'Unexpected spec: ${session.spec}';
}
return WebDriver(uri, session.id, UnmodifiableMapView(session.capabilities!),
createRequestClient(uri.resolve('session/${session.id}/')), session.spec);
}
/// Creates an async [WebDriver] from existing session with a sync function.
///
/// This will be helpful when you can't use async when creating WebDriver. For
/// example in a constructor.
///
/// This is intended for internal use! Please use [fromExistingSessionSync] from
/// `async_io.dart` or `async_html.dart`.
WebDriver fromExistingSessionSync(
AsyncRequestClient Function(Uri prefix) createRequestClient,
String sessionId,
WebDriverSpec spec,
{Uri? uri,
Map<String, dynamic>? capabilities}) {
uri ??= defaultUri;
capabilities ??= Capabilities.empty;
if (spec != WebDriverSpec.JsonWire && spec != WebDriverSpec.W3c) {
throw 'Unexpected spec: $spec';
}
return WebDriver(uri, sessionId, UnmodifiableMapView(capabilities),
createRequestClient(uri.resolve('session/$sessionId/')), spec);
}