Skip to content
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

Support getting current timeouts in sync w3c drivers. #281

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 3.0.4-wip

* Require Dart 3.0.
* Add support for reading and setting webdriver timeouts with the sync client.
* Require Dart 3.0.0

## 3.0.3

Expand Down
22 changes: 22 additions & 0 deletions lib/src/common/timeouts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class TimeoutValues {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakemac53 @lrhn - Would you have any concerns about using a record type with named fields in place of this class? The package is already on language version 3.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No concerns. It means there won't be a nice toString, possibly it will lack the names in production.
But the toString is for debugging only (there is no parse), so that shouldn't be a problem.

I wouldn't introduce a typedef then, just inline the type in the function declaration:
({Duration script, Duration implicit, Duration plageLoad}),
as a multi-value result.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need to worry about this interface changing. Without the class it could be harder to add more fields, because the record types are unrelated once we add a new named field. With the class (if we make it final class) we can add a new field as long at is isn't required in the constructor.

final Duration script;
final Duration implicit;
final Duration pageLoad;

TimeoutValues(
{required this.script, required this.implicit, required this.pageLoad});

@override
String toString() =>
'TimeoutValues(script: $script, implicit: $implicit, pageLoad: $pageLoad)';

@override
int get hashCode => Object.hashAll([script, implicit, pageLoad]);

@override
bool operator ==(Object other) =>
other is TimeoutValues &&
script == other.script &&
implicit == other.implicit &&
pageLoad == other.pageLoad;
}
7 changes: 7 additions & 0 deletions lib/src/common/webdriver_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';
import '../../async_core.dart';
import 'request.dart';
import 'session.dart';
import 'timeouts.dart';

/// Handler for spec related request building and response parsing.
///
Expand Down Expand Up @@ -502,6 +503,12 @@ abstract class TimeoutsHandler {

/// Parses response for 'Set Page Load Timeout'.
void parseSetPageLoadTimeoutResponse(WebDriverResponse response);

/// Builds request for 'Get Timeouts'.
WebDriverRequest buildGetTimeoutsRequest();

/// Parses response for 'Get Timeouts'.
TimeoutValues parseGetTimeoutsResponse(WebDriverResponse response);
}

/// Handler for retrieving logs.
Expand Down
13 changes: 13 additions & 0 deletions lib/src/handler/json_wire/timeouts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

import '../../common/request.dart';
import '../../common/timeouts.dart';
import '../../common/webdriver_handler.dart';
import 'utils.dart';

Expand Down Expand Up @@ -47,4 +48,16 @@ class JsonWireTimeoutsHandler extends TimeoutsHandler {
void parseSetPageLoadTimeoutResponse(WebDriverResponse response) {
parseJsonWireResponse(response);
}

@override
WebDriverRequest buildGetTimeoutsRequest() {
// Not supported by JSON protocol:
// https://www.selenium.dev/documentation/legacy/json_wire_protocol/#command-summary
throw UnimplementedError();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we plan to support it?
Otherwise use UnsupportedError.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not aware of plans to support getting timeouts in the JsonWire webdriver API. Switched to UnsupportedError.

}

@override
TimeoutValues parseGetTimeoutsResponse(WebDriverResponse response) {
throw UnimplementedError();
}
}
15 changes: 15 additions & 0 deletions lib/src/handler/w3c/timeouts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

import '../../common/request.dart';
import '../../common/timeouts.dart';
import '../../common/webdriver_handler.dart';
import 'utils.dart';

Expand Down Expand Up @@ -46,4 +47,18 @@ class W3cTimeoutsHandler extends TimeoutsHandler {
void parseSetPageLoadTimeoutResponse(WebDriverResponse response) {
parseW3cResponse(response);
}

@override
WebDriverRequest buildGetTimeoutsRequest() =>
WebDriverRequest.getRequest('timeouts');

@override
TimeoutValues parseGetTimeoutsResponse(WebDriverResponse response) {
final timeouts = parseW3cResponse(response) as Map<String, dynamic>;
return TimeoutValues(
script: Duration(milliseconds: timeouts['script'] as int),
implicit: Duration(milliseconds: timeouts['implicit'] as int),
pageLoad: Duration(milliseconds: timeouts['pageLoad'] as int),
);
}
}
8 changes: 7 additions & 1 deletion lib/src/sync/timeouts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
// limitations under the License.

import '../common/request_client.dart';
import '../common/timeouts.dart';
import '../common/webdriver_handler.dart';

/// Sets WebDriver timeouts.
/// Sets or gets WebDriver timeouts.
class Timeouts {
final SyncRequestClient _client;
final WebDriverHandler _handler;
Expand All @@ -40,6 +41,11 @@ class Timeouts {
_handler.timeouts.parseSetPageLoadTimeoutResponse);
}

/// Gets the current timeout values.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a getter?
(Unlss we specifically want it to match the underlying W3C API.)

And the operations above should probably have been setters, but they didn't have getters, which was an argument against that. Should they have getters now?
Does it make sense to get the individual timeouts?)
Would it make sense to set all the timeouts in one batch?

The assymmetry is irksome :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to change the existing API. We could introduce a new setter for all the timeouts though. The W3C API would support setting them all in one go:
https://www.w3.org/TR/webdriver2/#set-timeouts
https://github.com/google/webdriver.dart/blob/master/lib/src/handler/w3c/timeouts.dart#L21

I can do that if you think it's worth it.

TimeoutValues getAllTimeouts() => _client.send(
_handler.timeouts.buildGetTimeoutsRequest(),
_handler.timeouts.parseGetTimeoutsResponse);

@override
String toString() => '$_handler.timeouts($_client)';

Expand Down
2 changes: 1 addition & 1 deletion test/firefox_timeouts_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import 'package:test/test.dart';
import 'package:webdriver/sync_core.dart';

import 'sync/timeouts.dart';
import 'sync/timeouts_w3c.dart';

void main() {
runTests(spec: WebDriverSpec.W3c);
Expand Down
45 changes: 45 additions & 0 deletions test/sync/timeouts_w3c.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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.

@TestOn('vm')
library webdriver.options_test;

import 'package:test/test.dart';
import 'package:webdriver/src/common/timeouts.dart';
import 'package:webdriver/sync_core.dart';

import '../configs/sync_io_config.dart' as config;

void runTests({WebDriverSpec spec = WebDriverSpec.Auto}) {
group('TimeOuts', () {
late WebDriver driver;

setUp(() {
driver = config.createTestDriver(spec: spec);
});

test('set all timeouts', () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is testing "getting", so should probably be named as such.
I'd start the test name with a capital letter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forked this from https://github.com/google/webdriver.dart/blob/master/test/sync/timeouts.dart#L32, and so the main purpose is still to set the timeouts.

const five = Duration(seconds: 5);
const one = Duration(seconds: 1);
const ten = Duration(seconds: 10);

driver.timeouts.setScriptTimeout(five);
driver.timeouts.setImplicitTimeout(one);
driver.timeouts.setPageLoadTimeout(ten);

expect(driver.timeouts.getAllTimeouts(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you don't set them timeouts before doing the get?
Does it work? (Do they have default values? Are those default values known?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test that gets the default values. However, I don't know if these come from our Dart webdriver code or from geckodriver. If the latter, we'd be exposing the test to changes in these defaults in geckodriver.

TimeoutValues(script: five, implicit: one, pageLoad: ten));
});
}, timeout: const Timeout(Duration(minutes: 2)));
}