Skip to content

Commit

Permalink
Fix webview tests on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
piotruela committed Oct 2, 2024
1 parent 24e26f7 commit 3d6d6f9
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 70 deletions.
47 changes: 28 additions & 19 deletions dev/e2e_app/integration_test/external_link_test.dart
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
import 'dart:io';

import 'common.dart';

void main() {
patrol('Open external url', ($) async {
await createApp($);

await $.native.openUrl('https://leancode.co');
await $.native2.openUrl('https://leancode.co');

try {
await $.native.tap(Selector(text: 'Use without an account'));
} on PatrolActionException catch (_) {
// ignore
}
await $.pump(Duration(seconds: 5));

try {
await $.native.tap(Selector(text: 'Contact us'));
await $.native2.tap(
NativeSelector(
android: AndroidSelector(text: 'Use without an account'),
),
);
} on PatrolActionException catch (_) {
// ignore
}

try {
await $.native.tap(Selector(text: 'No thanks'));
await $.native2.tap(
NativeSelector(
android: AndroidSelector(text: 'No thanks'),
ios: IOSSelector(label: 'No thanks'),
),
appId: 'com.apple.mobilesafari',
);
} on PatrolActionException catch (_) {
// ignore
}

try {
await $.native.tap(Selector(text: 'ACCEPT ALL COOKIES'));
await $.native2.tap(
NativeSelector(
android: AndroidSelector(text: 'ACCEPT ALL COOKIES'),
ios: IOSSelector(label: 'ACCEPT ALL COOKIES'),
),
appId: 'com.apple.mobilesafari',
);
} on PatrolActionException catch (_) {
// ignore
}

if (Platform.isIOS) {
await $.native.waitUntilVisible(
Selector(text: 'Subscribe'),
appId: 'com.apple.mobilesafari',
);
} else {
await $.native.waitUntilVisible(Selector(text: 'Subscribe'));
}
await $.native2.waitUntilVisible(
NativeSelector(
android: AndroidSelector(text: 'Contact us'),
ios: IOSSelector(label: 'Contact us'),
),
appId: 'com.apple.mobilesafari',
);
});
}
21 changes: 15 additions & 6 deletions dev/e2e_app/integration_test/webview_hackernews_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ void main() {

await $('Open webview (Hacker News)').scrollTo().tap();

await Future<void>.delayed(const Duration(seconds: 3));
await $.pump(Duration(seconds: 3));

await $.native.tap(Selector(text: 'login'));
await $.native.enterTextByIndex(
Expand All @@ -26,22 +26,31 @@ void main() {

await $('Open webview (Hacker News)').scrollTo().tap();

await Future<void>.delayed(const Duration(seconds: 3));
await $.pump(Duration(seconds: 3));

await $.native2.tap(
NativeSelector(
android: AndroidSelector(text: 'login'),
ios: IOSSelector(label: 'login'),
),
);
await $.native2.enterTextByIndex(
'[email protected]',
index: 0,
await $.native2.enterText(
NativeSelector(
android: AndroidSelector(
className: 'android.widget.EditText',
instance: 0,
),
ios: IOSSelector(elementType: IOSElementType.textField),
),
text: '[email protected]',
keyboardBehavior: KeyboardBehavior.showAndDismiss,
);
await $.native2.enterText(
NativeSelector(
android: AndroidSelector(className: 'android.widget.EditText'),
android: AndroidSelector(
className: 'android.widget.EditText',
instance: 1,
),
ios: IOSSelector(elementType: IOSElementType.secureTextField),
),
text: 'ny4ncat',
Expand Down
36 changes: 15 additions & 21 deletions dev/e2e_app/integration_test/webview_leancode_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io' as io;

import 'common.dart';

void main() {
Expand All @@ -22,32 +20,28 @@ void main() {
}
await $.pumpAndSettle();

if (io.Platform.isIOS) {
await $.native2.scrollTo(
NativeSelector(
ios: IOSSelector(placeholderValue: 'Type your email'),
),
maxScrolls: 20,
);
}
final emailInputSelector = NativeSelector(
android: AndroidSelector(className: 'android.widget.EditText'),
ios: IOSSelector(placeholderValue: 'Type your email'),
);

await $.pump(Duration(seconds: 5));
await $.native2.scrollTo(emailInputSelector, maxScrolls: 20);

await $.pump(Duration(seconds: 2));

await $.native2.enterText(
NativeSelector(
android: AndroidSelector(className: 'android.widget.EditText'),
ios: IOSSelector(placeholderValue: 'Type your email'),
),
emailInputSelector,
text: '[email protected]',
keyboardBehavior: KeyboardBehavior.showAndDismiss,
keyboardBehavior: KeyboardBehavior.alternative,
tapLocation: Offset(0.5, 0.5),
);

await $.native2.tap(
NativeSelector(
android: AndroidSelector(text: 'Subscribe'),
ios: IOSSelector(label: 'Subscribe'),
),
final subscribeButtonSelector = NativeSelector(
android: AndroidSelector(text: 'Subscribe'),
ios: IOSSelector(label: 'Subscribe'),
);

await $.native2.scrollTo(subscribeButtonSelector);
await $.native2.tap(subscribeButtonSelector);
});
}
28 changes: 24 additions & 4 deletions dev/e2e_app/integration_test/webview_stackoverflow_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ void main() {

await $('Open webview (StackOverflow)').scrollTo().tap();

await $.pump(Duration(seconds: 2));

try {
await $.native.tap(Selector(text: 'Accept all cookies'));
} on PatrolActionException catch (_) {
Expand All @@ -18,14 +20,22 @@ void main() {
await $.pump(Duration(seconds: 2));

// bug: using `Email` and `Password` selectors doesn't work (#1554)
await $.native.enterTextByIndex('[email protected]', index: 0);
await $.native.enterTextByIndex(
'[email protected]',
index: 0,
keyboardBehavior: KeyboardBehavior.alternative,
);

await $.native.swipe(
from: Offset(0.5, 0.5),
to: Offset(0.5, 0.1),
);

await $.native.enterTextByIndex('ny4ncat', index: 1);
await $.native.enterTextByIndex(
'ny4ncat',
index: 1,
keyboardBehavior: KeyboardBehavior.alternative,
);
await $.native.tap(Selector(text: 'Log in'));
},
);
Expand All @@ -37,6 +47,8 @@ void main() {

await $('Open webview (StackOverflow)').scrollTo().tap();

await $.pump(Duration(seconds: 2));

try {
await $.native2.tap(
NativeSelector(
Expand All @@ -57,14 +69,22 @@ void main() {
await $.pump(Duration(seconds: 2));

// bug: using `Email` and `Password` selectors doesn't work (#1554)
await $.native2.enterTextByIndex('[email protected]', index: 0);
await $.native2.enterTextByIndex(
'[email protected]',
index: 0,
keyboardBehavior: KeyboardBehavior.alternative,
);

await $.native.swipe(
from: Offset(0.5, 0.5),
to: Offset(0.5, 0.1),
);

await $.native2.enterTextByIndex('ny4ncat', index: 1);
await $.native2.enterTextByIndex(
'ny4ncat',
index: 1,
keyboardBehavior: KeyboardBehavior.alternative,
);
await $.native2.tap(
NativeSelector(
android: AndroidSelector(text: 'Log in'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,6 @@ fun AndroidSelector.toUiSelector(): UiSelector {
}

fun AndroidSelector.toBySelector(): BySelector {
if (hasInstance()) {
throw IllegalArgumentException("instance() argument is not supported for BySelector")
}

var selector: BySelector? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,26 +216,26 @@ extension Selector {
inApp bundleId: String,
dismissKeyboard: Bool,
withTimeout timeout: TimeInterval?,
dx: CGFloat,
dy: CGFloat
dx: Double?,
dy: Double?
) throws
func enterText(
_ data: String,
on selector: IOSSelector,
inApp bundleId: String,
dismissKeyboard: Bool,
withTimeout timeout: TimeInterval?,
dx: CGFloat,
dy: CGFloat
dx: Double?,
dy: Double?
) throws
func enterText(
_ data: String,
byIndex index: Int,
inApp bundleId: String,
dismissKeyboard: Bool,
withTimeout timeout: TimeInterval?,
dx: CGFloat,
dy: CGFloat
dx: Double?,
dy: Double?
) throws
func swipe(from start: CGVector, to end: CGVector, inApp bundleId: String) throws
func scrollTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@
inApp bundleId: String,
dismissKeyboard: Bool,
withTimeout timeout: TimeInterval?,
dx: CGFloat,
dy: CGFloat
dx: Double?,
dy: Double?
) throws {
var data = data
if dismissKeyboard {
Expand Down Expand Up @@ -238,8 +238,8 @@
inApp bundleId: String,
dismissKeyboard: Bool,
withTimeout timeout: TimeInterval?,
dx: CGFloat,
dy: CGFloat
dx: Double?,
dy: Double?
) throws {
var data = data
if dismissKeyboard {
Expand Down Expand Up @@ -276,8 +276,8 @@
inApp bundleId: String,
dismissKeyboard: Bool,
withTimeout timeout: TimeInterval?,
dx: CGFloat,
dy: CGFloat
dx: Double?,
dy: Double?
) throws {
var data = data
if dismissKeyboard {
Expand Down Expand Up @@ -924,7 +924,7 @@
}

// MARK: Private stuff
private func clearAndEnterText(data: String, element: XCUIElement, dx: CGFloat?, dy: CGFloat?) {
private func clearAndEnterText(data: String, element: XCUIElement, dx: Double?, dy: Double?) {
let currentValue = element.value as? String
var delete: String = ""
if let value = currentValue {
Expand All @@ -949,8 +949,8 @@
}
let app = try self.getApp(withBundleId: bundleId)

let startCoordinate = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.9))
let endCoordinate = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.1))
let startCoordinate = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.7))
let endCoordinate = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.3))
startCoordinate.press(forDuration: 0.1, thenDragTo: endCoordinate)

attempts += 1 // Increment the scroll count after each scroll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@
inApp: request.appId,
dismissKeyboard: request.keyboardBehavior == .showAndDismiss,
withTimeout: request.timeoutMillis.map { TimeInterval($0 / 1000) },
dx: request.dx ?? 0.9,
dy: request.dy ?? 0.9
dx: request.dx,
dy: request.dy
)
} else {
throw PatrolError.internal("enterText(): neither index nor selector are set")
Expand Down

0 comments on commit 3d6d6f9

Please sign in to comment.