mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Precise browser resizing with integration_test and driver (#160678)
Fixes #136109 P2 Related #148028 P1 Roadmap: - [x] find why not working (described in #136109) - [x] create new API with backward compatibility (`--browser-dimension=393×852@3`) - [x] fix edge cases - [x] internal testing - [x] add documentation (flutter drive -h) This PR: - Fixes Chrome bug from dart side - Adds pixelRatio mobile emulation in web - Adds new notation: 393,852 -> 393×852[@1] - Leaves previous behavior as it was, so 393,852 will give wrong size until Chrome will fix it. But you can use 393×852@1. --------- Co-authored-by: Mouad Debbar <mdebbar@google.com>
This commit is contained in:
parent
36275e1e71
commit
a9e9ff9eb0
@ -140,11 +140,12 @@ class DriveCommand extends RunCommandBase {
|
|||||||
)
|
)
|
||||||
..addOption(
|
..addOption(
|
||||||
'browser-dimension',
|
'browser-dimension',
|
||||||
defaultsTo: '1600,1024',
|
defaultsTo: '1600x1024',
|
||||||
help:
|
help:
|
||||||
'The dimension of the browser when running a Flutter Web test. '
|
'The dimension of the browser when running a Flutter Web test. '
|
||||||
'This will affect screenshot and all offset-related actions.',
|
'Format is "width x height[@dpr]" where dpr is optional device pixel ratio. '
|
||||||
valueHelp: 'width,height',
|
'This will affect screenshot dimensions and all offset-related actions.',
|
||||||
|
valueHelp: '1600x1024[@1]',
|
||||||
)
|
)
|
||||||
..addFlag(
|
..addFlag(
|
||||||
'android-emulator',
|
'android-emulator',
|
||||||
@ -358,7 +359,7 @@ class DriveCommand extends RunCommandBase {
|
|||||||
chromeBinary: stringArg('chrome-binary'),
|
chromeBinary: stringArg('chrome-binary'),
|
||||||
headless: boolArg('headless'),
|
headless: boolArg('headless'),
|
||||||
webBrowserFlags: stringsArg(FlutterOptions.kWebBrowserFlag),
|
webBrowserFlags: stringsArg(FlutterOptions.kWebBrowserFlag),
|
||||||
browserDimension: stringArg('browser-dimension')!.split(','),
|
browserDimension: stringArg('browser-dimension')!.split(RegExp('[,x@]')),
|
||||||
browserName: stringArg('browser-name'),
|
browserName: stringArg('browser-name'),
|
||||||
driverPort:
|
driverPort:
|
||||||
stringArg('driver-port') != null ? int.tryParse(stringArg('driver-port')!) : null,
|
stringArg('driver-port') != null ? int.tryParse(stringArg('driver-port')!) : null,
|
||||||
|
@ -168,6 +168,38 @@ class WebDriverService extends DriverService {
|
|||||||
}) async {
|
}) async {
|
||||||
late async_io.WebDriver webDriver;
|
late async_io.WebDriver webDriver;
|
||||||
final Browser browser = Browser.fromCliName(browserName);
|
final Browser browser = Browser.fromCliName(browserName);
|
||||||
|
final bool isAndroidChrome = browser == Browser.androidChrome;
|
||||||
|
late int width;
|
||||||
|
late int height;
|
||||||
|
Map<String, dynamic>? mobileEmulation;
|
||||||
|
|
||||||
|
// Do not resize Android Chrome browser.
|
||||||
|
// For PC Chrome use mobileEmulation if dpr is provided.
|
||||||
|
if (!isAndroidChrome && browserDimension != null) {
|
||||||
|
try {
|
||||||
|
final int len = browserDimension.length;
|
||||||
|
if (len != 2 && len != 3) {
|
||||||
|
throw const FormatException();
|
||||||
|
}
|
||||||
|
width = int.parse(browserDimension[0]);
|
||||||
|
height = int.parse(browserDimension[1]);
|
||||||
|
if (len == 3) {
|
||||||
|
mobileEmulation = <String, dynamic>{
|
||||||
|
'deviceMetrics': <String, dynamic>{
|
||||||
|
'width': width,
|
||||||
|
'height': height,
|
||||||
|
'pixelRatio': double.parse(browserDimension[2]),
|
||||||
|
},
|
||||||
|
'userAgent':
|
||||||
|
'Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36 (KHTML, '
|
||||||
|
'like Gecko) Chrome/131.0.6778.200 Mobile Safari/537.36',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} on FormatException {
|
||||||
|
throwToolExit('Browser dimension is invalid. Try --browser-dimension=1600x1024[@1]');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
webDriver = await async_io.createDriver(
|
webDriver = await async_io.createDriver(
|
||||||
uri: Uri.parse('http://localhost:$driverPort/'),
|
uri: Uri.parse('http://localhost:$driverPort/'),
|
||||||
@ -176,6 +208,7 @@ class WebDriverService extends DriverService {
|
|||||||
headless,
|
headless,
|
||||||
webBrowserFlags: webBrowserFlags,
|
webBrowserFlags: webBrowserFlags,
|
||||||
chromeBinary: chromeBinary,
|
chromeBinary: chromeBinary,
|
||||||
|
mobileEmulation: mobileEmulation,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
} on SocketException catch (error) {
|
} on SocketException catch (error) {
|
||||||
@ -188,21 +221,10 @@ class WebDriverService extends DriverService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final bool isAndroidChrome = browser == Browser.androidChrome;
|
if (!isAndroidChrome && browserDimension != null) {
|
||||||
// Do not set the window size for android chrome browser.
|
|
||||||
if (!isAndroidChrome) {
|
|
||||||
assert(browserDimension!.length == 2);
|
|
||||||
late int x;
|
|
||||||
late int y;
|
|
||||||
try {
|
|
||||||
x = int.parse(browserDimension![0]);
|
|
||||||
y = int.parse(browserDimension[1]);
|
|
||||||
} on FormatException catch (ex) {
|
|
||||||
throwToolExit('Dimension provided to --browser-dimension is invalid: $ex');
|
|
||||||
}
|
|
||||||
final async_io.Window window = await webDriver.window;
|
final async_io.Window window = await webDriver.window;
|
||||||
await window.setLocation(const math.Point<int>(0, 0));
|
await window.setLocation(const math.Point<int>(0, 0));
|
||||||
await window.setSize(math.Rectangle<int>(0, 0, x, y));
|
await window.setSize(math.Rectangle<int>(0, 0, width, height));
|
||||||
}
|
}
|
||||||
final int result = await _processUtils.stream(
|
final int result = await _processUtils.stream(
|
||||||
<String>[_dartSdkPath, ...arguments, testFile],
|
<String>[_dartSdkPath, ...arguments, testFile],
|
||||||
@ -305,6 +327,7 @@ Map<String, dynamic> getDesiredCapabilities(
|
|||||||
bool? headless, {
|
bool? headless, {
|
||||||
List<String> webBrowserFlags = const <String>[],
|
List<String> webBrowserFlags = const <String>[],
|
||||||
String? chromeBinary,
|
String? chromeBinary,
|
||||||
|
Map<String, dynamic>? mobileEmulation,
|
||||||
}) => switch (browser) {
|
}) => switch (browser) {
|
||||||
Browser.chrome => <String, dynamic>{
|
Browser.chrome => <String, dynamic>{
|
||||||
'acceptInsecureCerts': true,
|
'acceptInsecureCerts': true,
|
||||||
@ -314,7 +337,6 @@ Map<String, dynamic> getDesiredCapabilities(
|
|||||||
async_io.LogType.performance: 'ALL',
|
async_io.LogType.performance: 'ALL',
|
||||||
},
|
},
|
||||||
'goog:chromeOptions': <String, dynamic>{
|
'goog:chromeOptions': <String, dynamic>{
|
||||||
if (chromeBinary != null) 'binary': chromeBinary,
|
|
||||||
'w3c': true,
|
'w3c': true,
|
||||||
'args': <String>[
|
'args': <String>[
|
||||||
'--bwsi',
|
'--bwsi',
|
||||||
@ -335,6 +357,8 @@ Map<String, dynamic> getDesiredCapabilities(
|
|||||||
'v8,blink.console,benchmark,blink,'
|
'v8,blink.console,benchmark,blink,'
|
||||||
'blink.user_timing',
|
'blink.user_timing',
|
||||||
},
|
},
|
||||||
|
if (chromeBinary != null) 'binary': chromeBinary,
|
||||||
|
if (mobileEmulation != null) 'mobileEmulation': mobileEmulation,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Browser.firefox => <String, dynamic>{
|
Browser.firefox => <String, dynamic>{
|
||||||
|
Loading…
Reference in New Issue
Block a user